Crate messy_json

Source
Expand description

§Introduction

The rust ecosystem allows for very good compile-time implementation of JSON deserializer to rust structure, however, things get a bit more sparse when it come to run-time deserialization of dynamically structured objects.

This crate approaches this problems in a simple manner, resembling serde_json’s Value.

§Usage

When deserializing from a known structure type, one would just call the serde’s deserializer and let it do its magic. However in this case, one needs to define how the JSON will be structured.

§Defining the schema

To do that, one can use the provided object : MessyJson

For instance defining an object that could look like the following in JSON :

{
    "hello": {
        "world": 128
    },
    "an_optional_one": "Waou"
}

One would define the following MessyJson :


let schema = MessyJson::from(MessyJsonInner::Obj(MessyJsonObject::from(MessyJsonObjectInner::new(
   vec![(
       arcstr::literal!("hello"),
       MessyJson::from(MessyJsonInner::Obj(MessyJsonObject::from(MessyJsonObjectInner::new(
           vec![(
               arcstr::literal!("world"),
               MessyJson::from(MessyJsonInner::String(MessyJsonScalar::new(false))),
           )]
           .into_iter()
           .collect(),
           false,
       )))),
   ),
(
    arcstr::literal!("an_optional_one"),
    MessyJson::from(MessyJsonInner::String(MessyJsonScalar::new(true)))
)]
   .into_iter()
   .collect(),
   false,
))));

Granted, this is a bit wordy to define such a simple structure but keep in mind that this should’nt be hand-written and should be composed by your application logic.

§Parsing the schema

To parse the &str using the schema one only need to crweate the deserializer and call it using the schema builder :


const DUMMY_OBJ: &str = r#"
{
        "hello": {
            "world": 128
        },
        "an_optional_one": "Waou"
    }
"#;

let mut deserializer = serde_json::Deserializer::from_str(DUMMY_OBJ);
let val: MessyJsonValueContainer = schema.builder(MessyJsonSettings::default()).deserialize(&mut deserializer).unwrap();

println!("{:#?}", val.inner());

Structs§

MessyJson
Wrapper for MessyJsonInner
MessyJsonArray
JSON Array schema value
MessyJsonArrayValue
Deserialized JSON Array Value
MessyJsonBuilder
Schema deserializer of a JSON Value
MessyJsonNumeric
JSON Number schema value
MessyJsonObject
Wrapper for MessyJsonObjectInner
MessyJsonObjectBuilder
Builder for MessyJsonObject
MessyJsonObjectInner
JSON Object schema value
MessyJsonObjectValue
Deserialized JSON Object Value
MessyJsonScalar
JSON Scalar schema value
MessyJsonSettings
Setting object for deserializing
MessyJsonValueContainer
Container for MessyJsonValue
MessyJsonValueRawVisitor

Enums§

MessyJsonExpected
An expected object, set when encountering a null value.
MessyJsonInner
Schema of a JSON Value
MessyJsonNullType
Deserialized JSON Null Value
MessyJsonNumberType
JSON Number type schema
MessyJsonValue
Deserialized JSON Value
MessyJsonValueRaw
Deserialized JSON Value

Type Aliases§

KeyType