-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserializing Structs containing flattened RawValues always fails #599
Comments
@fegies you will get that error when you are deserializing borrowed keys that require unescaping backslash, use String or Cow for the keys in your HashMap |
Deserializing to a Hashmap of with key type &str might lead to errors in cases with unesaping. This example is not such a case though. (As shown by the fact that TestC deserializes correctly) I think this error might be because of an Interaction with #[serde(flatten)] and #[serde(borrow)], and not related to the hashmap. Please observe the following example #[derive(Deserialize, Debug)]
struct DInner<'a> {
#[serde(borrow)]
b: &'a RawValue,
}
#[derive(Deserialize, Debug)]
struct TestD<'a> {
a : i32,
#[serde(borrow)]
#[serde(flatten)]
inner: DInner<'a>,
} which fails to deserialize with the same error, while the modified examples with owned HashMap key values still fails to deserialize. relevant rust playground: |
@fegies sorry, you are correct, seems to be an issue the code flatten generates. Here is fairly simple workaround https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e8e922add8a58d097d33cd98a3019560 |
@fegies a better version little bit more code (but not much) and does it in one pass https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3c2227417c489db7e833b8d90d2ba81b I wanted the same thing because I only want to process part of a json and pass through the rest. |
Just wondering, which level of difficulty does fixing this issue have? We are affected by it and we are willing to dedicate some time to work on a PR if feasible. |
We were using the `serde_json::Value` for the message payload which resulted in a lot of allocations when the payload was complex, and was causing high memory usage and fragmentation. With this, we don't actually parse this part of the json, but rather just treat it as a string (serde verifies it's valid json though). We unfortunately turn it into a `Value` when persisting to PG, we should fix this extra thing that diminishes the point of this change a bit. Includes a hack to workaround the serde_json flatten issue with RawValue: serde-rs/json#599
I encountered the same issue, and it appears that the reason
So, it seems that this bug cannot be easily fixed unless the way |
I think this is a duplicate of #1051. |
Output
Err(Error("invalid type: newtype struct, expected any valid JSON value", line: 4, column: 5))
Err(Error("invalid type: newtype struct, expected any valid JSON value", line: 4, column: 5))
Ok(TestC { rest: {"a": Number(42), "b": String("foo")} })
Expected behaviour
s is deserialized to all three structs successfully
Relevant Rust Playground
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1e2131c60a6b23e4a01c316786cbd7a6
Versions in use:
The text was updated successfully, but these errors were encountered: