orderedmap icon indicating copy to clipboard operation
orderedmap copied to clipboard

Preserve json keys order while marshalling in Go

Open wathika-eng opened this issue 10 months ago • 1 comments

https://goplay.tools/snippet/DmuiHIvVNdh

After marshalling returns nil, the orderedmap library does not implement Go's json.Marshaler interface directly.

wathika-eng avatar Feb 13 '25 14:02 wathika-eng

This has been raised before: https://github.com/elliotchance/orderedmap/issues/12

The problem is that while this type can be opinionated about how it's encoded it's not safe to assume the same is true for decoding. For example, multiple JSON formats would be acceptable for decoding and it's not sensible to settle on one.

That is why you should create your own marshaller for the symmetrical encode/decode format you desire. I'm open to accepting these new types into the library, but with some discussion first.

elliotchance avatar Jul 09 '25 14:07 elliotchance