You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
https://play.golang.org/p/AIAkjs_TELp
If we see above example. linkedhashmap is maintaining the order only on firsl level of keys. It is not maintaining the same for nested keys or maps.
Do we have any data structure that holds order in nested keys also?
@punithck unfortunately there is no data structure that can go beyond the first level simply due to the randomness in iteration order of maps when unmarshalling JSON to map in Golang.
The only way to achieve this is to write GoDS own unmarshaler as was done in json/decode
Given this would be a cool feature to have in GoDS as there is nothing out there that can anything similar on the web in Golang, it would give GoDS more value.
Hence, I will keep this issue open until GoDS JSON decode is implemented to unmarshal JSON into GoDS' data structures.
FromJSON is actually more problematic than described above:
Take this unit test:
funcTestGodsLinkedHashMapRetainsOrderWithComplexJSON(t*testing.T) {
jsonString:=`{ "a": 1, "c": { "b": 1, "a": 2, "c": 3 }, "d": 3, "b": 4}`data:=linkedhashmap.New[string, any]()
err:=json.Unmarshal([]byte(jsonString), &data)
iferr!=nil {
t.Fatal("Could not unmarshal json", err)
return
}
it:=data.Iterator()
it.Next()
ifit.Key() !="a" {
t.Error("First key not a")
}
it.Next()
ifit.Key() !="c" {
t.Error("Second key not c")
}
it.Next()
ifit.Key() !="d" {
t.Error("Third key not d") // IT FAILS HERE
}
it.Next()
ifit.Key() !="b" {
t.Error("Forth key not b") // And also here
}
}
@punithck points out that the object won't have sorted fields (its value is of the type map[string]interface{}). However, this test shows that the keys d and b are put in the wrong order because the key b is present in nested JSON.
shows that the logic works by finding the index of the key in the JSON itself, which seems messy.
I bumped this issue because one of the reasons I looked into GoDS is because it's one of the few libraries with a linked hash map implementation that seems standard enough to use in production code. I was hoping it could be my solution to parsing arbitrary JSON and keeping the fields in the order they were in the JSON. So, I was wondering if there were any plans to implement a JSON parser so that the order would be correct for complex JSON objects like I showed in the above unit test, and so that nested JSON objects can have their fields ordered correctly.
https://play.golang.org/p/AIAkjs_TELp
If we see above example. linkedhashmap is maintaining the order only on firsl level of keys. It is not maintaining the same for nested keys or maps.
Do we have any data structure that holds order in nested keys also?
Input:
Expected:
Ouput:
The text was updated successfully, but these errors were encountered: