forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_util.go
245 lines (220 loc) · 5.09 KB
/
json_util.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
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
package ravendb
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
)
// Note: com.fasterxml.jackson.databind.JsonNode represents decoded JSON value i.e. interface{}
// Note: Java's com.fasterxml.jackson.databind.node.JsonNodeType represents type of JSON value, which in Go
// is the same as value itself, and therefore is interface{}
// Note: Java's com.fasterxml.jackson.databind.node.ObjectNode is map[string]interface{}
// It represents parsed json document
// Note: Java's com.fasterxml.jackson.databind.TreeNode represents a decoded JSON value that combines
// value and type. In Go it's interface{}
// Note: Java's com.fasterxml.jackson.databind.node.ArrayNode represents array of JSON objects
// It's []map[string]interface{} in Go
// we should use jsonMarshal instead of jsonMarshal so that it's easy
// to change json marshalling in all code base (e.g. to use a faster
// json library or ensure that values are marshaled correctly)
func jsonMarshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func jsonUnmarshal(d []byte, v interface{}) error {
return json.Unmarshal(d, v)
}
func jsonGetAsTextPointer(doc map[string]interface{}, key string) *string {
v, ok := doc[key]
if !ok {
return nil
}
// TODO: only allow *string ?
if s, ok := v.(*string); ok {
return s
}
s := v.(string)
return &s
}
func jsonGetAsString(doc map[string]interface{}, key string) (string, bool) {
return jsonGetAsText(doc, key)
}
func jsonGetAsText(doc map[string]interface{}, key string) (string, bool) {
v, ok := doc[key]
if !ok {
return "", false
}
s, ok := v.(string)
if !ok {
return "", false
}
return s, true
}
func jsonGetAsInt(doc map[string]interface{}, key string) (int, bool) {
v, ok := doc[key]
if !ok {
return 0, false
}
f, ok := v.(float64)
if ok {
return int(f), true
}
s, ok := v.(string)
if !ok {
return 0, false
}
n, err := strconv.Atoi(s)
if err != nil {
return 0, false
}
return n, true
}
func jsonGetAsInt64(doc map[string]interface{}, key string) (int64, bool) {
v, ok := doc[key]
if !ok {
return 0, false
}
f, ok := v.(float64)
if ok {
return int64(f), true
}
s, ok := v.(string)
if !ok {
return 0, false
}
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, false
}
return n, true
}
func jsonGetAsBool(doc map[string]interface{}, key string) (bool, bool) {
v, ok := doc[key]
if !ok {
return false, false
}
b, ok := v.(bool)
if ok {
return b, true
}
s, ok := v.(string)
if !ok {
return false, false
}
if strings.EqualFold(s, "true") {
return true, true
}
if strings.EqualFold(s, "false") {
return false, true
}
return false, false
}
// converts a struct to JSON representations as map of string to value
// TODO: could be faster
func structToJSONMap(v interface{}) map[string]interface{} {
d, err := jsonMarshal(v)
must(err)
var res map[string]interface{}
err = jsonUnmarshal(d, &res)
must(err)
return res
}
// given a json in the form of map[string]interface{}, de-serialize it to a struct
// TODO: could be faster
func structFromJSONMap(js map[string]interface{}, v interface{}) error {
d, err := jsonMarshal(js)
if err != nil {
return err
}
return jsonUnmarshal(d, v)
}
// matches a Java naming from EnityMapper
func valueToTree(v interface{}) map[string]interface{} {
return structToJSONMap(v)
}
// TODO: remove
// copyJSONMap makes a deep copy of map[string]interface{}
// TODO: possibly not the fastest way to do it
/*
func copyJSONMap(v map[string]interface{}) map[string]interface{} {
d, err := jsonMarshal(v)
must(err)
var res map[string]interface{}
err = jsonUnmarshal(d, &res)
must(err)
return res
}
*/
// jsonDecodeFirst decode first JSON object from d
// This is like jsonUnmarshal() but allows for d
// to contain multiple JSON objects
// This is for compatibility with Java's ObjectMapper.readTree()
func jsonUnmarshalFirst(d []byte, v interface{}) error {
r := bytes.NewReader(d)
dec := json.NewDecoder(r)
err := dec.Decode(v)
if err != nil {
dbg("jsonDecodeFirst: dec.Decode() of type %T failed with %s. JSON:\n%s\n\n", v, err, string(d))
}
return err
}
func isUnprintable(c byte) bool {
if c < 32 {
// 9 - tab, 10 - LF, 13 - CR
if c == 9 || c == 10 || c == 13 {
return false
}
return true
}
return c >= 127
}
func isBinaryData(d []byte) bool {
for _, b := range d {
if isUnprintable(b) {
return true
}
}
return false
}
func asHex(d []byte) ([]byte, bool) {
if !isBinaryData(d) {
return d, false
}
// convert unprintable characters to hex
var res []byte
for i, c := range d {
if i > 2048 {
break
}
if isUnprintable(c) {
s := fmt.Sprintf("x%02x ", c)
res = append(res, s...)
} else {
res = append(res, c)
}
}
return res, true
}
// if d is a valid json, pretty-print it
// only used for debugging
func maybePrettyPrintJSON(d []byte) []byte {
if d2, ok := asHex(d); ok {
return d2
}
var m map[string]interface{}
err := json.Unmarshal(d, &m)
if err != nil {
return d
}
d2, err := json.MarshalIndent(m, "", " ")
if err != nil {
return d
}
return d2
}
func stringPtrToString(s *string) string {
if s == nil {
return ""
}
return *s
}