-
Notifications
You must be signed in to change notification settings - Fork 0
/
njson_encode.go
180 lines (155 loc) · 4.21 KB
/
njson_encode.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package nbtreader
import (
"bytes"
"fmt"
"reflect"
"slices"
)
const startDetectingCyclesAfter = 1000
// Marshaler is the interface implemented by types that can marshal themselves into valid
// NJSON.
type Marshaler interface {
MarshalNJSON() ([]byte, error)
}
func MarshalNJSON(v any) ([]byte, error) {
if v, ok := v.(Marshaler); ok {
return v.MarshalNJSON()
}
e := &njsonEncoderState{ptrSeen: map[any]struct{}{}}
err := e.marshal(v)
buf := slices.Clone(e.Bytes())
return buf, err
}
// An UnsupportedValueError is returned by Marshal when attempting to encode an
// unsupported value.
type UnsupportedValueError struct {
Str string
}
// Error implements [[error]]
func (e *UnsupportedValueError) Error() string {
return "njson: unsupported value: " + e.Str
}
type njsonEncoderState struct {
bytes.Buffer
ptrLevel int
ptrSeen map[any]struct{}
}
// njsonError is an error wrapper type for internal use only.
// Panics with errors are wrapped in njsonError so that the top-level recover
// can distinguish intentional panics from this package.
type njsonError struct{ error }
// error aborts the encoding by panicking with err wrapped in njsonError.
func (e *njsonEncoderState) error(err error) {
panic(njsonError{err})
}
func (e *njsonEncoderState) marshal(v any) (err error) {
defer func() {
if r := recover(); r != nil {
if je, ok := r.(njsonError); ok {
err = je.error
} else {
panic(r)
}
}
}()
e.valueEncoder(reflect.ValueOf(v))
return nil
}
func (e *njsonEncoderState) valueEncoder(v reflect.Value) {
switch v.Type() {
case reflect.TypeOf(Byte(0)), reflect.TypeOf(Short(0)), reflect.TypeOf(Int(0)), reflect.TypeOf(Long(0)):
e.intEncoder(v)
return
case reflect.TypeOf(Float(0)), reflect.TypeOf(Double(0)):
e.floatEncoder(v)
return
case reflect.TypeOf(String("")):
e.stringEncoder(v)
return
case reflect.TypeOf(List{}):
l := v.Interface().(List)
if anno := l.TagType.Annotation(); anno != NoAnnotation {
// allocation friendly prepending of type annotation
l.Elements = append(l.Elements, nil)
copy(l.Elements[1:], l.Elements)
l.Elements[0] = String(anno.String())
}
v = reflect.ValueOf(l.Elements)
fallthrough
case reflect.TypeOf(ByteArray{}), reflect.TypeOf(IntArray{}), reflect.TypeOf(LongArray{}):
e.arrayEncoder(v)
return
}
switch kind := v.Kind(); kind {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
e.intEncoder(v)
case reflect.Float32, reflect.Float64:
e.floatEncoder(v)
case reflect.String:
e.stringEncoder(v)
case reflect.Slice:
e.arrayEncoder(v)
case reflect.Map:
e.objectEncoder(v)
case reflect.Pointer, reflect.Interface:
e.valueEncoder(e.realValue(v))
default:
e.error(&UnsupportedValueError{fmt.Sprintf("encoding '%s' isn't supported yet: %s", kind, v.Type())})
}
}
func (e *njsonEncoderState) intEncoder(v reflect.Value) {
e.WriteString(fmt.Sprintf("%d", v.Int()))
}
func (e *njsonEncoderState) floatEncoder(v reflect.Value) {
e.WriteString(fmt.Sprintf("%f", v.Float()))
}
func (e *njsonEncoderState) stringEncoder(v reflect.Value) {
e.WriteString("\"" + e.realValue(v).String() + "\"")
}
func (e *njsonEncoderState) arrayEncoder(v reflect.Value) {
e.WriteByte('[')
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
e.valueEncoder(val)
if i < v.Len()-1 {
e.WriteByte(',')
}
}
e.WriteByte(']')
}
func (e *njsonEncoderState) objectEncoder(v reflect.Value) {
if v.Type().Key().Kind() != reflect.String {
e.error(&UnsupportedValueError{fmt.Sprintf("invalid map key type '%s'", v.Type().Key())})
}
e.WriteByte('{')
iter := v.MapRange()
for i := 0; iter.Next(); i++ {
key, val := e.realValue(iter.Key()), e.realValue(iter.Value())
e.WriteByte('"')
e.WriteString(key.String())
e.WriteString(AnnotationOf(val).String())
e.WriteByte('"')
e.WriteByte(':')
e.valueEncoder(val)
if i < v.Len()-1 {
e.WriteByte(',')
}
}
e.WriteByte('}')
}
func (e *njsonEncoderState) realValue(v reflect.Value) reflect.Value {
switch v.Kind() {
case reflect.Interface, reflect.Pointer:
default:
return v
}
if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
ptr := v.Interface()
if _, ok := e.ptrSeen[ptr]; ok {
}
e.ptrSeen[ptr] = struct{}{}
}
v = e.realValue(v.Elem())
e.ptrLevel--
return v
}