Skip to content

Commit

Permalink
add Array() for conversion to []interface{}; add MustInt() and MustSt…
Browse files Browse the repository at this point in the history
…ring() and MustFloat64() for single value return context
  • Loading branch information
mreiferson committed May 14, 2012
1 parent 8660c5d commit 35ca82f
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 20 deletions.
43 changes: 34 additions & 9 deletions README.md
@@ -1,12 +1,11 @@
### go-simplejson

a dead simple library to interact with arbitrary JSON in Go
a Go package to interact with arbitrary JSON

### importing

import simplejson github.com/bitly/go-simplejson


### go doc

```
Expand All @@ -15,7 +14,6 @@ FUNCTIONS
func Version() string
returns the current implementation version
TYPES
type Json struct {
Expand All @@ -26,6 +24,9 @@ func NewJson(body []byte) (*Json, error)
NewJson returns a pointer to a new `Json` object after unmarshaling
`body` bytes
func (j *Json) Array() ([]interface{}, error)
Array type asserts to an `array`
func (j *Json) Bytes() ([]byte, error)
Bytes type asserts to `[]byte`
Expand All @@ -35,9 +36,9 @@ func (j *Json) CheckGet(key string) (*Json, bool)
useful for chained operations when success is important:
if data, ok := js.Get("top_level").CheckGet("inner"); ok {
log.Println(data)
}
if data, ok := js.Get("top_level").CheckGet("inner"); ok {
log.Println(data)
}
func (j *Json) Encode() ([]byte, error)
Encode returns it's marshaled data as `[]byte`
Expand All @@ -51,17 +52,41 @@ func (j *Json) Get(key string) *Json
useful for chaining operations (to traverse a nested JSON):
js.Get("top_level").Get("dict").Get("value").Int()
js.Get("top_level").Get("dict").Get("value").Int()
func (j *Json) Int() (int, error)
Int type asserts to `int`
Int type asserts to `float64` then converts to `int`
func (j *Json) Int64() (int64, error)
Int type asserts to `int64`
Int type asserts to `float64` then converts to `int64`
func (j *Json) Map() (map[string]interface{}, error)
Map type asserts to `map`
func (j *Json) MustFloat64(args ...float64) float64
MustFloat64 guarantees the return of a `float64` (with optional default)
useful when you explicitly want a `float64` in a single value return
context:
myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150))
func (j *Json) MustInt(args ...int) int
MustInt guarantees the return of an `int` (with optional default)
useful when you explicitly want an `int` in a single value return
context:
myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150))
func (j *Json) MustString(args ...string) string
MustString guarantees the return of a `string` (with optional default)
useful when you explicitly want a `string` in a single value return
context:
myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default"))
func (j *Json) String() (string, error)
String type asserts to `string`
```
103 changes: 92 additions & 11 deletions simplejson.go
Expand Up @@ -3,11 +3,12 @@ package simplejson
import (
"encoding/json"
"errors"
"log"
)

// returns the current implementation version
func Version() string {
return "0.1"
return "0.2"
}

type Json struct {
Expand Down Expand Up @@ -70,6 +71,14 @@ func (j *Json) Map() (map[string]interface{}, error) {
return nil, errors.New("type assertion to map[string]interface{} failed")
}

// Array type asserts to an `array`
func (j *Json) Array() ([]interface{}, error) {
if a, ok := (j.data).([]interface{}); ok {
return a, nil
}
return nil, errors.New("type assertion to []interface{} failed")
}

// String type asserts to `string`
func (j *Json) String() (string, error) {
if s, ok := (j.data).(string); ok {
Expand All @@ -86,28 +95,100 @@ func (j *Json) Float64() (float64, error) {
return -1, errors.New("type assertion to float64 failed")
}

// Int type asserts to `int`
// Int type asserts to `float64` then converts to `int`
func (j *Json) Int() (int, error) {
if f, ok := (j.data).(float64); ok {
i := int(f)
return i, nil
return int(f), nil
}
return -1, errors.New("type assertion to int failed")

return -1, errors.New("type assertion to float64 failed")
}

// Int type asserts to `int64`
// Int type asserts to `float64` then converts to `int64`
func (j *Json) Int64() (int64, error) {
if f, ok := (j.data).(float64); ok {
i := int64(f)
return i, nil
return int64(f), nil
}
return -1, errors.New("type assertion to int failed")

return -1, errors.New("type assertion to float64 failed")
}

// Bytes type asserts to `[]byte`
func (j *Json) Bytes() ([]byte, error) {
if b, ok := (j.data).([]byte); ok {
return b, nil
if s, ok := (j.data).(string); ok {
return []byte(s), nil
}
return nil, errors.New("type assertion to []byte failed")
}

// MustString guarantees the return of a `string` (with optional default)
//
// useful when you explicitly want a `string` in a single value return context:
// myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default"))
func (j *Json) MustString(args ...string) string {
var def string

switch len(args) {
case 0:
break
case 1:
def = args[0]
default:
log.Panicf("MustString() received too many arguments %d", len(args))
}

s, err := j.String()
if err == nil {
return s
}

return def
}

// MustInt guarantees the return of an `int` (with optional default)
//
// useful when you explicitly want an `int` in a single value return context:
// myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150))
func (j *Json) MustInt(args ...int) int {
var def int

switch len(args) {
case 0:
break
case 1:
def = args[0]
default:
log.Panicf("MustInt() received too many arguments %d", len(args))
}

i, err := j.Int()
if err == nil {
return i
}

return def
}

// MustFloat64 guarantees the return of a `float64` (with optional default)
//
// useful when you explicitly want a `float64` in a single value return context:
// myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150))
func (j *Json) MustFloat64(args ...float64) float64 {
var def float64

switch len(args) {
case 0:
break
case 1:
def = args[0]
default:
log.Panicf("MustFloat64() received too many arguments %d", len(args))
}

i, err := j.Float64()
if err == nil {
return i
}

return def
}

0 comments on commit 35ca82f

Please sign in to comment.