github.com/mymmsc/gox@v1.3.33/util/hashbidimap/serialization.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package hashbidimap
     6  
     7  import (
     8  	"encoding/json"
     9  	"github.com/mymmsc/gox/util"
    10  )
    11  
    12  func assertSerializationImplementation() {
    13  	var _ util.JSONSerializer = (*Map)(nil)
    14  	var _ util.JSONDeserializer = (*Map)(nil)
    15  }
    16  
    17  // ToJSON outputs the JSON representation of the map.
    18  func (m *Map) ToJSON() ([]byte, error) {
    19  	return m.forwardMap.ToJSON()
    20  }
    21  
    22  // FromJSON populates the map from the input JSON representation.
    23  func (m *Map) FromJSON(data []byte) error {
    24  	elements := make(map[string]interface{})
    25  	err := json.Unmarshal(data, &elements)
    26  	if err == nil {
    27  		m.Clear()
    28  		for key, value := range elements {
    29  			m.Put(key, value)
    30  		}
    31  	}
    32  	return err
    33  }