Debian Codegraph @

maps/hashbidimap/serialization.go

raw symbols SCIP
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
10 "github.com/emirpasic/gods/v2/containers"
11 )
12
13 // Assert Serialization implementation
14 var _ containers.JSONSerializer = (*Map[string, int])(nil)
15 var _ containers.JSONDeserializer = (*Map[string, int])(nil)
16
17 // ToJSON outputs the JSON representation of the map.
18 func (m *Map[K, V]) ToJSON() ([]byte, error) {
19 return m.forwardMap.ToJSON()
20 }
21
22 // FromJSON populates the map from the input JSON representation.
23 func (m *Map[K, V]) FromJSON(data []byte) error {
24 var elements map[K]V
25 err := json.Unmarshal(data, &elements)
26 if err != nil {
27 return err
28 }
29
30 m.Clear()
31 for k, v := range elements {
32 m.Put(k, v)
33 }
34
35 return nil
36 }
37
38 // UnmarshalJSON @implements json.Unmarshaler
39 func (m *Map[K, V]) UnmarshalJSON(bytes []byte) error {
40 return m.FromJSON(bytes)
41 }
42
43 // MarshalJSON @implements json.Marshaler
44 func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
45 return m.ToJSON()
46 }