You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gods/maps/linkedhashmap/serialization.go

111 lines
2.1 KiB
Go

// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package linkedhashmap
import (
"bytes"
"cmp"
"encoding/json"
"slices"
"github.com/emirpasic/gods/v2/containers"
)
// Assert Serialization implementation
var _ containers.JSONSerializer = (*Map[string, int])(nil)
var _ containers.JSONDeserializer = (*Map[string, int])(nil)
// ToJSON outputs the JSON representation of map.
func (m *Map[K, V]) ToJSON() ([]byte, error) {
var b []byte
buf := bytes.NewBuffer(b)
buf.WriteRune('{')
it := m.Iterator()
lastIndex := m.Size() - 1
index := 0
for it.Next() {
km, err := json.Marshal(it.Key())
if err != nil {
return nil, err
}
buf.Write(km)
buf.WriteRune(':')
vm, err := json.Marshal(it.Value())
if err != nil {
return nil, err
}
buf.Write(vm)
if index != lastIndex {
buf.WriteRune(',')
}
index++
}
buf.WriteRune('}')
return buf.Bytes(), nil
}
// FromJSON populates map from the input JSON representation.
//func (m *Map[K, V]) FromJSON(data []byte) error {
// elements := make(map[string]interface{})
// err := json.Unmarshal(data, &elements)
// if err == nil {
// m.Clear()
// for key, value := range elements {
// m.Put(key, value)
// }
// }
// return err
//}
// FromJSON populates map from the input JSON representation.
func (m *Map[K, V]) FromJSON(data []byte) error {
elements := make(map[K]V)
err := json.Unmarshal(data, &elements)
if err != nil {
return err
}
index := make(map[K]int)
var keys []K
for key := range elements {
keys = append(keys, key)
esc, _ := json.Marshal(key)
index[key] = bytes.Index(data, esc)
}
byIndex := func(key1, key2 K) int {
return cmp.Compare(index[key1], index[key2])
}
slices.SortFunc(keys, byIndex)
m.Clear()
for _, key := range keys {
m.Put(key, elements[key])
}
return nil
}
// UnmarshalJSON @implements json.Unmarshaler
func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
return m.FromJSON(bytes)
}
// MarshalJSON @implements json.Marshaler
func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
return m.ToJSON()
}