arcnmx / serde-value

Serialization value trees
http://arcnmx.github.io/serde-value/serde_value/
MIT License
43 stars 30 forks source link

Cannot deserialize into a newtype tuple struct #22

Closed tyranron closed 5 years ago

tyranron commented 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.

Also, .clone() here is required by serde_value::Value, while serde_json::Value can just borrow (I believe this is the same as #15):

    let deserialized: Event<EventId> =
        Event::deserialize(value.clone()).expect("Deserialization error");
Arnavion commented 5 years ago

Fix in #24 + #25