繁体   English   中英

以不同的格式反序列化 JSON - Serde_JSON

[英]Deserialising JSON in a different format - Serde_JSON

我正在尝试从 Rust 中的文件中读取 JSON ,该文件具有以下尺寸:

{
    "DIPLOBLASTIC":"Characterizing the ovum when it has two primary germinallayers.",
    "DEFIGURE":"To delineate. [Obs.]These two stones as they are here defigured. Weever.",
    "LOMBARD":"Of or pertaining to Lombardy, or the inhabitants of Lombardy.",
    "BAHAISM":"The religious tenets or practices of the Bahais."
}

我想将每个单词及其描述存储在一个向量中(这是一个刽子手游戏)。 如果文件格式如下,我可以读取文件:

[
    {
        "word": "DIPLOBLASTIC",
        "description": "Characterizing the ovum when it has two primary germinallayers."
    },
    {
        "word": "DEFIGURE",
        "description": "To delineate. [Obs.]These two stones as they are here defigured. Weever."
    }
]

我使用以下代码执行此操作:

#[macro_use]
extern crate serde_derive;

use serde_json::Result;
use std::fs;

#[derive(Deserialize, Debug)]
struct Word {
    word: String,
    description: String,
}

fn main() -> Result<()> {
    let data = fs::read_to_string("src/words.json").expect("Something went wrong...");
    let words: Vec<Word> = serde_json::from_str(&data)?;
    println!("{}", words[0].word);
    Ok(())
}

但是,我试图弄清楚如何保留 JSON 文件的原始格式,而不将其转换为第二个 JSON 示例中的文字和描述。

有没有办法使用现有的 JSON 格式或者我需要重新格式化它?

如果要保存分配和迭代,可以使用自定义反序列化实现来完成此操作:

#[macro_use]
extern crate serde_derive;

use std::fmt;
use serde::de::{Deserialize, Visitor, MapAccess};

#[derive(Deserialize, Debug)]
struct Word {
    word: String,
    description: String,
}

#[derive(Debug)]
struct WordList {
    list: Vec<Word>,
}

#[derive(Debug)]
struct WordListVisitor;

impl<'de> Visitor<'de> for WordListVisitor {
    type Value = WordList;
    
    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a string->string map")
    }
    
    fn visit_map<A: MapAccess<'de>>(self, mut access: A) -> Result<Self::Value, A::Error> {
        let mut list = Vec::with_capacity(access.size_hint().unwrap_or(0));
    
        while let Some((word, description)) = access.next_entry()? {
            list.push(Word { word, description });
        }
        
        Ok(WordList { list })
    }
}


impl<'de> Deserialize<'de> for WordList {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_map(WordListVisitor)
    }
}

fn main() -> serde_json::Result<()> {
    let data = r#"{
        "DIPLOBLASTIC":"Characterizing the ovum when it has two primary germinallayers.",
        "DEFIGURE":"To delineate. [Obs.]These two stones as they are here defigured. Weever.",
        "LOMBARD":"Of or pertaining to Lombardy, or the inhabitants of Lombardy.",
        "BAHAISM":"The religious tenets or practices of the Bahais."
    }"#;
    let words: WordList = serde_json::from_str(data)?;
    println!("{:#?}", words);
    Ok(())
}

操场

您可以将 map 收集到HashMapBTreeMap ,然后使用其键值对来制作单词向量。

fn main() -> Result<()> {
    let data = r#"{
        "DIPLOBLASTIC":"Characterizing the ovum when it has two primary germinallayers.",
        "DEFIGURE":"To delineate. [Obs.]These two stones as they are here defigured. Weever.",
        "LOMBARD":"Of or pertaining to Lombardy, or the inhabitants of Lombardy.",
        "BAHAISM":"The religious tenets or practices of the Bahais."
    }"#;
    let words: std::collections::BTreeMap<String, String> = serde_json::from_str(data)?;
    let words = words
        .into_iter()
        .map(|(word, description)| Word { word, description })
        .collect::<Vec<_>>();
    println!("{:#?}", words);
    Ok(())
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM