forked from zubairhamed/betwixt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
160 lines (111 loc) · 2.87 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
package betwixt
import (
"encoding/json"
"log"
)
type YamlObjectDefinition struct {
Id int
Name string `json:"Name"`
Multiple bool
}
func ParseObjectDefinitionsJson(data []byte) []ObjectDefinition {
var n map[string]interface{}
err := json.Unmarshal(data, &n)
if err != nil {
log.Println(err, n)
}
obj := []ObjectDefinition{}
l := n["Objects"].([]interface{})
for _, _v := range l {
v := _v.(map[string]interface{})
m := &DefaultObjectDefinition{}
id := int(v["Id"].(float64))
m.Id = LWM2MObjectType(id)
if v["Name"] != nil {
m.Name = v["Name"].(string)
}
if v["Description"] != nil {
m.Description = v["Description"].(string)
}
m.Multiple, _ = v["Multiple"].(bool)
m.Mandatory, _ = v["Mandatory"].(bool)
if v["Resources"] != nil {
resources := v["Resources"].([]interface{})
res := []ResourceDefinition{}
for _, v2 := range resources {
r := v2.(map[string]interface{})
rd := &DefaultResourceDefinition{}
rd.Id = LWM2MResourceType(int(r["Id"].(float64)))
if r["Name"] != nil {
rd.Name = r["Name"].(string)
}
if r["Multiple"] != nil {
rd.Multiple = r["Multiple"].(bool)
}
if r["Mandatory"] != nil {
rd.Mandatory = r["Mandatory"].(bool)
}
op := r["Operations"]
rt := r["ResourceType"]
if r["RangeOrEnums"] != nil {
rd.RangeOrEnums = r["RangeOrEnums"].(string)
}
// vv := r["ValueValidator"].(yaml.Scalar).String()
switch rt {
case "multiple":
rd.ResourceType = VALUETYPE_MULTIPLE
case "string":
rd.ResourceType = VALUETYPE_STRING
case "byte":
rd.ResourceType = VALUETYPE_BYTE
case "int":
rd.ResourceType = VALUETYPE_INTEGER
case "int32":
rd.ResourceType = VALUETYPE_INTEGER32
case "int64":
rd.ResourceType = VALUETYPE_INTEGER64
case "float":
rd.ResourceType = VALUETYPE_FLOAT
case "float64":
rd.ResourceType = VALUETYPE_FLOAT64
case "bool":
rd.ResourceType = VALUETYPE_BOOLEAN
case "opaque":
rd.ResourceType = VALUETYPE_OPAQUE
case "time":
rd.ResourceType = VALUETYPE_TIME
case "objectlink":
rd.ResourceType = VALUETYPE_OBJECTLINK
case "object":
rd.ResourceType = VALUETYPE_OBJECT
case "resource":
rd.ResourceType = VALUETYPE_RESOURCE
case "multiresource":
rd.ResourceType = VALUETYPE_MULTIRESOURCE
}
switch op {
case "N":
rd.Operations = OPERATION_NONE
case "R":
rd.Operations = OPERATION_R
case "W":
rd.Operations = OPERATION_W
case "RW":
rd.Operations = OPERATION_RW
case "E":
rd.Operations = OPERATION_E
case "RE":
rd.Operations = OPERATION_RE
case "WE":
rd.Operations = OPERATION_WE
case "RWE":
rd.Operations = OPERATION_RWE
}
res = append(res, rd)
}
m.SetResources(res)
}
obj = append(obj, m)
}
return obj
}