Skip to content
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

json encoding of a map - order of members in an object #6244

Closed
gopherbot opened this issue Aug 25, 2013 · 7 comments
Closed

json encoding of a map - order of members in an object #6244

gopherbot opened this issue Aug 25, 2013 · 7 comments

Comments

@gopherbot
Copy link

by janh4lfar:

Object members should not be ordered:

From http://www.ietf.org/rfc/rfc4627.txt?number=4627

"... An object is an unordered collection of zero or more name/value
pairs, where a name is a string and a value is a string, number,
boolean, null, object, or array. ..."

In other words - do not object members by their names.

Expected behaviour as implemented in nodejs, php, chrome and firefox as reference:

> var test = {"BBB":"b", "AAA" : "a",
"CCC" : "c"}
undefined
> test
{ BBB: 'b', AAA: 'a', CCC: 'c' }
> JSON.stringify(test)
'{"BBB":"b","AAA":"a","CCC":"c"}'

A little go snippet:

testMap := make(map[string]string)
originalJsonBytes := []byte(`{"BBB":"b", "AAA" :
"a", "CCC" : "c"}`)
json.Unmarshal(originalJsonBytes, &testMap)
jsonBytes, _ := json.Marshal(testMap)
fmt.Println(testMap, string(jsonBytes))

// current output: json members are sorted by their name:
map[BBB:b AAA:a CCC:c]
{"AAA":"a","BBB":"b","CCC":"c"}

// right output to ensure interoperability - imho it should look like this:
map[BBB:b AAA:a CCC:c]
{"BBB":"b","AAA":"a","CCC":"c"}

The solution of the bug seems to be very simple - just remove sort.Sort(sv) in
src/pkg/encoding/json/encode.go in mapEncoder.encode() - obviously that might not be the
only place to look for ;)

Just by the way - I love this package!
@cznic
Copy link
Contributor

cznic commented Aug 25, 2013

Comment 1:

IMO you've get the meaning of the word "unordered", as used in the referenced RFC,
wrong. The 'unordered' property of (a Javascript) object simply means that the below two
JSONs represent same object _values_:
        {"A": 1, "B": 2}
and
        {"B": 2, "A": 1}
IOW, proper interoperability IMO _must not_ depend on the order of the name/value pairs
in the JSON representation.

@bradfitz
Copy link
Contributor

Comment 2:

Status changed to WorkingAsIntended.

@gopherbot
Copy link
Author

Comment 3 by janh4lfar:

@0xj...
The honourable Douglas Crockford for sure could have been more precise. 
Assuming the order of your objects members is preserved - this is all you need to have
your top contributors of this week.
var topContributorsThisWeek = {
  "xxx@bar.com" : "For doing this",
  "aaa@foo.org"   : "Did that"
}
If not, you have to preserve the order in an extra object
var topContributorsThisWeek = {
  "contributors" : {
    "aaa@foo.org"   : "Did that",
    "xxx@bar.com" : "For doing this"
  },
  "order" : [
    "xxx@bar.com",
    "aaa@foo.org"
  ]
}
the "unordered" order of object members is like an implicit list - it is like buy two
get one for free ;)
Nazdar - Honza

@gopherbot
Copy link
Author

Comment 4 by janh4lfar:

@bradfitz - that is sad to see please take a look at my last comment. Even if the JSON
spec is a little unspecific on this please consider, that JSON is there for
interoperability.

@gopherbot
Copy link
Author

Comment 5 by janh4lfar:

The order of members in a JSON object matters - you can not just sort them
alphabetically. Just think of a list of country codes.

@gopherbot
Copy link
Author

Comment 6 by janh4lfar:

http://play.golang.org/p/LL4euXjM7a

@bradfitz
Copy link
Contributor

Comment 7:

Sorry, but you're just wrong here. JSON objects are unordered, just like Go maps.  If
you're depending on the order that a specific implementation serializes your JSON
objects in, you have a bug.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants