I can decode json strings to a map with go language like this:

func main(){
  date := []byte(`{"127.1":{"host":"host1","list":["list123","list456"]},"127.2":{"host":"host2","list":["list223","list256"]}}`)
  var x interface{}
  json.Unmarshal(date, &x)
  t := x.(map[string]interface{})
  var aa []interface{}
  aa = (t["127.2"].(map[string]interface{})["list"])
  for _, v := range aa {
     fmt.Println(v.(string))
  }
}

but I wonder how to decode it to a sync.Map in Go1.9. I have tried many ways but failed, can anyone help me?

I tried like this:

    func main(){
      date := []byte(`{"127.1":{"host":"host1","list":["list123","list456"]},"127.2":{"host":"host2","list":["list223","list256"]}}`)
      var x interface{}
      json.Unmarshal(date, &x)
      t := x.((sync.Map)[string]interface{})  //compile error
}

Also I tried like this:

    func main(){
      date := []byte(`{"127.1":{"host":"host1","list":["list123","list456"]},"127.2":{"host":"host2","list":["list223","list256"]}}`)
      var x sync.Map
      json.Unmarshal(date, &x)
      fmt.Println(x) // but the map has nothing
}
  • 1
    Are you sure you really even want a sync.Map? It has a very particular use case, and even documents that outside of its intended use case "it will likely have comparable or worse performance and worse type safety than an ordinary map paired with a read-write mutex" – JimB Sep 24 '17 at 18:26
  • I have a webserver to serve much clients, each client may use a goroutine to request the server and lookup the map on the server. Then the server response the result to each request . Also the server have one goroutine to update the map every 10 minutes by requesting to a remote server. I will test the performance for each map type, thanks for your reminding. – zengxiaobai2 Sep 25 '17 at 1:01
  • 1
    sync.RWMutex seems absolutely reasonable for writes every 10 minutes. – Peter Sep 25 '17 at 14:51

You cannot directly unmarshal into a sync.Map, because sync.Map has no exported fields (so the Unmarshaler doesn't have any way to store data in it), and it doesn't implement the json.Unmarshaler interface.

So you'll have to handle this yourself, probably by including a sync.Map in a type of your own, and implementing json.Unmarshaler on that type:

type Foo struct {
    sync.Map
}

func (f *Foo) UnmarshalJSON(data []byte) error {
    var tmpMap map[string]interface{}
    if err := json.Unmarshal(data, &tmpMap); err != nil {
        return err
    }
    for key, value := range tmpMap {
        f.Store(key.value)
    }
    return nil
}
  • Thanks, it is a way to solve it indeed. – zengxiaobai2 Sep 25 '17 at 0:42

In case you need a snippet to do it another way around

func (f Foo) MarshalJSON() ([]byte, error) {
    tmpMap := make(map[YourTypeOfKey]YourTypeOfValue)
    f.Range(func(k, v interface{}) bool {
        tmpMap[k.(YourTypeOfKey)] = v.(YourTypeOfValue)
        return true
    })
    return json.Marshal(tmpMap)
}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.