Repository navigation
Expand file tree
/
Copy pathjson.go
More file actions
280 lines (267 loc) · 7.6 KB
/
Copy pathjson.go
File metadata and controls
280 lines (267 loc) · 7.6 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Package fxmdjson provides independent native FastJSON views of FXMacroData JSON.
// It performs no network I/O and accepts no credentials or client configuration.
package fxmdjson
import (
"bytes"
"encoding/json"
"errors"
"github.com/valyala/fastjson"
"io"
"strconv"
"strings"
"unicode/utf8"
)
const (
MaxResponseBytes = 4 << 20
MaxViewBytes = 8 << 20
MaxJSONDepth = 64
MaxJSONValues = 50000
)
// Document owns the complete original response and an immutable parsed view.
// Native arithmetic results never overwrite observations or metadata.
type Document struct {
raw json.RawMessage
value any
}
// Parse serializes a customer-owned SDK model. ParseJSON preserves original HTTP
// bytes and unknown fields that a typed model might otherwise omit.
func Parse(data any) (*Document, error) {
raw, err := json.Marshal(data)
if err != nil {
return nil, errors.New("response is not JSON-serializable")
}
return ParseJSON(raw)
}
func ParseJSON(raw []byte) (*Document, error) {
if len(raw) == 0 || len(raw) > MaxResponseBytes || !utf8.Valid(raw) {
return nil, errors.New("response size or encoding is invalid")
}
owned := append(json.RawMessage(nil), raw...)
decoder := json.NewDecoder(bytes.NewReader(owned))
decoder.UseNumber()
remaining := MaxJSONValues
value, err := decode(decoder, 0, &remaining)
if err != nil {
return nil, err
}
if _, err := decoder.Token(); err != io.EOF {
return nil, errors.New("exactly one JSON value is required")
}
return &Document{raw: owned, value: value}, nil
}
// Raw returns an independent copy of the unmodified full response.
func (d *Document) Raw() json.RawMessage {
if d == nil {
return nil
}
return append(json.RawMessage(nil), d.raw...)
}
// Exists distinguishes missing fields from explicit JSON null.
func (d *Document) Exists(pointer string) (bool, error) {
if d == nil {
return false, errors.New("a document is required")
}
_, found, err := lookup(d.value, pointer)
return found, err
}
func (d *Document) selected(pointer string) (any, error) {
if d == nil {
return nil, errors.New("a document is required")
}
value, found, err := lookup(d.value, pointer)
if err != nil {
return nil, err
}
if !found {
return nil, errors.New("JSON field is unavailable")
}
return value, nil
}
// JSONAt returns a compact subtree view, retaining numeric spelling. Raw retains
// original whitespace, escaping, member order and the complete surrounding data.
func (d *Document) JSONAt(pointer string) (json.RawMessage, error) {
value, err := d.selected(pointer)
if err != nil {
return nil, err
}
raw, err := json.Marshal(value)
if err != nil || len(raw) > MaxViewBytes {
return nil, errors.New("JSON view exceeds bounds or cannot be encoded")
}
return raw, nil
}
// NumberTextAt returns an original JSON number lexeme without float conversion.
func (d *Document) NumberTextAt(pointer string) (string, error) {
value, err := d.selected(pointer)
if err != nil {
return "", err
}
number, ok := value.(json.Number)
if !ok {
return "", errors.New("JSON value is not a number")
}
return number.String(), nil
}
func lookup(root any, pointer string) (any, bool, error) {
if len(pointer) > 4096 || !utf8.ValidString(pointer) || strings.Count(pointer, "/") > MaxJSONDepth+1 {
return nil, false, errors.New("JSON Pointer exceeds supported bounds")
}
if pointer == "" {
return root, true, nil
}
if !strings.HasPrefix(pointer, "/") {
return nil, false, errors.New("invalid JSON Pointer")
}
current := root
for _, part := range strings.Split(pointer[1:], "/") {
var name strings.Builder
for i := 0; i < len(part); i++ {
if part[i] != '~' {
name.WriteByte(part[i])
continue
}
i++
if i >= len(part) {
return nil, false, errors.New("invalid JSON Pointer escape")
}
switch part[i] {
case '0':
name.WriteByte('~')
case '1':
name.WriteByte('/')
default:
return nil, false, errors.New("invalid JSON Pointer escape")
}
}
key := name.String()
switch value := current.(type) {
case map[string]any:
var found bool
current, found = value[key]
if !found {
return nil, false, nil
}
case []any:
if key == "" || (len(key) > 1 && key[0] == '0') {
return nil, false, errors.New("invalid JSON array index")
}
for _, c := range key {
if c < '0' || c > '9' {
return nil, false, errors.New("invalid JSON array index")
}
}
index, err := strconv.ParseUint(key, 10, 64)
if err != nil || index >= uint64(len(value)) {
return nil, false, nil
}
current = value[int(index)]
default:
return nil, false, nil
}
}
return current, true, nil
}
func decode(dec *json.Decoder, depth int, remaining *int) (any, error) {
if depth > MaxJSONDepth {
return nil, errors.New("nesting limit exceeded")
}
*remaining--
if *remaining < 0 {
return nil, errors.New("value limit exceeded")
}
token, err := dec.Token()
if err != nil {
return nil, errors.New("malformed JSON")
}
switch value := token.(type) {
case json.Delim:
switch value {
case '{':
object := map[string]any{}
for dec.More() {
key, err := dec.Token()
if err != nil {
return nil, errors.New("malformed object")
}
name, ok := key.(string)
if !ok {
return nil, errors.New("non-string object key")
}
if _, exists := object[name]; exists {
return nil, errors.New("duplicate object key")
}
item, err := decode(dec, depth+1, remaining)
if err != nil {
return nil, err
}
object[name] = item
}
close, err := dec.Token()
if err != nil || close != json.Delim('}') {
return nil, errors.New("malformed object")
}
return object, nil
case '[':
array := []any{}
for dec.More() {
item, err := decode(dec, depth+1, remaining)
if err != nil {
return nil, err
}
array = append(array, item)
}
close, err := dec.Token()
if err != nil || close != json.Delim(']') {
return nil, errors.New("malformed array")
}
return array, nil
}
return nil, errors.New("unexpected delimiter")
case json.Number:
return value, nil
case string, bool, nil:
return token, nil
}
return nil, errors.New("unsupported JSON token")
}
// Native returns an independent native view of the complete response.
// No parser instance is reused, so later calls cannot invalidate earlier views.
func (d *Document) Native() (*fastjson.Value, error) { return d.NativeAt("") }
// NativeAt selects a complete JSON value by RFC 6901 pointer and constructs an
// independently owned native tree. Native edits never affect Document or Raw.
// Numeric lexemes are retained; native numeric conversions are the caller's choice.
func (d *Document) NativeAt(pointer string) (*fastjson.Value, error) {
raw, err := d.JSONAt(pointer)
if err != nil {
return nil, err
}
value, err := fastjson.Parse(string(raw))
if err != nil {
return nil, errors.New("selected JSON value cannot be represented by the native parser")
}
return ownedNative(value), nil
}
// Detach every node, including primitive nodes that a native parser may intern.
// Container backing belongs to this fresh parse and is not exposed beforehand.
func ownedNative(value *fastjson.Value) *fastjson.Value {
copy := *value
switch copy.Type() {
case fastjson.TypeObject:
object, _ := copy.Object()
type member struct {
key string
value *fastjson.Value
}
members := make([]member, 0)
object.Visit(func(key []byte, v *fastjson.Value) { members = append(members, member{string(key), v}) })
for _, m := range members {
copy.Set(m.key, ownedNative(m.value))
}
case fastjson.TypeArray:
values, _ := copy.Array()
for i, v := range values {
values[i] = ownedNative(v)
}
}
return ©
}