Skip to content

Commit

Permalink
handle all numeric types
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Jun 24, 2014
1 parent 73b1d0b commit af5b9f2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 56 deletions.
69 changes: 45 additions & 24 deletions simplejson_go10.go
Expand Up @@ -6,49 +6,70 @@ import (
"encoding/json"
"errors"
"io"
"reflect"
)

// NewFromReader returns a *Json by decoding from an io.Reader
func NewFromReader(r io.Reader) (*Json, error) {
j := new(Json)
dec := json.NewDecoder(r)
err := dec.Decode(&j.data)
return j, err
}

// Implements the json.Unmarshaler interface.
func (j *Json) UnmarshalJSON(p []byte) error {
return json.Unmarshal(p, &j.data)
}

// Float64 type asserts to `float64`
// Float64 coerces into a float64
func (j *Json) Float64() (float64, error) {
if i, ok := (j.data).(float64); ok {
return i, nil
switch j.data.(type) {
case float32, float64:
return reflect.ValueOf(j.data).Float(), nil
case int, int8, int16, int32, int64:
return float64(reflect.ValueOf(j.data).Int()), nil
case uint, uint8, uint16, uint32, uint64:
return float64(reflect.ValueOf(j.data).Uint()), nil
}
return -1, errors.New("type assertion to float64 failed")
}

// NewFromReader returns a *Json by decoding from an io.Reader
func NewFromReader(r io.Reader) (*Json, error) {
j := new(Json)
dec := json.NewDecoder(r)
err := dec.Decode(&j.data)
return j, err
return 0, errors.New("invalid value type")
}

// Int type asserts to `float64` then converts to `int`
// Int coerces into an int
func (j *Json) Int() (int, error) {
if f, ok := (j.data).(float64); ok {
return int(f), nil
switch j.data.(type) {
case float32, float64:
return int(reflect.ValueOf(j.data).Float()), nil
case int, int8, int16, int32, int64:
return int(reflect.ValueOf(j.data).Int()), nil
case uint, uint8, uint16, uint32, uint64:
return int(reflect.ValueOf(j.data).Uint()), nil
}
return -1, errors.New("type assertion to float64 failed")
return 0, errors.New("invalid value type")
}

// Int64 type asserts to `float64` then converts to `int64`
// Int64 coerces into an int64
func (j *Json) Int64() (int64, error) {
if f, ok := (j.data).(float64); ok {
return int64(f), nil
switch j.data.(type) {
case float32, float64:
return int64(reflect.ValueOf(j.data).Float()), nil
case int, int8, int16, int32, int64:
return reflect.ValueOf(j.data).Int(), nil
case uint, uint8, uint16, uint32, uint64:
return int64(reflect.ValueOf(j.data).Uint()), nil
}
return -1, errors.New("type assertion to float64 failed")
return 0, errors.New("invalid value type")
}

// Uint64 type asserts to `float64` then converts to `uint64`
// Uint64 coerces into an uint64
func (j *Json) Uint64() (uint64, error) {
if f, ok := (j.data).(float64); ok {
return uint64(f), nil
switch j.data.(type) {
case float32, float64:
return uint64(reflect.ValueOf(j.data).Float()), nil
case int, int8, int16, int32, int64:
return uint64(reflect.ValueOf(j.data).Int()), nil
case uint, uint8, uint16, uint32, uint64:
return reflect.ValueOf(j.data).Uint(), nil
}
return 0, errors.New("type assertion to float64 failed")
return 0, errors.New("invalid value type")
}
76 changes: 46 additions & 30 deletions simplejson_go11.go
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"io"
"reflect"
"strconv"
)

Expand All @@ -26,48 +27,63 @@ func NewFromReader(r io.Reader) (*Json, error) {
return j, err
}

// Float64 type asserts to `json.Number` then converts to `float64`
// Float64 coerces into a float64
func (j *Json) Float64() (float64, error) {
if n, ok := (j.data).(json.Number); ok {
return n.Float64()
switch j.data.(type) {
case json.Number:
return j.data.(json.Number).Float64()
case float32, float64:
return reflect.ValueOf(j.data).Float(), nil
case int, int8, int16, int32, int64:
return float64(reflect.ValueOf(j.data).Int()), nil
case uint, uint8, uint16, uint32, uint64:
return float64(reflect.ValueOf(j.data).Uint()), nil
}
if f, ok := (j.data).(float64); ok {
return f, nil
}
return -1, errors.New("type assertion to json.Number failed")
return 0, errors.New("invalid value type")
}

// Int type asserts to `json.Number` then converts to `int`
// Int coerces into an int
func (j *Json) Int() (int, error) {
if n, ok := (j.data).(json.Number); ok {
i, ok := n.Int64()
return int(i), ok
}
if f, ok := (j.data).(float64); ok {
return int(f), nil
switch j.data.(type) {
case json.Number:
i, err := j.data.(json.Number).Int64()
return int(i), err
case float32, float64:
return int(reflect.ValueOf(j.data).Float()), nil
case int, int8, int16, int32, int64:
return int(reflect.ValueOf(j.data).Int()), nil
case uint, uint8, uint16, uint32, uint64:
return int(reflect.ValueOf(j.data).Uint()), nil
}
return -1, errors.New("type assertion to json.Number failed")
return 0, errors.New("invalid value type")
}

// Int64 type asserts to `json.Number` then converts to `int64`
// Int64 coerces into an int64
func (j *Json) Int64() (int64, error) {
if n, ok := (j.data).(json.Number); ok {
return n.Int64()
switch j.data.(type) {
case json.Number:
return j.data.(json.Number).Int64()
case float32, float64:
return int64(reflect.ValueOf(j.data).Float()), nil
case int, int8, int16, int32, int64:
return reflect.ValueOf(j.data).Int(), nil
case uint, uint8, uint16, uint32, uint64:
return int64(reflect.ValueOf(j.data).Uint()), nil
}
if f, ok := (j.data).(float64); ok {
return int64(f), nil
}
return -1, errors.New("type assertion to json.Number failed")
return 0, errors.New("invalid value type")
}

// Uint64 type asserts to `json.Number` then converts to `uint64`
// Uint64 coerces into an uint64
func (j *Json) Uint64() (uint64, error) {
if n, ok := (j.data).(json.Number); ok {
u, err := strconv.ParseUint(n.String(), 10, 64)
return u, err
}
if f, ok := (j.data).(float64); ok {
return uint64(f), nil
switch j.data.(type) {
case json.Number:
return strconv.ParseUint(j.data.(json.Number).String(), 10, 64)
case float32, float64:
return uint64(reflect.ValueOf(j.data).Float()), nil
case int, int8, int16, int32, int64:
return uint64(reflect.ValueOf(j.data).Int()), nil
case uint, uint8, uint16, uint32, uint64:
return reflect.ValueOf(j.data).Uint(), nil
}
return 0, errors.New("type assertion to json.Number failed")
return 0, errors.New("invalid value type")
}
12 changes: 10 additions & 2 deletions simplejson_test.go
Expand Up @@ -2,8 +2,9 @@ package simplejson

import (
"encoding/json"
"github.com/bmizerany/assert"
"testing"

"github.com/bmizerany/assert"
)

func TestSimplejson(t *testing.T) {
Expand All @@ -20,7 +21,8 @@ func TestSimplejson(t *testing.T) {
"int": 10,
"float": 5.150,
"string": "simplejson",
"bool": true
"bool": true,
"sub_obj": {"a": 1}
}
}`))

Expand Down Expand Up @@ -101,6 +103,12 @@ func TestSimplejson(t *testing.T) {

js.Del("test2")
assert.NotEqual(t, "setTest", js.Get("test2").MustString())

js.Get("test").Get("sub_obj").Set("a", 2)
assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").MustInt())

js.GetPath("test", "sub_obj").Set("a", 3)
assert.Equal(t, 3, js.GetPath("test", "sub_obj", "a").MustInt())
}

func TestStdlibInterfaces(t *testing.T) {
Expand Down

0 comments on commit af5b9f2

Please sign in to comment.