forked from ricardolonga/jsongo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.go
158 lines (129 loc) · 4.15 KB
/
object.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
package jsone
import (
"fmt"
"reflect"
)
// O represents a Json Object.
type O map[string]interface{}
// Object creates a json object.
func Object() O {
return O{}
}
// Put inserts an element into a json object.
func (jObj O) Put(key string, value interface{}) O {
jObj[key] = value
return jObj
}
// Get retrieves an element from a json object. Type of the return value is not predefined,
// caller has to check the return type.
func (jObj O) Get(key string) interface{} {
return jObj[key]
}
// GetString retrieves a string data from a json object. Return error, if key not exist or data type not string.
func (jObj O) GetString(key string) (string, error) {
switch jObj[key].(type) {
case string:
return jObj[key].(string), nil
}
return "", fmt.Errorf("casting error[%s]. Interface is %s, not string", key, reflect.TypeOf(jObj[key]))
}
// GetInt retrieves an int data from a json object. Return error, if key not exist or data type not int.
func (jObj O) GetInt(key string) (int, error) {
switch jObj[key].(type) {
case int:
return jObj[key].(int), nil
}
return 0, fmt.Errorf("casting error[%s]. Interface is %s, not int", key, reflect.TypeOf(jObj[key]))
}
// GetInt64 retrieves an int64 data from a json object. Return error, if key not exist or data type not int64.
func (jObj O) GetInt64(key string) (int64, error) {
switch jObj[key].(type) {
case int64:
return jObj[key].(int64), nil
}
return 0, fmt.Errorf("casting error[%s]. Interface is %s, not int64", key, reflect.TypeOf(jObj[key]))
}
// GetFloat64 retrieves a float64 data from a json object. Return error, if key not exist or data type not float64.
func (jObj O) GetFloat64(key string) (float64, error) {
switch jObj[key].(type) {
case float64:
return jObj[key].(float64), nil
}
return 0.0, fmt.Errorf("casting error[%s]. Interface is %s, not float64", key,
reflect.TypeOf(jObj[key]))
}
// GetBoolean retrieves a boolean data from a json object. Return error, if key not exist or data type not boolean.
func (jObj O) GetBoolean(key string) (bool, error) {
switch jObj[key].(type) {
case bool:
return jObj[key].(bool), nil
}
return false, fmt.Errorf("casting error[%s]. Interface is %s, not boolean", key, reflect.TypeOf(jObj[key]))
}
// GetObject retrieves a json object data from a json object. Return error,
// if key not exist or data type not json object.
func (jObj O) GetObject(key string) (value O, err error) {
switch jObj[key].(type) {
case map[string]interface{}:
object := Object()
for k, v := range jObj[key].(map[string]interface{}) {
object.Put(k, v)
}
return object, nil
case O:
return jObj[key].(O), nil
}
return nil, fmt.Errorf("casting error[%s]. Interface is %s, not jsone.object",
key, reflect.TypeOf(jObj[key]))
}
// GetArray retrieves a json array data from a json object. Return error, if key not exist or data type not json array.
func (jObj O) GetArray(key string) (newArray *A, err error) {
newArray = Array()
switch jObj[key].(type) {
case []interface{}:
values := jObj[key].([]interface{})
for _, value := range values {
newArray.Put(value)
}
return newArray, nil
case []string:
values := jObj[key].([]string)
for _, value := range values {
newArray.Put(value)
}
return newArray, nil
case *A:
return jObj[key].(*A), nil
}
return nil, fmt.Errorf("casting error[%s]. Interface is %s, not jsone.A or []interface{}",
key, reflect.TypeOf(jObj[key]))
}
// Keys returns the list of keys in the current object.
func (jObj O) Keys() (objKeys []string) {
keys := make([]string, 0)
if nil == jObj {
return keys
}
for k := range jObj {
keys = append(keys, k)
}
return keys
}
// Remove an element from a json object.
func (jObj O) Remove(key string) O {
delete(jObj, key)
return jObj
}
// Has checks the object has an element in the name of the input string. Returns true if present, else false.
func (jObj O) Has(key string) bool {
_, ok := jObj[key]
return ok
}
// String on object generates a string representation of json object.
func (jObj O) String() string {
return _string(jObj)
}
// Indent on object generates a string representation of json object with proper indent.
func (jObj O) Indent() string {
return indent(jObj)
}