-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Update json::Encoder to properly encode maps. #9142
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
Conversation
According to extra::serialize, maps where the key and value types are encodable should also be encodable, but extra::json only handled maps with string keys. This updates extra::json to allow for non-string keys.
Essentially, instead of using the key stored in the map as the json key, "key" and "val" are now used as the keys when emitting maps. In general the only problem I've found is that the json output is less readable. |
@singingboyo: Hey there! Thanks for this, but I'm leaning towards r- on this. Landing this will break our ability to use rust maps to interact with JSON apis. |
@erickt If I'm understanding you correctly, the concern you have is addressed by using If the Encodable implementation for Json is changed/hidden as suggested in #9028 then that would become |
I don't think it makes sense to do this. You can never make a reversible encoding of non-string keys with JSON so it should be prevented. |
@thestinger How so? The test case encodes/decodes a map with non-string keys without any issues, so I'm not sure what the concern is here. The json keys themselves can't be non-string, but this outputs |
@singingboyo: the problem is that it's really easy to break round-tripping the encoding with:
Yes you could use |
@erickt: I think there's still some misunderstanding on what this PR allows/prevents. The short version of this mini-essay is that round-tripping works intuitively as long as you don't mix and match encode/decode mechanisms. As long as values encoded using Encoder/PrettyEncoder are decoded using Decoder, and values encoded via the Json enum (so in most cases by using To steal your example:
This works and results in Regarding things being unintuitive: Mostly I'd be worried about someone encoding a map using Encoder and expecting The encoding/decoding implementation is fairly complex and is still possible to break (encode with Encoder, decode with just TL;DR: Round-tripping works intuitively as long as you don't mix and match encode/decode mechanisms. |
This needs a rebase. |
Closing due to inactivity, but please reopen if you make progress on this! |
Whoops, didn't intend to leave this sitting unattended for so long. In any case, I'm not sure I want to re-open. I feel like there's some doubt as to the efficacy of/need for this change. As I mentioned in my last reply here, it does work, and round-tripping is in a functional state. (Though, of course, it needs updating after several changes in other parts of the library.) However, looking at it again, I feel like the need to not have data flow from ToJson to Decoder or from Encoder to FromJson could potentially be a sticking point. If there's input saying that this change is wanted, then I'll happily jump back in and update it to match up with the latest rust, but without that input I feel I could better spend my time doing things like doc-fixing. (Which I haven't done enough of lately!) |
… r=Jarcho fix `undocumented-unsafe-blocks` false positive This fixes rust-lang#9142 by iterating over the parent nodes as long as within a block, expression, statement, local, const or static. --- changelog: none
According to extra::serialize, maps where the key and value types
are encodable should also be encodable, but extra::json only handled
maps with string keys. This updates extra::json to allow for
non-string keys.
Closes #8883
May also have some effect on #9028