-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bearcove/mixed
Add example for deserializing mixed arrays
- Loading branch information
Showing
8 changed files
with
558 additions
and
262 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,99 @@ | ||
use std::borrow::Cow; | ||
|
||
use jiter::JsonArray; | ||
use merde_json::{ | ||
Fantome, JsonArrayExt, JsonDeserialize, JsonSerialize, JsonSerializer, JsonValue, JsonValueExt, | ||
ToRustValue, | ||
}; | ||
|
||
#[derive(Debug, PartialEq)] | ||
struct MixedArray<'src, 'val> { | ||
_boo: Fantome<'src, 'val>, | ||
|
||
items: Vec<&'val JsonValue<'src>>, | ||
} | ||
merde_json::derive! { | ||
impl(JsonSerialize, JsonDeserialize) for MixedArray { items } | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
struct MixedArray2<'src, 'val> { | ||
_boo: Fantome<'src, 'val>, | ||
|
||
items: &'val JsonArray<'src>, | ||
} | ||
merde_json::derive! { | ||
impl(JsonSerialize, JsonDeserialize) for MixedArray2 { items } | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
struct Items<'src, 'val> { | ||
_boo: Fantome<'src, 'val>, | ||
|
||
number: u32, | ||
string: Cow<'val, str>, | ||
boolean: bool, | ||
} | ||
|
||
impl<'src, 'val> JsonDeserialize<'src, 'val> for Items<'src, 'val> | ||
where | ||
'src: 'val, | ||
{ | ||
fn json_deserialize( | ||
value: Option<&'val JsonValue<'src>>, | ||
) -> Result<Self, merde_json::MerdeJsonError> { | ||
let arr = value | ||
.ok_or(merde_json::MerdeJsonError::MissingValue)? | ||
.as_array()?; | ||
|
||
Ok(Items { | ||
_boo: Default::default(), | ||
|
||
number: arr.must_get(0)?, | ||
string: arr.must_get(1)?, | ||
boolean: arr.must_get(2)?, | ||
}) | ||
} | ||
} | ||
|
||
impl JsonSerialize for Items<'_, '_> { | ||
fn json_serialize(&self, serializer: &mut JsonSerializer) { | ||
serializer | ||
.write_arr() | ||
.elem(&self.number) | ||
.elem(&self.string) | ||
.elem(&self.boolean); | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
struct MixedArray3<'src, 'val> { | ||
_boo: Fantome<'src, 'val>, | ||
|
||
items: Items<'src, 'val>, | ||
} | ||
merde_json::derive! { | ||
impl(JsonSerialize, JsonDeserialize) for MixedArray3 { items } | ||
} | ||
|
||
fn main() { | ||
let input = r#" | ||
{ | ||
"items": [ | ||
42, "foo", true | ||
] | ||
} | ||
"#; | ||
|
||
let ma = merde_json::from_str(input).unwrap(); | ||
let ma: MixedArray = ma.to_rust_value().unwrap(); | ||
println!("{:?}", ma); | ||
|
||
let ma = merde_json::from_str(input).unwrap(); | ||
let ma: MixedArray2 = ma.to_rust_value().unwrap(); | ||
println!("{:?}", ma); | ||
|
||
let ma = merde_json::from_str(input).unwrap(); | ||
let ma: MixedArray3 = ma.to_rust_value().unwrap(); | ||
println!("{:?}", ma); | ||
} |
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,61 @@ | ||
use std::borrow::Cow; | ||
|
||
use merde_json::{Fantome, ToRustValue, ToStatic}; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, PartialEq, Eq)] | ||
struct Address<'src, 'val> { | ||
_boo: Fantome<'src, 'val>, | ||
|
||
street: Cow<'val, str>, | ||
city: Cow<'val, str>, | ||
state: Cow<'val, str>, | ||
zip: u16, | ||
} | ||
|
||
merde_json::derive! { | ||
impl (JsonDeserialize, ToStatic) for Address { | ||
street, | ||
city, | ||
state, | ||
zip | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, PartialEq, Eq)] | ||
struct Person<'src, 'val> { | ||
_boo: Fantome<'src, 'val>, | ||
|
||
name: Cow<'val, str>, | ||
age: u8, | ||
address: Address<'src, 'val>, | ||
} | ||
|
||
merde_json::derive! { | ||
impl (JsonDeserialize, ToStatic) for Person { name, age, address } | ||
} | ||
|
||
fn get_person() -> Person<'static, 'static> { | ||
let input = r#" | ||
{ | ||
"name": "John Doe", | ||
"age": 42, | ||
"address": { | ||
"street": "123 Main St", | ||
"city": "Anytown", | ||
"state": "CA", | ||
"zip": 12345 | ||
} | ||
} | ||
"#; | ||
|
||
let person = merde_json::from_str(input).unwrap(); | ||
let person: Person = person.to_rust_value().unwrap(); | ||
person.to_static() | ||
} | ||
|
||
fn main() { | ||
let person = get_person(); | ||
println!("{:?}", person); | ||
} |
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
Oops, something went wrong.