-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyorderedmap.go
150 lines (115 loc) · 2.55 KB
/
anyorderedmap.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package orderedmapjson
import (
"bytes"
"encoding/json"
"fmt"
)
type AnyOrderedMap struct {
*orderedMap[any]
}
func NewAnyOrderedMap() *AnyOrderedMap {
return &AnyOrderedMap{
orderedMap: newOrderedMap[any](),
}
}
func NewAnyOrderedMapWithCapacity(capacity int) *AnyOrderedMap {
return &AnyOrderedMap{
orderedMap: newOrderedMapWithCapacity[any](capacity),
}
}
func (m *AnyOrderedMap) UnmarshalJSON(b []byte) error {
if m.orderedMap == nil {
m.orderedMap = newOrderedMap[any]()
}
decoder := json.NewDecoder(bytes.NewReader(b))
// Skip '{'
token, err := decoder.Token()
if err != nil {
return err
}
if token != json.Delim('{') {
return fmt.Errorf("expected '{' but got %v", token)
}
return unmarshalAnyOrderedMap(decoder, m)
}
func (m *AnyOrderedMap) Copy() *AnyOrderedMap {
return &AnyOrderedMap{
orderedMap: m.orderedMap.Copy(),
}
}
func unmarshalAnyOrderedMap(decoder *json.Decoder, m *AnyOrderedMap) error {
for {
token, err := decoder.Token()
if err != nil {
return err
}
// Reached end of map
if token == json.Delim('}') {
return nil
}
key, ok := token.(string)
if !ok {
return fmt.Errorf("unexpected key type: %T", token)
}
token, err = decoder.Token()
if err != nil {
return err
}
switch token {
case json.Delim('{'):
mm := NewAnyOrderedMap()
if err := unmarshalAnyOrderedMap(decoder, mm); err != nil {
return err
}
m.Set(key, mm)
case json.Delim('['):
values, err := unmarshalAnyOrderedMapArray(decoder)
if err != nil {
return err
}
m.Set(key, values)
default:
m.Set(key, token)
}
}
}
func unmarshalAnyOrderedMapArray(decoder *json.Decoder) ([]any, error) {
var values []any
for {
token, err := decoder.Token()
if err != nil {
return values, err
}
switch token {
// Reached end of array
case json.Delim(']'):
return values, nil
case json.Delim('{'):
mm := NewAnyOrderedMap()
if err := unmarshalAnyOrderedMap(decoder, mm); err != nil {
return nil, err
}
values = append(values, mm)
case json.Delim('['):
vv, err := unmarshalAnyOrderedMapArray(decoder)
if err != nil {
return values, err
}
values = append(values, vv)
default:
values = append(values, token)
}
}
}
func UnmarshalArrayWithAnyOrderedMap(b []byte) ([]any, error) {
decoder := json.NewDecoder(bytes.NewReader(b))
// Skip '['
token, err := decoder.Token()
if err != nil {
return nil, err
}
if token != json.Delim('[') {
return nil, fmt.Errorf("expected '[' but got %v", token)
}
return unmarshalAnyOrderedMapArray(decoder)
}