go-ordered-json icon indicating copy to clipboard operation
go-ordered-json copied to clipboard

Ordered object: object within json doesn't respect order

Open luispabon opened this issue 6 years ago • 1 comments
trafficstars

I'm currently using your library to aid with message signature verification via a hash from a legacy system, where reconstructing the original structure in the exact same order is paramount.

Consider the following json:

Loaded into an OrderedMap then iterated through, like so (omitting error handling for brevity):

jsonString := `{
    "stuff": {
        "a": "1",
        "b": "12",
        "c": "654"
    },
    "yolo": "covfefe",
    "yay": 5
}`

var unmarshalled json.OrderedObject

err := json.Unmarshal([]byte(jsonString), &unmarshalled)

for key, item := range unmarshalled {
  // do some stuff
}

All the values at the root level are processed in the right order 100% of the time. The "stuff" array however is a map[string]interface{} which by its own nature is unordered. Therefore its contents can come out in any random order.

These should probably be unmarshalled into another OrderedObject

luispabon avatar Jan 25 '19 10:01 luispabon

Hm, I thought it did that -- though I didn't write the code, just hosting it for convenience. :) If it doesn't, then that sounds like a bug to me. I'm on vacation until mid-February, but I'm happy to review any pull requests with that testcase added.

virtuald avatar Jan 26 '19 01:01 virtuald