| 1 |
package simplejson |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"log" |
| 7 |
) |
| 8 |
|
| 9 |
// returns the current implementation version |
| 10 |
func Version() string { |
| 11 |
return "0.5.0" |
| 12 |
} |
| 13 |
|
| 14 |
type Json struct { |
| 15 |
data interface{} |
| 16 |
} |
| 17 |
|
| 18 |
// NewJson returns a pointer to a new `Json` object |
| 19 |
// after unmarshaling `body` bytes |
| 20 |
func NewJson(body []byte) (*Json, error) { |
| 21 |
j := new(Json) |
| 22 |
err := j.UnmarshalJSON(body) |
| 23 |
if err != nil { |
| 24 |
return nil, err |
| 25 |
} |
| 26 |
return j, nil |
| 27 |
} |
| 28 |
|
| 29 |
// New returns a pointer to a new, empty `Json` object |
| 30 |
func New() *Json { |
| 31 |
return &Json{ |
| 32 |
data: make(map[string]interface{}), |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// Interface returns the underlying data |
| 37 |
func (j *Json) Interface() interface{} { |
| 38 |
return j.data |
| 39 |
} |
| 40 |
|
| 41 |
// Encode returns its marshaled data as `[]byte` |
| 42 |
func (j *Json) Encode() ([]byte, error) { |
| 43 |
return j.MarshalJSON() |
| 44 |
} |
| 45 |
|
| 46 |
// EncodePretty returns its marshaled data as `[]byte` with indentation |
| 47 |
func (j *Json) EncodePretty() ([]byte, error) { |
| 48 |
return json.MarshalIndent(&j.data, "", " ") |
| 49 |
} |
| 50 |
|
| 51 |
// Implements the json.Marshaler interface. |
| 52 |
func (j *Json) MarshalJSON() ([]byte, error) { |
| 53 |
return json.Marshal(&j.data) |
| 54 |
} |
| 55 |
|
| 56 |
// Set modifies `Json` map by `key` and `value` |
| 57 |
// Useful for changing single key/value in a `Json` object easily. |
| 58 |
func (j *Json) Set(key string, val interface{}) { |
| 59 |
m, err := j.Map() |
| 60 |
if err != nil { |
| 61 |
return |
| 62 |
} |
| 63 |
m[key] = val |
| 64 |
} |
| 65 |
|
| 66 |
// SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, |
| 67 |
// and then finally writing in the value |
| 68 |
func (j *Json) SetPath(branch []string, val interface{}) { |
| 69 |
if len(branch) == 0 { |
| 70 |
j.data = val |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
// in order to insert our branch, we need map[string]interface{} |
| 75 |
if _, ok := (j.data).(map[string]interface{}); !ok { |
| 76 |
// have to replace with something suitable |
| 77 |
j.data = make(map[string]interface{}) |
| 78 |
} |
| 79 |
curr := j.data.(map[string]interface{}) |
| 80 |
|
| 81 |
for i := 0; i < len(branch)-1; i++ { |
| 82 |
b := branch[i] |
| 83 |
// key exists? |
| 84 |
if _, ok := curr[b]; !ok { |
| 85 |
n := make(map[string]interface{}) |
| 86 |
curr[b] = n |
| 87 |
curr = n |
| 88 |
continue |
| 89 |
} |
| 90 |
|
| 91 |
// make sure the value is the right sort of thing |
| 92 |
if _, ok := curr[b].(map[string]interface{}); !ok { |
| 93 |
// have to replace with something suitable |
| 94 |
n := make(map[string]interface{}) |
| 95 |
curr[b] = n |
| 96 |
} |
| 97 |
|
| 98 |
curr = curr[b].(map[string]interface{}) |
| 99 |
} |
| 100 |
|
| 101 |
// add remaining k/v |
| 102 |
curr[branch[len(branch)-1]] = val |
| 103 |
} |
| 104 |
|
| 105 |
// Del modifies `Json` map by deleting `key` if it is present. |
| 106 |
func (j *Json) Del(key string) { |
| 107 |
m, err := j.Map() |
| 108 |
if err != nil { |
| 109 |
return |
| 110 |
} |
| 111 |
delete(m, key) |
| 112 |
} |
| 113 |
|
| 114 |
// Get returns a pointer to a new `Json` object |
| 115 |
// for `key` in its `map` representation |
| 116 |
// |
| 117 |
// useful for chaining operations (to traverse a nested JSON): |
| 118 |
// js.Get("top_level").Get("dict").Get("value").Int() |
| 119 |
func (j *Json) Get(key string) *Json { |
| 120 |
m, err := j.Map() |
| 121 |
if err == nil { |
| 122 |
if val, ok := m[key]; ok { |
| 123 |
return &Json{val} |
| 124 |
} |
| 125 |
} |
| 126 |
return &Json{nil} |
| 127 |
} |
| 128 |
|
| 129 |
// GetPath searches for the item as specified by the branch |
| 130 |
// without the need to deep dive using Get()'s. |
| 131 |
// |
| 132 |
// js.GetPath("top_level", "dict") |
| 133 |
func (j *Json) GetPath(branch ...string) *Json { |
| 134 |
jin := j |
| 135 |
for _, p := range branch { |
| 136 |
jin = jin.Get(p) |
| 137 |
} |
| 138 |
return jin |
| 139 |
} |
| 140 |
|
| 141 |
// GetIndex returns a pointer to a new `Json` object |
| 142 |
// for `index` in its `array` representation |
| 143 |
// |
| 144 |
// this is the analog to Get when accessing elements of |
| 145 |
// a json array instead of a json object: |
| 146 |
// js.Get("top_level").Get("array").GetIndex(1).Get("key").Int() |
| 147 |
func (j *Json) GetIndex(index int) *Json { |
| 148 |
a, err := j.Array() |
| 149 |
if err == nil { |
| 150 |
if len(a) > index { |
| 151 |
return &Json{a[index]} |
| 152 |
} |
| 153 |
} |
| 154 |
return &Json{nil} |
| 155 |
} |
| 156 |
|
| 157 |
// CheckGet returns a pointer to a new `Json` object and |
| 158 |
// a `bool` identifying success or failure |
| 159 |
// |
| 160 |
// useful for chained operations when success is important: |
| 161 |
// if data, ok := js.Get("top_level").CheckGet("inner"); ok { |
| 162 |
// log.Println(data) |
| 163 |
// } |
| 164 |
func (j *Json) CheckGet(key string) (*Json, bool) { |
| 165 |
m, err := j.Map() |
| 166 |
if err == nil { |
| 167 |
if val, ok := m[key]; ok { |
| 168 |
return &Json{val}, true |
| 169 |
} |
| 170 |
} |
| 171 |
return nil, false |
| 172 |
} |
| 173 |
|
| 174 |
// Map type asserts to `map` |
| 175 |
func (j *Json) Map() (map[string]interface{}, error) { |
| 176 |
if m, ok := (j.data).(map[string]interface{}); ok { |
| 177 |
return m, nil |
| 178 |
} |
| 179 |
return nil, errors.New("type assertion to map[string]interface{} failed") |
| 180 |
} |
| 181 |
|
| 182 |
// Array type asserts to an `array` |
| 183 |
func (j *Json) Array() ([]interface{}, error) { |
| 184 |
if a, ok := (j.data).([]interface{}); ok { |
| 185 |
return a, nil |
| 186 |
} |
| 187 |
return nil, errors.New("type assertion to []interface{} failed") |
| 188 |
} |
| 189 |
|
| 190 |
// Bool type asserts to `bool` |
| 191 |
func (j *Json) Bool() (bool, error) { |
| 192 |
if s, ok := (j.data).(bool); ok { |
| 193 |
return s, nil |
| 194 |
} |
| 195 |
return false, errors.New("type assertion to bool failed") |
| 196 |
} |
| 197 |
|
| 198 |
// String type asserts to `string` |
| 199 |
func (j *Json) String() (string, error) { |
| 200 |
if s, ok := (j.data).(string); ok { |
| 201 |
return s, nil |
| 202 |
} |
| 203 |
return "", errors.New("type assertion to string failed") |
| 204 |
} |
| 205 |
|
| 206 |
// Bytes type asserts to `[]byte` |
| 207 |
func (j *Json) Bytes() ([]byte, error) { |
| 208 |
if s, ok := (j.data).(string); ok { |
| 209 |
return []byte(s), nil |
| 210 |
} |
| 211 |
return nil, errors.New("type assertion to []byte failed") |
| 212 |
} |
| 213 |
|
| 214 |
// StringArray type asserts to an `array` of `string` |
| 215 |
func (j *Json) StringArray() ([]string, error) { |
| 216 |
arr, err := j.Array() |
| 217 |
if err != nil { |
| 218 |
return nil, err |
| 219 |
} |
| 220 |
retArr := make([]string, 0, len(arr)) |
| 221 |
for _, a := range arr { |
| 222 |
if a == nil { |
| 223 |
retArr = append(retArr, "") |
| 224 |
continue |
| 225 |
} |
| 226 |
s, ok := a.(string) |
| 227 |
if !ok { |
| 228 |
return nil, err |
| 229 |
} |
| 230 |
retArr = append(retArr, s) |
| 231 |
} |
| 232 |
return retArr, nil |
| 233 |
} |
| 234 |
|
| 235 |
// MustArray guarantees the return of a `[]interface{}` (with optional default) |
| 236 |
// |
| 237 |
// useful when you want to interate over array values in a succinct manner: |
| 238 |
// for i, v := range js.Get("results").MustArray() { |
| 239 |
// fmt.Println(i, v) |
| 240 |
// } |
| 241 |
func (j *Json) MustArray(args ...[]interface{}) []interface{} { |
| 242 |
var def []interface{} |
| 243 |
|
| 244 |
switch len(args) { |
| 245 |
case 0: |
| 246 |
case 1: |
| 247 |
def = args[0] |
| 248 |
default: |
| 249 |
log.Panicf("MustArray() received too many arguments %d", len(args)) |
| 250 |
} |
| 251 |
|
| 252 |
a, err := j.Array() |
| 253 |
if err == nil { |
| 254 |
return a |
| 255 |
} |
| 256 |
|
| 257 |
return def |
| 258 |
} |
| 259 |
|
| 260 |
// MustMap guarantees the return of a `map[string]interface{}` (with optional default) |
| 261 |
// |
| 262 |
// useful when you want to interate over map values in a succinct manner: |
| 263 |
// for k, v := range js.Get("dictionary").MustMap() { |
| 264 |
// fmt.Println(k, v) |
| 265 |
// } |
| 266 |
func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { |
| 267 |
var def map[string]interface{} |
| 268 |
|
| 269 |
switch len(args) { |
| 270 |
case 0: |
| 271 |
case 1: |
| 272 |
def = args[0] |
| 273 |
default: |
| 274 |
log.Panicf("MustMap() received too many arguments %d", len(args)) |
| 275 |
} |
| 276 |
|
| 277 |
a, err := j.Map() |
| 278 |
if err == nil { |
| 279 |
return a |
| 280 |
} |
| 281 |
|
| 282 |
return def |
| 283 |
} |
| 284 |
|
| 285 |
// MustString guarantees the return of a `string` (with optional default) |
| 286 |
// |
| 287 |
// useful when you explicitly want a `string` in a single value return context: |
| 288 |
// myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default")) |
| 289 |
func (j *Json) MustString(args ...string) string { |
| 290 |
var def string |
| 291 |
|
| 292 |
switch len(args) { |
| 293 |
case 0: |
| 294 |
case 1: |
| 295 |
def = args[0] |
| 296 |
default: |
| 297 |
log.Panicf("MustString() received too many arguments %d", len(args)) |
| 298 |
} |
| 299 |
|
| 300 |
s, err := j.String() |
| 301 |
if err == nil { |
| 302 |
return s |
| 303 |
} |
| 304 |
|
| 305 |
return def |
| 306 |
} |
| 307 |
|
| 308 |
// MustStringArray guarantees the return of a `[]string` (with optional default) |
| 309 |
// |
| 310 |
// useful when you want to interate over array values in a succinct manner: |
| 311 |
// for i, s := range js.Get("results").MustStringArray() { |
| 312 |
// fmt.Println(i, s) |
| 313 |
// } |
| 314 |
func (j *Json) MustStringArray(args ...[]string) []string { |
| 315 |
var def []string |
| 316 |
|
| 317 |
switch len(args) { |
| 318 |
case 0: |
| 319 |
case 1: |
| 320 |
def = args[0] |
| 321 |
default: |
| 322 |
log.Panicf("MustStringArray() received too many arguments %d", len(args)) |
| 323 |
} |
| 324 |
|
| 325 |
a, err := j.StringArray() |
| 326 |
if err == nil { |
| 327 |
return a |
| 328 |
} |
| 329 |
|
| 330 |
return def |
| 331 |
} |
| 332 |
|
| 333 |
// MustInt guarantees the return of an `int` (with optional default) |
| 334 |
// |
| 335 |
// useful when you explicitly want an `int` in a single value return context: |
| 336 |
// myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150)) |
| 337 |
func (j *Json) MustInt(args ...int) int { |
| 338 |
var def int |
| 339 |
|
| 340 |
switch len(args) { |
| 341 |
case 0: |
| 342 |
case 1: |
| 343 |
def = args[0] |
| 344 |
default: |
| 345 |
log.Panicf("MustInt() received too many arguments %d", len(args)) |
| 346 |
} |
| 347 |
|
| 348 |
i, err := j.Int() |
| 349 |
if err == nil { |
| 350 |
return i |
| 351 |
} |
| 352 |
|
| 353 |
return def |
| 354 |
} |
| 355 |
|
| 356 |
// MustFloat64 guarantees the return of a `float64` (with optional default) |
| 357 |
// |
| 358 |
// useful when you explicitly want a `float64` in a single value return context: |
| 359 |
// myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150)) |
| 360 |
func (j *Json) MustFloat64(args ...float64) float64 { |
| 361 |
var def float64 |
| 362 |
|
| 363 |
switch len(args) { |
| 364 |
case 0: |
| 365 |
case 1: |
| 366 |
def = args[0] |
| 367 |
default: |
| 368 |
log.Panicf("MustFloat64() received too many arguments %d", len(args)) |
| 369 |
} |
| 370 |
|
| 371 |
f, err := j.Float64() |
| 372 |
if err == nil { |
| 373 |
return f |
| 374 |
} |
| 375 |
|
| 376 |
return def |
| 377 |
} |
| 378 |
|
| 379 |
// MustBool guarantees the return of a `bool` (with optional default) |
| 380 |
// |
| 381 |
// useful when you explicitly want a `bool` in a single value return context: |
| 382 |
// myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true)) |
| 383 |
func (j *Json) MustBool(args ...bool) bool { |
| 384 |
var def bool |
| 385 |
|
| 386 |
switch len(args) { |
| 387 |
case 0: |
| 388 |
case 1: |
| 389 |
def = args[0] |
| 390 |
default: |
| 391 |
log.Panicf("MustBool() received too many arguments %d", len(args)) |
| 392 |
} |
| 393 |
|
| 394 |
b, err := j.Bool() |
| 395 |
if err == nil { |
| 396 |
return b |
| 397 |
} |
| 398 |
|
| 399 |
return def |
| 400 |
} |
| 401 |
|
| 402 |
// MustInt64 guarantees the return of an `int64` (with optional default) |
| 403 |
// |
| 404 |
// useful when you explicitly want an `int64` in a single value return context: |
| 405 |
// myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150)) |
| 406 |
func (j *Json) MustInt64(args ...int64) int64 { |
| 407 |
var def int64 |
| 408 |
|
| 409 |
switch len(args) { |
| 410 |
case 0: |
| 411 |
case 1: |
| 412 |
def = args[0] |
| 413 |
default: |
| 414 |
log.Panicf("MustInt64() received too many arguments %d", len(args)) |
| 415 |
} |
| 416 |
|
| 417 |
i, err := j.Int64() |
| 418 |
if err == nil { |
| 419 |
return i |
| 420 |
} |
| 421 |
|
| 422 |
return def |
| 423 |
} |
| 424 |
|
| 425 |
// MustUInt64 guarantees the return of an `uint64` (with optional default) |
| 426 |
// |
| 427 |
// useful when you explicitly want an `uint64` in a single value return context: |
| 428 |
// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150)) |
| 429 |
func (j *Json) MustUint64(args ...uint64) uint64 { |
| 430 |
var def uint64 |
| 431 |
|
| 432 |
switch len(args) { |
| 433 |
case 0: |
| 434 |
case 1: |
| 435 |
def = args[0] |
| 436 |
default: |
| 437 |
log.Panicf("MustUint64() received too many arguments %d", len(args)) |
| 438 |
} |
| 439 |
|
| 440 |
i, err := j.Uint64() |
| 441 |
if err == nil { |
| 442 |
return i |
| 443 |
} |
| 444 |
|
| 445 |
return def |
| 446 |
} |