-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
503 lines (449 loc) · 12.3 KB
/
json.go
File metadata and controls
503 lines (449 loc) · 12.3 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/feather-lang/feather"
)
// SchemaNode represents a node in the JSON schema
type SchemaNode struct {
Type string // "string", "number", "bool", "object", "array"
Name string // field name
Children []*SchemaNode // for object: fields; for array: single element describing item type
}
// parseSchema parses the schema DSL into a tree of SchemaNodes
func parseSchema(schemaStr string) ([]*SchemaNode, error) {
tokens := tokenizeSchema(schemaStr)
nodes, _, err := parseSchemaTokens(tokens, 0)
return nodes, err
}
func tokenizeSchema(s string) []string {
var tokens []string
var current strings.Builder
for i := 0; i < len(s); i++ {
c := s[i]
switch c {
case '{':
if current.Len() > 0 {
tokens = append(tokens, current.String())
current.Reset()
}
tokens = append(tokens, "{")
case '}':
if current.Len() > 0 {
tokens = append(tokens, current.String())
current.Reset()
}
tokens = append(tokens, "}")
case ' ', '\t', '\n', '\r', ';':
if current.Len() > 0 {
tokens = append(tokens, current.String())
current.Reset()
}
default:
current.WriteByte(c)
}
}
if current.Len() > 0 {
tokens = append(tokens, current.String())
}
return tokens
}
func parseSchemaTokens(tokens []string, pos int) ([]*SchemaNode, int, error) {
var nodes []*SchemaNode
for pos < len(tokens) {
token := tokens[pos]
if token == "}" {
return nodes, pos, nil
}
if token == "{" {
pos++
continue
}
// Expect a type
switch token {
case "string", "number", "bool":
if pos+1 >= len(tokens) {
return nil, pos, fmt.Errorf("expected field name after %s", token)
}
node := &SchemaNode{Type: token, Name: tokens[pos+1]}
nodes = append(nodes, node)
pos += 2
case "object":
if pos+1 >= len(tokens) {
return nil, pos, fmt.Errorf("expected field name after object")
}
name := tokens[pos+1]
pos += 2
if pos >= len(tokens) || tokens[pos] != "{" {
return nil, pos, fmt.Errorf("expected { after object %s", name)
}
pos++ // skip {
children, newPos, err := parseSchemaTokens(tokens, pos)
if err != nil {
return nil, newPos, err
}
pos = newPos + 1 // skip }
node := &SchemaNode{Type: "object", Name: name, Children: children}
nodes = append(nodes, node)
case "array":
if pos+1 >= len(tokens) {
return nil, pos, fmt.Errorf("expected field name after array")
}
name := tokens[pos+1]
pos += 2
if pos >= len(tokens) {
return nil, pos, fmt.Errorf("expected element type after array %s", name)
}
elemType := tokens[pos]
pos++
var elemNode *SchemaNode
if elemType == "object" {
if pos >= len(tokens) || tokens[pos] != "{" {
return nil, pos, fmt.Errorf("expected { after array %s object", name)
}
pos++ // skip {
children, newPos, err := parseSchemaTokens(tokens, pos)
if err != nil {
return nil, newPos, err
}
pos = newPos + 1 // skip }
elemNode = &SchemaNode{Type: "object", Children: children}
} else {
elemNode = &SchemaNode{Type: elemType}
}
node := &SchemaNode{Type: "array", Name: name, Children: []*SchemaNode{elemNode}}
nodes = append(nodes, node)
default:
return nil, pos, fmt.Errorf("unexpected token: %s", token)
}
}
return nodes, pos, nil
}
// encodeWithSchema encodes a feather dict/list according to the schema
func encodeWithSchema(obj *feather.Obj, schema []*SchemaNode) (string, error) {
dict, err := feather.AsDict(obj)
if err != nil {
return "", fmt.Errorf("expected dict for object encoding: %v", err)
}
var parts []string
for _, node := range schema {
val, ok := dict.Items[node.Name]
if !ok {
continue // skip missing fields
}
encoded, err := encodeValue(val, node)
if err != nil {
return "", fmt.Errorf("field %s: %v", node.Name, err)
}
parts = append(parts, fmt.Sprintf("%q:%s", node.Name, encoded))
}
return "{" + strings.Join(parts, ",") + "}", nil
}
func encodeValue(val *feather.Obj, node *SchemaNode) (string, error) {
switch node.Type {
case "string":
// JSON-encode the string
b, _ := json.Marshal(val.String())
return string(b), nil
case "number":
s := val.String()
// Validate it's a number
if _, err := strconv.ParseFloat(s, 64); err != nil {
return "", fmt.Errorf("invalid number: %s", s)
}
return s, nil
case "bool":
s := val.String()
switch s {
case "1", "true":
return "true", nil
case "0", "false":
return "false", nil
default:
return "", fmt.Errorf("invalid bool: %s", s)
}
case "object":
return encodeWithSchema(val, node.Children)
case "array":
list, err := val.List()
if err != nil {
return "", fmt.Errorf("expected list for array: %v", err)
}
elemNode := node.Children[0]
var items []string
for i, item := range list {
encoded, err := encodeValue(item, elemNode)
if err != nil {
return "", fmt.Errorf("index %d: %v", i, err)
}
items = append(items, encoded)
}
return "[" + strings.Join(items, ",") + "]", nil
default:
return "", fmt.Errorf("unknown type: %s", node.Type)
}
}
// decodeWithSchema decodes JSON into a feather-compatible structure according to schema
func decodeWithSchema(jsonStr string, schema []*SchemaNode) (map[string]any, error) {
var raw map[string]any
if err := json.Unmarshal([]byte(jsonStr), &raw); err != nil {
return nil, err
}
return decodeObject(raw, schema)
}
func decodeObject(raw map[string]any, schema []*SchemaNode) (map[string]any, error) {
result := make(map[string]any)
for _, node := range schema {
val, ok := raw[node.Name]
if !ok {
continue
}
decoded, err := decodeValue(val, node)
if err != nil {
return nil, fmt.Errorf("field %s: %v", node.Name, err)
}
result[node.Name] = decoded
}
return result, nil
}
func decodeValue(val any, node *SchemaNode) (any, error) {
switch node.Type {
case "string":
if s, ok := val.(string); ok {
return s, nil
}
return fmt.Sprintf("%v", val), nil
case "number":
switch v := val.(type) {
case float64:
if v == float64(int64(v)) {
return fmt.Sprintf("%d", int64(v)), nil
}
return fmt.Sprintf("%g", v), nil
case string:
return v, nil
default:
return fmt.Sprintf("%v", v), nil
}
case "bool":
switch v := val.(type) {
case bool:
if v {
return "1", nil
}
return "0", nil
default:
return fmt.Sprintf("%v", v), nil
}
case "object":
obj, ok := val.(map[string]any)
if !ok {
return nil, fmt.Errorf("expected object, got %T", val)
}
return decodeObject(obj, node.Children)
case "array":
arr, ok := val.([]any)
if !ok {
return nil, fmt.Errorf("expected array, got %T", val)
}
elemNode := node.Children[0]
var items []any
for i, item := range arr {
decoded, err := decodeValue(item, elemNode)
if err != nil {
return nil, fmt.Errorf("index %d: %v", i, err)
}
items = append(items, decoded)
}
return items, nil
default:
return nil, fmt.Errorf("unknown type: %s", node.Type)
}
}
func registerJSONCommand(fi *feather.Interp, state *ServerState) {
jsonCmd := &Command{
Name: "json",
Help: "Encode or decode JSON with schema",
Usage: "json VALUE -as SCHEMA | json VALUE -from SCHEMA",
Subcommands: []*Command{
{Name: "-as", Help: "Encode TCL value to JSON using schema", Usage: "json VALUE -as SCHEMA"},
{Name: "-from", Help: "Decode JSON string to TCL value using schema", Usage: "json VALUE -from SCHEMA"},
},
}
registry.Register(jsonCmd)
// Use low-level registration to avoid TCL quoting of JSON output
fi.Internal().Register("json", func(i *feather.InternalInterp, cmd feather.FeatherObj, args []feather.FeatherObj) feather.FeatherResult {
if len(args) < 3 {
i.SetErrorString("wrong # args: should be \"json value -as schema\" or \"json value -from schema\"")
return feather.ResultError
}
flag := i.GetString(args[1])
schemaStr := i.GetString(args[2])
schema, err := parseSchema(schemaStr)
if err != nil {
i.SetErrorString(fmt.Sprintf("json: invalid schema: %v", err))
return feather.ResultError
}
switch flag {
case "-as":
// Encode TCL dict to JSON directly to buffer
dictVal, _, err := i.GetDict(args[0])
if err != nil {
i.SetErrorString(fmt.Sprintf("json: expected dict: %v", err))
return feather.ResultError
}
enc := newJSONEncoder(i)
if err := enc.encodeDict(dictVal, schema); err != nil {
i.SetErrorString(fmt.Sprintf("json: encode error: %v", err))
return feather.ResultError
}
i.SetResult(i.InternString(enc.String()))
return feather.ResultOK
case "-from":
// Decode JSON to TCL dict
jsonStr := i.GetString(args[0])
decoded, err := decodeWithSchema(jsonStr, schema)
if err != nil {
i.SetErrorString(fmt.Sprintf("json: decode error: %v", err))
return feather.ResultError
}
// Build dict result
dict := i.NewDict()
for k, v := range decoded {
dict = setDictValue(i, dict, k, v)
}
i.SetResult(dict)
return feather.ResultOK
default:
i.SetErrorString(fmt.Sprintf("json: unknown flag %q (use -as or -from)", flag))
return feather.ResultError
}
})
}
// jsonEncoder writes JSON directly to a buffer based on schema
type jsonEncoder struct {
i *feather.InternalInterp
buf *strings.Builder
}
func newJSONEncoder(i *feather.InternalInterp) *jsonEncoder {
return &jsonEncoder{i: i, buf: &strings.Builder{}}
}
func (e *jsonEncoder) String() string {
return e.buf.String()
}
func (e *jsonEncoder) encodeDict(dict map[string]feather.FeatherObj, schema []*SchemaNode) error {
e.buf.WriteByte('{')
first := true
for _, node := range schema {
val, ok := dict[node.Name]
if !ok {
continue
}
if !first {
e.buf.WriteByte(',')
}
first = false
e.buf.WriteByte('"')
e.buf.WriteString(node.Name)
e.buf.WriteString("\":")
if err := e.encodeValue(val, node); err != nil {
return fmt.Errorf("field %s: %v", node.Name, err)
}
}
e.buf.WriteByte('}')
return nil
}
func (e *jsonEncoder) encodeValue(val feather.FeatherObj, node *SchemaNode) error {
switch node.Type {
case "string":
s := e.getRawString(val)
b, _ := json.Marshal(s)
e.buf.Write(b)
return nil
case "number":
s := e.getRawString(val)
if _, err := strconv.ParseFloat(s, 64); err != nil {
return fmt.Errorf("invalid number: %s", s)
}
e.buf.WriteString(s)
return nil
case "bool":
s := e.getRawString(val)
switch s {
case "1", "true":
e.buf.WriteString("true")
case "0", "false":
e.buf.WriteString("false")
default:
return fmt.Errorf("invalid bool: %s", s)
}
return nil
case "object":
dictVal, _, err := e.i.GetDict(val)
if err != nil {
return fmt.Errorf("expected dict for object: %v", err)
}
return e.encodeDict(dictVal, node.Children)
case "array":
list, err := e.i.GetList(val)
if err != nil {
return fmt.Errorf("expected list for array: %v", err)
}
elemNode := node.Children[0]
e.buf.WriteByte('[')
for idx, item := range list {
if idx > 0 {
e.buf.WriteByte(',')
}
if err := e.encodeValue(item, elemNode); err != nil {
return fmt.Errorf("index %d: %v", idx, err)
}
}
e.buf.WriteByte(']')
return nil
default:
return fmt.Errorf("unknown type: %s", node.Type)
}
}
// getRawString extracts the raw string value, stripping Tcl braces if present
func (e *jsonEncoder) getRawString(val feather.FeatherObj) string {
s := e.i.GetString(val)
// Strip Tcl braces that wrap strings with spaces
if len(s) >= 2 && s[0] == '{' && s[len(s)-1] == '}' {
return s[1 : len(s)-1]
}
return s
}
func setDictValue(i *feather.InternalInterp, dict feather.FeatherObj, key string, val any) feather.FeatherObj {
switch v := val.(type) {
case string:
return i.DictSet(dict, key, i.InternString(v))
case map[string]any:
subDict := i.NewDict()
for k, sv := range v {
subDict = setDictValue(i, subDict, k, sv)
}
return i.DictSet(dict, key, subDict)
case []any:
list := i.NewList()
for _, item := range v {
switch iv := item.(type) {
case string:
list = i.ListAppend(list, i.InternString(iv))
case map[string]any:
subDict := i.NewDict()
for k, sv := range iv {
subDict = setDictValue(i, subDict, k, sv)
}
list = i.ListAppend(list, subDict)
default:
list = i.ListAppend(list, i.InternString(fmt.Sprintf("%v", iv)))
}
}
return i.DictSet(dict, key, list)
default:
return i.DictSet(dict, key, i.InternString(fmt.Sprintf("%v", v)))
}
}