#serde_json struct fails to deserialize itself

12 messages · Page 1 of 1 (latest)

lime apex
#

The following struct definition:

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Graph {
    hash: String,
    #[serde(rename = "parent_hash")]
    parent_hash: String,
    thumb_url: String,
    state_url: String,
    title: String,
    access: String,
    created: String,
    state: GraphState,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct GraphState {
    pub version: String,
    pub random_seed: String,
    //pub graph: GraphMeta,
    //pub expressions: Vec<Expression>,
}

fails to do a roundtrip serialize->deserialize with the error "missing field version". This is probably either a really obvious case of user error that I'm missing or an issue with serde_derive

short surge
#

Can you show an example value and the json it serializes to?

lime apex
#

yep

#
Graph {
    hash: "",
    parent_hash: "",
    thumb_url: "",
    state_url: "",
    title: "",
    access: "",
    created: "",
    state: GraphState {
        version: "",
        random_seed: "",
    },
}```
serializes to
```json
{"hash":"","parent_hash":"","thumbUrl":"","stateUrl":"","title":"","access":"","created":"","state":{"version":"","randomSeed":""}}
#

which fails to deserialize

short surge
#

?eval

use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Graph {
    hash: String,
    #[serde(rename = "parent_hash")]
    parent_hash: String,
    thumb_url: String,
    state_url: String,
    title: String,
    access: String,
    created: String,
    state: GraphState,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct GraphState {
    pub version: String,
    pub random_seed: String,
    //pub graph: GraphMeta,
    //pub expressions: Vec<Expression>,
}

let g = Graph::default();
let j = serde_json::to_string(&g).unwrap();
eprintln!("{g:?} = {j}");
serde_json::from_str::<Graph>(&j)
neat topazBOT
#
Graph { hash: "", parent_hash: "", thumb_url: "", state_url: "", title: "", access: "", created: "", state: GraphState { version: "", random_seed: "" } } = {"hash":"","parent_hash":"","thumbUrl":"","stateUrl":"","title":"","access":"","created":"","state":{"version":"","randomSeed":""}}

Ok(Graph { hash: "", parent_hash: "", thumb_url: "", state_url: "", title: "", access: "", created: "", state: GraphState { version: "", random_seed: "" } })```
lime apex
#

what the heck

#

OH lol

#

it was the former case in my prediction

#

Im just an idiot

#

trying to deserialize a Graph's json into a GraphState