Closed L0g4n closed 4 years ago
First off, you probably know this already, but the Value
type is only useful in the case where the document you're deserializing has an unknown structure. If the structure is known, it's preferable to use your own struct
s and derive Deserialize
for them.
That said, you're right that I haven't given the Value
any convenience APIs to use on top of pattern matching. I assume you'd like something akin to serde_json Value?
Now that you mention it - I probably should implement Deserialize
for my struct since the structure is known.
That said, you're right that I haven't given the Value any convenience APIs to use on top of pattern matching. I assume you'd like something akin to serde_json Value?
I just looked into them and just I think that would be nice - for the "unknown structure usecase" this would increase the usability quite a bit.
BTW, if your data would be valid JSON (e.g only string keys in dictionaries) you can even deserialize into serde_json::Value
and use its API :)
Well, it is quite similar to JSON since it's a Python dict with string keys that contains lists but I do not know if parsing the decoded pickle stuff would not lead to syntax errors in JSON.
Edit: In my case not feasible since my lists contain tuples which are not valid JSON.
Tuples will just deserialize to Arrays there.
Good to know, but I will try the "derive Deserialize
approach" first. I guess that means I have to implement the trait from serde
.
Yes, usually it's nothing more than #[derive(Deserialize)]
for each struct that appears in the (possibly nested) data structure. See https://serde.rs/derive.html
So, I tried the "derive Deserialize" approach just by myself, and it works pretty good 👍 After deserialization I now get debug output like this
Deserialized: RpaIndices({"all_assets.rpyc": [RpaTuple(1111638641, 1111684220, "")]})
The serde API seems pretty developer friendly, thanks for your help!
Nice! Yes, serde is one of the go-to examples of good API design in Rust.
Hi,
this might be a little dump question -- I am still rather new to Rust -- but what is the recommended approach to yield the parsed values, i.e. get access to the underlying Rust datatypes.
In my case: After parsing I now have
Value
, to be more precise aDict(BTreeMap<HashableValue::String, List(Vec<Value>)
as an enum variant. This of course represents a dictionary which has a string as a key and an entry that is a map that includes a tuple.So my question what is an idiomatic way to get the access to the underlying pure Rust datatypes? My first approach would be to pattern match to get access to the values but this seems cumbersome for nested datatypes like in my case. Or is the only way?