簡體   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