Uh oh!
There was an error while loading. Please reload this page.
Update json::Encoder to properly encode maps. - #9142
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.
brandonson
commented
Sep 12, 2013
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. |
erickt
commented
Sep 12, 2013
@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. |
brandonson
commented
Sep 12, 2013
@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 |
thestinger
commented
Sep 12, 2013
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. |
brandonson
commented
Sep 12, 2013
@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 |
erickt
commented
Sep 13, 2013
@singingboyo: the problem is that it's really easy to break round-tripping the encoding with: Yes you could use |
brandonson
commented
Sep 13, 2013
@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. |
emberian
commented
Sep 25, 2013
This needs a rebase. |
alexcrichton
commented
Oct 23, 2013
Closing due to inactivity, but please reopen if you make progress on this! |
brandonson
commented
Oct 31, 2013
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 fixesrust-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