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§
- Messy
Json - Wrapper for MessyJsonInner
- Messy
Json Array - JSON Array schema value
- Messy
Json Array Value - Deserialized JSON Array Value
- Messy
Json Builder - Schema deserializer of a JSON Value
- Messy
Json Numeric - JSON Number schema value
- Messy
Json Object - Wrapper for MessyJsonObjectInner
- Messy
Json Object Builder - Builder for MessyJsonObject
- Messy
Json Object Inner - JSON Object schema value
- Messy
Json Object Value - Deserialized JSON Object Value
- Messy
Json Scalar - JSON Scalar schema value
- Messy
Json Settings - Setting object for deserializing
- Messy
Json Value Container - Container for MessyJsonValue
- Messy
Json Value RawVisitor
Enums§
- Messy
Json Expected - An expected object, set when encountering a null value.
- Messy
Json Inner - Schema of a JSON Value
- Messy
Json Null Type - Deserialized JSON Null Value
- Messy
Json Number Type - JSON Number type schema
- Messy
Json Value - Deserialized JSON Value
- Messy
Json Value Raw - Deserialized JSON Value