Native JSON for Rust
This crate provides native JSON syntax for Rust, it brings with a powerful way of parsing JSON syntax into native Rust structs. You can declare the JSON object natively as you do with JavaScript, JSON in Rust was made easy!
Note: This crate is just a crude proc-macro (compiler plugin), for more features, please refer to native-json
Usage
Add dependencies to your Cargo.toml, serde_json
is only needed if you want to stringify the JSON object.
[]
= "1.2"
= { = "1.0", = ["derive"] }
= "1.0"
Example of using native JSON object
use json;
use HashMap;
use ;
Declare a named JSON struct
With JSON declare syntax, you can declare nested native JSON object in place.
Note: Identifier with underscore suffix will be renamed when serialize and deserialize, type_
will be renamed to type
.
JSON Declare Syntax
json!
The native-json will generate native Rust structs for you, each object is named by object hierarchy path, concatenated with underscore.
JSON_OBJECT_NAME.object
was converted toJSON_OBJECT_NAME_object
JSON_OBJECT_NAME.array's item
was converted toJSON_OBJECT_NAME_array_item
Example of using named JSON object
use json;
use ;
use HashMap;
json!