-
Notifications
You must be signed in to change notification settings - Fork 0
/
any.go
55 lines (49 loc) · 1.03 KB
/
any.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
package ojson
import (
"bytes"
"encoding/json"
)
// Any represents any Go value. When using it to decode a JSON value
// it ensures that embedded JSON objects are decoded as [Object].
type Any struct {
v any
}
// Value returns the value encapsulated in a.
func (a Any) Value() any {
return a.v
}
// MarshalJSON implements the [json.Marshaler] interface.
func (a Any) MarshalJSON() ([]byte, error) {
return json.Marshal(a.v)
}
// UnmarshalJSON implements the [json.Unmarshaler] interface.
func (a *Any) UnmarshalJSON(d []byte) error {
dec := json.NewDecoder(bytes.NewReader(d))
return a.unmarshalJSON(dec)
}
func (a *Any) unmarshalJSON(d *json.Decoder) error {
tok, err := d.Token()
if err != nil {
return err
}
switch v := tok.(type) {
case json.Delim:
switch v {
case '{':
o := NewObject()
if err := o.unmarshalJSON(d); err != nil {
return err
}
a.v = o
case '[':
var arr Array
if err := arr.unmarshalJSON(d); err != nil {
return err
}
a.v = []any(arr)
}
default:
a.v = v
}
return nil
}