This is full-featured modern JSON implementation according to ECMA-404 standard.
This crate allows deserialization of JSON io::Read
stream into primitive types (bool
, i32
, etc.),
String and any other types that implement special trait called TryFromJson
, which can be implemented
automatically through #[derive(TryFromJson)]
for your structs and enums.
And serialization back to JSON through DebugToJson
trait, that acts like Debug
, allowing to
print your objects with println!()
and such.
It allows to read whitespece-separated JSON values from stream in sequence. It also allows to pipe blob strings to a writer.
This implementation avoids unnecessary memory allocations and temporary object creations.
In Cargo.toml
of your project add:
toml
[dependencies]
nop-json = "1.0"
```rust use nop_json::Reader;
let mut reader = Reader::new(r#" true 100.5 "Hello" [true, false] "#.as_bytes());
let thetrue: bool = reader.read().unwrap();
let thehundredpointfive: f32 = reader.read().unwrap();
let thehello: String = reader.read().unwrap();
let thearray: Vec
asserteq!(thetrue, true);
asserteq!(thehundredpointfive, 100.5);
asserteq!(thehello, "Hello");
asserteq!(thearray, vec![true, false]);
``
First need to create a
Readerobject giving it something that implements
Iterator. In example above i use
"...".bytes()`.
Then call reader.read() to read each value from stream to some variable that implements TryFromJson
.
This crate has implementation of TryFromJson
for many primitive types, Vec
, HashMap
, and more.
We have generic Value
type that can hold any JSON node.
```rust use nop_json::{Reader, Value}; use std::convert::TryInto;
let mut reader = Reader::new(r#" true 100.5 "Hello" [true, false] "#.as_bytes());
let thetrue: Value = reader.read().unwrap(); let thehundredpointfive: Value = reader.read().unwrap(); let thehello: Value = reader.read().unwrap(); let thearray: Value = reader.read().unwrap();
asserteq!(thetrue, Value::Bool(true)); let thehundredpointfive: f32 = thehundredpointfive.tryinto().unwrap(); asserteq!(thehundredpointfive, 100.5f32); asserteq!(thehello, Value::String("Hello".tostring())); asserteq!(thearray, Value::Array(vec![Value::Bool(true), Value::Bool(false)])); ```
You can parse any JSON document to Value
.
``` use nop_json::{Reader, Value};
let mut reader = Reader::new(r#" {"array": [{"x": 1}, "a string"]} "#.bytes()); let doc: Value = reader.read().unwrap(); asserteq!(doc.tostring(), r#"{"array":[{"x":1},"a string"]}"#); ```
To deserialize a struct or an enum, your struct needs to implement TryFromJson
trait.
To serialize - DebugToJson
.
```rust use nop_json::{Reader, TryFromJson, DebugToJson};
struct Point {x: i32, y: i32}
enum Geometry { #[json(point)] Point(Point), #[json(cx, cy, r)] Circle(i32, i32, i32), Nothing, }
let mut reader = Reader::new(r#" {"point": {"x": 0, "y": 0}} "#.as_bytes());
let obj: Geometry = reader.read().unwrap();
println!("Serialized back to JSON: {:?}", obj);
``
See
TryFromJson,
DebugToJson`.
You can println!() word "true" or "false" to serialize a boolean. Also numbers can be printed as println!() does by default.
The format is JSON-compatible. To serialize a &str, you can use escape
function.
Alternatively you can create a Value
object, and serialize it.
To skip current value without storing it (and allocating memory), read it to the ()
type.
```rust
use nop_json::Reader;
let mut reader = Reader::new(r#" true 100.5 "Hello" [true, false] "#.as_bytes());
let _: () = reader.read().unwrap(); let _: () = reader.read().unwrap(); let _: () = reader.read().unwrap(); let _: () = reader.read().unwrap(); ```
See read_blob
.
License: MIT