-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_value.go
More file actions
39 lines (33 loc) · 852 Bytes
/
Copy pathmap_value.go
File metadata and controls
39 lines (33 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package orderedmap
import (
"encoding/json"
"fmt"
)
// Entry represents a map value entry with its insertion sequence.
type Entry struct {
index uint64
Value any
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *Entry) UnmarshalJSON(b []byte) error {
e.index = nextSequence()
if err := json.Unmarshal(b, &e.Value); err != nil {
return fmt.Errorf("unmarshalling entry: %w", err)
}
if _, ok := e.Value.(map[string]any); ok {
var m Map
if err := json.Unmarshal(b, &m); err != nil {
return fmt.Errorf("unmarshalling json entry into map: %w", err)
}
e.Value = &m
}
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (e *Entry) MarshalJSON() ([]byte, error) {
b, err := json.Marshal(e.Value)
if err != nil {
return nil, fmt.Errorf("marshalling entry: %w", err)
}
return b, nil
}