Crate messy_json[−][src]
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 :
use messy_json::*; let schema: MessyJson = MessyJson::Obj(Box::new(MessyJsonObject::new( vec![( "hello".to_string(), MessyJson::Obj(Box::new(MessyJsonObject::new( vec![( "world".to_string(), MessyJson::Number(MessyJsonNumeric::new(MessyJsonNumberType::U64, false)), )] .into_iter() .collect(), false, ))), ), ( "an_optional_one".to_string(), MessyJson::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 :
use serde::de::DeserializeSeed; use messy_json::*; 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().deserialize(&mut deserializer).unwrap(); println!("{:#?}", val.inner());
Structs
MessyJsonArray | JSON Array schema value |
MessyJsonBuilder | Schema deserializer of a JSON Value |
MessyJsonNumeric | JSON Number schema value |
MessyJsonObject | JSON Object schema value |
MessyJsonScalar | JSON Scalar schema value |
MessyJsonValueContainer | Container for MessyJsonValue |
Enums
MessyJson | Schema of a JSON Value |
MessyJsonNumberType | JSON Number type schema |
MessyJsonValue | Deserialized JSON Value |