-
Notifications
You must be signed in to change notification settings - Fork 13k
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
libserialize: encoding non string key map to json silently produces invalid json instead of failing #19490
Conversation
This seems too "ugly" for normal uses. Can we do better? For example, we may optionally implement (or insert something like |
i'd prefer keeping it explicit. |
The autoserialization is commonly used for types that are not aware of serialization (but could have been made aware of it). Adding (I have made it clear that this specific issue is subjective, as "ugly" is quoted, but I don't think that making the output format overly unconventional is good in general. If that were a concern we should have used some other format specific to us.) |
I agree with @lifthrasiir that this seems like the wrong default. This means that any users encoding a |
yes, which is the only sensible way for non-string (or not trivially convertable to string) keys. |
I think that not serializing maps as maps is such a gotcha that |
I agree with @alexcrichton. I've felt a bit weird with the JSON module's use of |
We also serialize enums with fields in a way that feels a bit shoehorned in. |
another alternative would be to output invalid json (map key is a json value, not a string), and to require the json serializer to be created with a flag explicitly allowing that. |
go-lang: http://golang.org/pkg/encoding/json/#Marshal (has flag to convert key to json and escape as string) python: https://docs.python.org/2/library/json.html (convert to string in python-specific format)
.net: http://james.newtonking.com/json/help/index.html?topic=html/SerializationGuide.htm other languages can't get their head around one or another way either. I agree now with @alexcrichton, an error in serialization is the right way to go. i'll update this PR. |
I have tried several schemes:
A few more possible ways to do these checks
|
One possible solution would be to set a flag when emitting a string and resetting it in all other emissions, and then after you emit a key if the flag is set you know you emitted a string and otherwise you know that it wasn't a key. The other direction is also fine where you set a flag when emitting a key and then when you emit a type elsewhere it'll check the flag to make sure it's valid |
afd14a6
to
a8929fa
Compare
That sounds reasonable. panicking -> error reporting will be done in another PR (after Encoder+PrettyEncoder merge) |
Awesome, looks great! Could you actually go a head an add a variant to the error enum returned to reflect this new condition? (as opposed to waiting to a further PR) |
a8929fa
to
b1f8a41
Compare
c85de97
to
fa718dd
Compare
r? @alexcrichton |
aef867a
to
bc0236f
Compare
bc0236f
to
66426be
Compare
rebased and fixed |
66426be
to
d3e903b
Compare
fixed unit test, rebased and fixed rebase fallout |
135da92
to
81173a3
Compare
phew, good thing it took me so long to get this ready... it doesn't work! |
b3fb183
to
c0490ae
Compare
@alexcrichton it is fixed and tested now, i actually messed up the unit test before, that's why the mistake wasn't caught by it. |
should i squash the fix? |
Sure! |
c0490ae
to
b262ab5
Compare
done |
@alexcrichton those failures don't seem to be mine, should i rebase or sth? |
Conflicts: src/libserialize/serialize.rs
b262ab5
to
a320149
Compare
rebased |
the only fix required was changing a |
importing object type string key maps is still supported
writing them should be explicit, and can be done as follows
related to #8335, #9028, #9142