-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for
serde
and serde_json
.
Fixes #2
- Loading branch information
1 parent
d2e619c
commit 9483e51
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! This example shows how to serialize and deserialize `json_syntax::Value` | ||
//! using the `serde` crate. This will not allow you to attach metadata to each | ||
//! value fragment (the `M` type will be unit `()`). | ||
use json_syntax::Print; | ||
|
||
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)] | ||
struct MyType { | ||
foo: String, | ||
bar: Vec<u32>, | ||
} | ||
|
||
fn main() { | ||
// Instantiate our type. | ||
let a = MyType { | ||
foo: "Hello World!".to_string(), | ||
bar: vec![1, 2, 3], | ||
}; | ||
|
||
// Serialize `a` into a JSON value. | ||
let json = json_syntax::to_value(&a).expect("serialization failed"); | ||
|
||
// Print the value. | ||
println!("{}", json.pretty_print()); | ||
|
||
// Deserialize JSON back into `MyType`. | ||
let b: MyType = json_syntax::from_value(json).expect("deserialization failed"); | ||
|
||
// The round-trip should not change the actual data. | ||
assert_eq!(a, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//! This example shows how to convert a `serde_json::Value` into/from a | ||
//! `json_syntax::Value` using the `serde_json` feature. | ||
fn main() { | ||
// First we create a `serde_json` value. | ||
let a = serde_json::json!({ | ||
"foo": 1, | ||
"bar": [2, 3] | ||
}); | ||
|
||
// We convert the `serde_json` value into a `json_syntax` value. | ||
let b = json_syntax::Value::from_serde_json(a, |fragment| { | ||
// `json_syntax` keeps metadata information attached to every value | ||
// fragment, which `serde_json` does not. This is why this closure is | ||
// necessary. It is called for every `serde_json` fragment to let you | ||
// choose the metadata you want to associate to the fragment. | ||
// This is intended to store code mapping information, but you can store | ||
// any information. Here we store a text description of the fragment. | ||
match fragment { | ||
json_syntax::SerdeJsonFragment::Key(key) => { | ||
format!("I'm an object key `{key}`") | ||
} | ||
json_syntax::SerdeJsonFragment::Value(value) => match value { | ||
serde_json::Value::Null => "I'm the `null` value".to_string(), | ||
serde_json::Value::Bool(b) => format!("I'm the boolean `{b:?}`"), | ||
serde_json::Value::Number(n) => format!("I'm the number {n}"), | ||
serde_json::Value::String(s) => format!("I'm the string {s:?}"), | ||
serde_json::Value::Array(a) => format!("I'm an array of {} elements", a.len()), | ||
serde_json::Value::Object(o) => format!("I'm an object of {} entries", o.len()), | ||
}, | ||
} | ||
}); | ||
|
||
// We convert it back into a `serde_json` value. | ||
let _ = json_syntax::Value::into_serde_json(b); | ||
} |