dtolnay / miniserde

Data structure serialization library with several opposite design goals from Serde

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add more precise examples

fusion2222 opened this issue · comments

I am trying to deserialize a json string which can have various format. For this basically, we need to deserialize string containing json, into Value object. I followed docs, however looks like it is not possible to work with Value object, like docs say it will - (In this case docs are referring to serde_json) docs.

I would love to use miniserde, however I am very not sure how to use this library and docs are not helpful much. My non-working snippet:

use miniserde::{json, Serialize, Deserialize};

fn main() {
    let my_json_data = r#"
        {
            "name": "John Doe",
            "age": 43,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;

    let deserialized : json::Value = json::from_str(my_json_data).unwrap();
    println!("[+] Early quitting! {:#?}", deserialized["name"]);

The line at the bottom throws following error:

error[E0608]: cannot index into a value of type `miniserde::json::Value`

If you look at the definitions, Value is an enum. If you match on it and get an Object, it then derefs to a BTreeMap that you can index.