-
Notifications
You must be signed in to change notification settings - Fork 171
/
json.go
155 lines (117 loc) · 4.03 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
package vm
import (
"encoding/json"
"strconv"
"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
)
type jsonObj map[string]interface{}
// Class methods --------------------------------------------------------
func builtinJSONClassMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
Name: "parse",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 1 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 1 argument. got=%v", strconv.Itoa(len(args)))
}
j, ok := args[0].(*StringObject)
if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, args[0].Class().Name)
}
var obj jsonObj
var objs []jsonObj
jsonString := j.value
err := json.Unmarshal([]byte(jsonString), &obj)
if err != nil {
err = json.Unmarshal([]byte(jsonString), &objs)
if err != nil {
return t.vm.InitErrorObject(errors.InternalError, sourceLine, "Can't parse string %s as json: %s", jsonString, err.Error())
}
var objects []Object
for _, obj := range objs {
objects = append(objects, t.vm.convertJSONToHashObj(obj))
}
return t.vm.InitArrayObject(objects)
}
return t.vm.convertJSONToHashObj(obj)
},
},
{
Name: "validate",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 1 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 1 argument. got=%v", strconv.Itoa(len(args)))
}
j, ok := args[0].(*StringObject)
if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, args[0].Class().Name)
}
var obj jsonObj
var objs []jsonObj
jsonString := j.value
err := json.Unmarshal([]byte(jsonString), &obj)
if err != nil {
err = json.Unmarshal([]byte(jsonString), &objs)
if err != nil {
return FALSE
}
return TRUE
}
return TRUE
},
},
}
}
// Instance methods -----------------------------------------------------
func builtinJSONInstanceMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{}
}
// Internal functions ===================================================
// Functions for initialization -----------------------------------------
func initJSONClass(vm *VM) {
class := vm.initializeClass("JSON")
class.setBuiltinMethods(builtinJSONClassMethods(), true)
class.setBuiltinMethods(builtinJSONInstanceMethods(), false)
vm.objectClass.setClassConstant(class)
}
// Polymorphic helper functions -----------------------------------------
func (v *VM) convertJSONToHashObj(j jsonObj) Object {
objectMap := map[string]Object{}
for key, jsonValue := range j {
switch jsonValue := jsonValue.(type) {
// Array of json objects
case []map[string]interface{}:
objs := []Object{}
for _, value := range jsonValue {
objs = append(objs, v.convertJSONToHashObj(value))
}
objectMap[key] = v.InitArrayObject(objs)
case []interface{}:
objs := []Object{}
for _, elem := range jsonValue {
switch e := elem.(type) {
case map[string]interface{}:
objs = append(objs, v.convertJSONToHashObj(e))
default:
objs = append(objs, v.InitObjectFromGoType(e))
}
}
objectMap[key] = v.InitArrayObject(objs)
// Single json object
case map[string]interface{}:
objectMap[key] = v.convertJSONToHashObj(jsonValue)
case float64:
// TODO: Find a better way to distinguish between Float & Integer because default GO JSON package
// TODO: support only for parsing float out regardless of integer or float type data of JSON value
if jsonValue == float64(int(jsonValue)) {
objectMap[key] = v.InitIntegerObject(int(jsonValue))
} else {
objectMap[key] = v.initFloatObject(jsonValue)
}
default:
objectMap[key] = v.InitObjectFromGoType(jsonValue)
}
}
return v.InitHashObject(objectMap)
}