linkedhashmap maintaining order only on first level? Is there any data structure that holds order in nested maps also? #171
Labels
No labels
bug
discussion
documentation
duplicate
ignore
new feature
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
golibs/gms-gods#171
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
@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:
@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 keysdandbare put in the wrong order because the keybis present in nested JSON.github.com/emirpasic/gods@14f714261f/maps/linkedhashmap/serialization.go (L84)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.