orderedmap icon indicating copy to clipboard operation
orderedmap copied to clipboard

`json.Marshal` — doesn't work

Open RubaXa opened this issue 4 years ago • 6 comments

Hi,

I try convert OrderedMap to json, but receive empty object :/

package main

import (
	"fmt"
	"encoding/json"
	"github.com/elliotchance/orderedmap"
)

func main() {
	m := orderedmap.NewOrderedMap()

	m.Set("foo", "bar")
	m.Set("qux", 1.23)
	m.Set(123, true)

	b, err := json.Marshal(m)
	if err != nil {
		fmt.Println("error:", err)
	}
	
	fmt.Println("result:", string(b)) // "result: {}" ⚠️
}

https://play.golang.org/p/UFER3tR6uPA

RubaXa avatar Mar 31 '20 13:03 RubaXa

This is because marshalling has not been implemented for orderedmap.

I'm not sure what this would actually look like because it must be reasonable that the order would also be retained in the data structure. So simply using a JSON object would not be sufficient.

Since there are multiple ways marshalling and unmarshalling could work so it's best to leave it up to the implementation. Here is one example that might suit your needs:

https://play.golang.org/p/LjOrp4k6lqf

elliotchance avatar Apr 01 '20 18:04 elliotchance

This is because marshalling has not been implemented for orderedmap.

I'm not sure what this would actually look like because it must be reasonable that the order would also be retained in the data structure. So simply using a JSON object would not be sufficient.

Since there are multiple ways marshalling and unmarshalling could work so it's best to leave it up to the implementation. Here is one example that might suit your needs:

https://play.golang.org/p/LjOrp4k6lqf

marshaling and unmarshaling may cause unexpected issue, like int type will be marshal into int64 type in your example https://play.golang.org/p/IjP7E3lRMhT

abusizhishen avatar Jul 09 '20 03:07 abusizhishen

If the order and specific data types must be retained in the serialized value then you will need a more complex marshaller to suit your specific needs.

elliotchance avatar Jul 09 '20 13:07 elliotchance

This is because marshalling has not been implemented for orderedmap.

I'm not sure what this would actually look like because it must be reasonable that the order would also be retained in the data structure. So simply using a JSON object would not be sufficient.

Since there are multiple ways marshalling and unmarshalling could work so it's best to leave it up to the implementation. Here is one example that might suit your needs:

https://play.golang.org/p/LjOrp4k6lqf

The structure after serialization is not what I want.

According to your code result is

[
   [
     "2",
    {
      "a": "2",
      "b": "7"
    }
  ],
  [
    "3",
    {
      "a": "3",
      "b": "1"
    }
  ]
]

Expect:

{
  "2": {
        "a": 3,
        "b": 4
    }
}

89trillion-feiyang avatar Jun 30 '21 09:06 89trillion-feiyang

@89trillion-feiyang - you can serialize it in any format you want. Here's some sample code to produce that: https://play.golang.org/p/tKGpbGCpFbC

Keep in mind some caveats with that:

  1. The order will be lost when serializing (and unserializing). There's no way around that because Go sorts maps for JSON output.
  2. All of your keys must be strings.

elliotchance avatar Jul 01 '21 14:07 elliotchance

@89trillion-feiyang - you can serialize it in any format you want. Here's some sample code to produce that: https://play.golang.org/p/tKGpbGCpFbC

Keep in mind some caveats with that:

  1. The order will be lost when serializing (and unserializing). There's no way around that because Go sorts maps for JSON output.
  2. All of your keys must be strings.

OK. Thanks! I solved it.

89trillion-feiyang avatar Jul 02 '21 07:07 89trillion-feiyang