Closed tyranron closed 5 years ago
The following code works OK:
use serde::{Deserialize, Serialize}; use serde_json; use serde_value; use uuid; #[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)] struct EventId(uuid::Uuid); #[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)] struct Event<I> { id: I, } #[test] fn newtype_deserialization() { let ev = Event { id: EventId(uuid::Uuid::nil()), }; let serialized = serde_json::to_string(&ev).expect("Serialization error"); assert_eq!( serialized, r#"{"id":"00000000-0000-0000-0000-000000000000"}"# ); //let value: serde_value::Value = let value: serde_json::Value = serde_json::from_str(&serialized).expect("Deserialization error"); let deserialized: Event<EventId> = Event::deserialize(value.clone()).expect("Deserialization error"); assert_eq!(deserialized, ev); }
However, if I try to use:
- //let value: serde_value::Value = - let value: serde_json::Value = + let value: serde_value::Value = + //let value: serde_json::Value = serde_json::from_str(&serialized).expect("Deserialization error");
I receive the following error:
thread 'newtype_deserialization' panicked at 'Deserialization error: InvalidType(Str("00000000-0000-0000-0000-000000000000"), "tuple struct EventId")', src/libcore/result.rs:997:5
which makes troubles to use serde_value::Value as intermediate representation in deserialization scenarios.
serde_value::Value
Also, .clone() here is required by serde_value::Value, while serde_json::Value can just borrow (I believe this is the same as #15):
.clone()
serde_json::Value
let deserialized: Event<EventId> = Event::deserialize(value.clone()).expect("Deserialization error");
Fix in #24 + #25
The following code works OK:
However, if I try to use:
I receive the following error:
which makes troubles to use
serde_value::Value
as intermediate representation in deserialization scenarios.Also,
.clone()
here is required byserde_value::Value
, whileserde_json::Value
can just borrow (I believe this is the same as #15):