-
Notifications
You must be signed in to change notification settings - Fork 19
/
json.go
150 lines (123 loc) · 3.47 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
package types
import (
"encoding/json"
"fmt"
"github.com/nyaruka/goflow/utils"
"github.com/buger/jsonparser"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
// XJSON is the base type for XJSONObject and XJSONArray
type XJSON []byte
func (x XJSON) ToXJSON(env utils.Environment) XText { return NewXText(string(x)) }
func (x XJSON) Reduce(env utils.Environment) XPrimitive { return x.ToXJSON(env) }
// String converts this type to native string
func (x XJSON) String() string {
return string(x)
}
func (x XJSON) MarshalJSON() ([]byte, error) {
return []byte(x), nil
}
type XJSONObject struct {
XJSON
}
func NewXJSONObject(data []byte) XJSONObject {
return XJSONObject{XJSON: data}
}
// Describe returns a representation of this type for error messages
func (x XJSONObject) Describe() string { return "json object" }
func (x XJSONObject) Length() int {
length := 0
jsonparser.ObjectEach(x.XJSON, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
length++
return nil
})
return length
}
func (x XJSONObject) Resolve(env utils.Environment, key string) XValue {
val, valType, _, err := jsonparser.Get(x.XJSON, key)
if err != nil {
return NewXResolveError(x, key)
}
return jsonTypeToXValue(val, valType)
}
var _ XValue = XJSONObject{}
var _ XLengthable = XJSONObject{}
var _ XResolvable = XJSONObject{}
var _ json.Marshaler = XJSONObject{}
type XJSONArray struct {
XJSON
}
func NewXJSONArray(data []byte) XJSONArray {
return XJSONArray{XJSON: data}
}
// Describe returns a representation of this type for error messages
func (x XJSONArray) Describe() string { return "json array" }
func (x XJSONArray) Length() int {
length := 0
jsonparser.ArrayEach(x.XJSON, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
length++
})
return length
}
func (x XJSONArray) Index(index int) XValue {
val, valType, _, err := jsonparser.Get(x.XJSON, fmt.Sprintf("[%d]", index))
if err != nil {
return NewXError(err)
}
return jsonTypeToXValue(val, valType)
}
var _ XValue = XJSONArray{}
var _ XIndexable = XJSONArray{}
var _ json.Marshaler = XJSONArray{}
func JSONToXValue(data []byte) XValue {
val, valType, _, err := jsonparser.Get(data)
if err != nil {
return NewXError(err)
}
return jsonTypeToXValue(val, valType)
}
func jsonTypeToXValue(data []byte, valType jsonparser.ValueType) XValue {
switch valType {
case jsonparser.Null:
return nil
case jsonparser.String:
strVal, err := jsonparser.ParseString(data)
if err == nil {
return NewXText(strVal)
}
case jsonparser.Number:
decimalVal, err := decimal.NewFromString(string(data))
if err == nil {
return NewXNumber(decimalVal)
}
case jsonparser.Boolean:
boolVal, err := jsonparser.ParseBoolean(data)
if err == nil {
return NewXBoolean(boolVal)
}
case jsonparser.Array:
return NewXJSONArray(data)
case jsonparser.Object:
return NewXJSONObject(data)
}
return NewXError(errors.Errorf("unknown JSON parsing error"))
}
// ToXJSON converts the given value to a JSON string
func ToXJSON(env utils.Environment, x XValue) (XText, XError) {
if utils.IsNil(x) {
return NewXText(`null`), nil
}
if IsXError(x) {
return XTextEmpty, x.(XError)
}
return x.ToXJSON(env), nil
}
// MustMarshalToXText calls json.Marshal in the given value and panics in the case of an error
func MustMarshalToXText(x interface{}) XText {
j, err := utils.JSONMarshal(x)
if err != nil {
panic(fmt.Sprintf("unable to marshal %s to JSON", x))
}
return NewXText(string(j))
}