-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.go
149 lines (141 loc) · 3.55 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
package builtin
import (
"encoding/json"
"fmt"
"github.com/tzmfreedom/land/ast"
)
func init() {
staticMethods := ast.NewMethodMap()
staticMethods.Set(
"serialize",
[]*ast.Method{
ast.CreateMethod(
"serialize",
StringType,
[]*ast.Parameter{objectTypeParameter},
func(this *ast.Object, params []*ast.Object, extra map[string]interface{}) interface{} {
mapValue := serializeJson(params[0], false)
value, err := json.Marshal(mapValue)
if err != nil {
panic(err)
}
return NewString(string(value))
},
),
ast.CreateMethod(
"serialize",
StringType,
[]*ast.Parameter{
objectTypeParameter,
booleanTypeParameter,
},
func(this *ast.Object, params []*ast.Object, extra map[string]interface{}) interface{} {
mapValue := serializeJson(params[0], params[1].BoolValue())
value, err := json.Marshal(mapValue)
if err != nil {
panic(err)
}
return NewString(string(value))
},
),
},
)
staticMethods.Set(
"deserializeUntyped",
[]*ast.Method{
ast.CreateMethod(
"deserializeUntyped",
ObjectType,
[]*ast.Parameter{stringTypeParameter},
func(this *ast.Object, params []*ast.Object, extra map[string]interface{}) interface{} {
srcMap := map[string]interface{}{}
err := json.Unmarshal([]byte(params[0].StringValue()), &srcMap)
if err != nil {
panic(err)
}
return deserializeJson(srcMap)
},
),
},
)
classType := ast.CreateClass(
"JSON",
[]*ast.Method{},
nil,
staticMethods,
)
primitiveClassMap.Set("JSON", classType)
}
func serializeJson(object *ast.Object, suppressApexObjectNull bool) interface{} {
classType := object.ClassType
switch classType {
case StringType:
return object.StringValue()
case IntegerType:
return object.IntegerValue()
case DoubleType:
return object.DoubleValue()
case BooleanType:
return object.BoolValue()
case NullType:
return nil
}
if classType.Name == "List" {
records := object.Extra["records"].([]*ast.Object)
values := make([]interface{}, len(records))
for i, record := range records {
values[i] = serializeJson(record, suppressApexObjectNull)
}
return values
}
if classType.Name == "Map" {
ret := map[string]interface{}{}
values := object.Extra["values"].(map[string]*ast.Object)
for field, value := range values {
ret[field] = serializeJson(value, suppressApexObjectNull)
}
return ret
}
ret := map[string]interface{}{}
for field, value := range object.InstanceFields.All() {
if value == Null && suppressApexObjectNull {
continue
}
ret[field] = serializeJson(value, suppressApexObjectNull)
}
return ret
}
func deserializeJson(value interface{}) *ast.Object {
if value == nil {
return Null
}
switch typedValue := value.(type) {
case string:
return NewString(typedValue)
case int:
return NewInteger(typedValue)
case float64:
return NewDouble(typedValue)
case bool:
return NewBoolean(typedValue)
case []interface{}:
classType := CreateListType(ObjectType)
newObj := ast.CreateObject(classType)
records := make([]*ast.Object, len(typedValue))
for i, record := range typedValue {
records[i] = deserializeJson(record)
}
newObj.Extra["records"] = records
return newObj
case map[string]interface{}:
classType := CreateMapType(StringType, ObjectType)
newObj := ast.CreateObject(classType)
values := map[string]*ast.Object{}
for key, value := range typedValue {
values[key] = deserializeJson(value)
}
newObj.Extra["values"] = values
return newObj
}
panic(fmt.Sprintf("no expected type %v", value))
}