-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
198 lines (191 loc) · 4.75 KB
/
json.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package builtin_libs
import (
"bytes"
"encoding/json"
"math"
"reflect"
"strings"
. "github.com/zgg-lang/zgg-go/runtime"
"github.com/oliveagle/jsonpath"
)
func jsonMarshal(v interface{}) ([]byte, error) {
var ob bytes.Buffer
enc := json.NewEncoder(&ob)
enc.SetEscapeHTML(false)
if err := enc.Encode(v); err != nil {
return nil, err
}
return ob.Bytes(), nil
}
func jsonMarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
var ob bytes.Buffer
enc := json.NewEncoder(&ob)
enc.SetEscapeHTML(false)
enc.SetIndent(prefix, indent)
if err := enc.Encode(v); err != nil {
return nil, err
}
return ob.Bytes(), nil
}
func libJson(*Context) ValueObject {
lib := NewObject()
lib.SetMember("encode", NewNativeFunction("json.encode", func(c *Context, this Value, args []Value) Value {
var bs []byte
var err error
switch len(args) {
case 1:
bs, err = jsonMarshal(args[0].ToGoValue())
case 2:
var indent string
switch iv := args[1].(type) {
case ValueInt:
indent = strings.Repeat(" ", iv.AsInt())
case ValueStr:
indent = iv.Value()
default:
c.OnRuntimeError("json.encode(value, indent): indent must be a string or an integer, not %s", args[1].Type().Name)
return nil
}
bs, err = jsonMarshalIndent(args[0].ToGoValue(), "", indent)
case 3:
bs, err = jsonMarshalIndent(args[0].ToGoValue(), args[1].ToString(c), args[2].ToString(c))
default:
c.OnRuntimeError("json.encode: requires 1 to 3 argument(s)")
return nil
}
if err != nil {
c.OnRuntimeError("json.encode: " + err.Error())
return nil
}
return NewStr(string(bs))
}), nil)
lib.SetMember("decode", NewNativeFunction("json.decode", func(c *Context, this Value, args []Value) Value {
if len(args) != 1 {
c.OnRuntimeError("json.decode: requires 1 argument")
return nil
}
var bs []byte
switch arg := args[0].(type) {
case ValueStr:
bs = []byte(arg.Value())
case ValueBytes:
bs = arg.Value()
default:
c.OnRuntimeError("json.decode: argument must be a string or a bytes")
return nil
}
var j interface{}
if err := json.Unmarshal(bs, &j); err != nil {
c.OnRuntimeError("json.decode: " + err.Error())
return nil
}
return jsonToValue(j, c)
}), nil)
lib.SetMember("format", NewNativeFunction("json.format", func(c *Context, this Value, args []Value) Value {
var (
jsonStr Value
indentSize ValueInt
indentStr ValueStr
indentType int
)
EnsureFuncParams(c, "json.format", args,
ArgRuleRequired{"value", TypeAny, &jsonStr},
ArgRuleOneOf{
"indent",
[]ValueType{TypeInt, TypeStr},
[]interface{}{&indentSize, &indentStr},
&indentType, &indentSize, NewInt(0)},
)
var bs []byte
switch s := jsonStr.(type) {
case ValueBytes:
bs = s.Value()
default:
bs = []byte(s.ToString(c))
}
var j interface{}
if err := json.Unmarshal(bs, &j); err != nil {
c.OnRuntimeError("json.format: decode failed %s", err)
}
var (
outs []byte
err error
)
switch indentType {
case 0:
outs, err = jsonMarshalIndent(j, "", strings.Repeat(" ", indentSize.AsInt()))
case 1:
outs, err = jsonMarshalIndent(j, "", indentStr.Value())
default:
outs, err = jsonMarshal(j)
}
if err != nil {
c.OnRuntimeError("json.format: marshal failed %s", err)
}
return NewStr(string(outs))
}), nil)
lib.SetMember("find", NewNativeFunction("json.find", func(c *Context, this Value, args []Value) Value {
var (
path ValueStr
value Value
data interface{}
err error
)
EnsureFuncParams(c, "json.find", args,
ArgRuleRequired{"jsonpath", TypeStr, &path},
ArgRuleRequired{"object", TypeAny, &value},
)
switch v := value.(type) {
case ValueStr:
err = json.Unmarshal([]byte(v.Value()), &data)
case ValueBytes:
err = json.Unmarshal(v.Value(), &data)
default:
data = v.ToGoValue()
}
if err != nil {
c.OnRuntimeError("json.find: parse value error %s", err)
return nil
}
res, err := jsonpath.JsonPathLookup(data, path.Value())
if err != nil {
c.OnRuntimeError("json.find: jsonpath %s lookup error: %s", path.Value(), err)
return nil
}
return FromGoValue(reflect.ValueOf(res), c)
}), nil)
return lib
}
func jsonToValue(src interface{}, c *Context) Value {
if src == nil {
return Nil()
}
switch srcVal := src.(type) {
case float64:
if math.Floor(srcVal) == srcVal {
return NewInt(int64(srcVal))
}
return NewFloat(srcVal)
case bool:
return NewBool(srcVal)
case string:
return NewStr(srcVal)
case []interface{}:
{
rv := NewArray(len(srcVal))
for _, elemVal := range srcVal {
rv.PushBack(jsonToValue(elemVal, c))
}
return rv
}
case map[string]interface{}:
{
rv := NewObject()
for k, elemVal := range srcVal {
rv.SetMember(k, jsonToValue(elemVal, c), c)
}
return rv
}
}
return Undefined()
}