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
easyjson unmarshals empty slice []
into nil slice instead of empty slice, which in turn will be marshalled to null
instead of empty slice.
#188
Comments
https://github.com/mailru/easyjson/blob/master/jwriter/writer.go#L17 You can create your own jwriter.Writer with this flags. |
@rvasily can you close? |
@shmel1k I don't think that is a valid answer. an empty array shouldn't be marshalled as null. |
@james-lawrence https://github.com/mailru/easyjson/blob/master/tests/basic_test.go#L176 If I remember correctly, this behavior is supported. You can marshal as null or as an empty slice. The comment above explains this. |
@shmel1k, thanks for the explanation. Is there a better way to do this? Without tampering the source code. Because creating my own jwriter.Writer will require changing code in |
@shmel1k . If a map is defined as map[string]interface{} and if it's Unmarshalled from a json like this:
The map should have a key-value pair with this (And this is what happens when using json.Unmarshal:
However when using easyjson.Unmarshal, it becomes:
In a nutshell, the unmashalling should Marshal '[]' to an empty slice, instead of a The following changes to the source code will solution this problem: change the line at https://github.com/mailru/easyjson/blob/master/jlexer/lexer.go#L1154 from
to
This will align the easyjson.Unmarshal behaviour with the json.Unmarshal when dealing with empty slice value in a map. |
Yeah, I agree that you're right :) You can send pull-request for this fix |
Cool. I have made a pull request, #190 |
I do not have permissions to merge, so ask @rvasily |
I know this PR is kind of old but it fixes the issue my team is facing right now. Is there anyway we can get this merged in @rvasily? Thanks very much for your time! |
#190 merged |
Hi, I've noticed some strange behaviour recently. See below for the details.
First of all, I define the following type:
This allows me to store a key-value set with the value being anything I like.
I then give this some values like this:
If I marshal this into JSON, it looks like:
If I store this JSON string somewhere, e.g. database and use it later. When I read it out and unmarshal it back to
Attributes
, it looks like this:Now the value of
key4
becomes nil, instead of empty slice. This is problematic since if I marshal it again, it becomes "null" in the JSON:If I use encoding/json library instead of easyjson, it works ok. See below:
output:
It's consistent, empty slice is always empty slice, not nil, and null.
The text was updated successfully, but these errors were encountered: