| 1 |
// +build go1.1 |
| 2 |
|
| 3 |
package simplejson |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"encoding/json" |
| 8 |
"errors" |
| 9 |
"io" |
| 10 |
"reflect" |
| 11 |
"strconv" |
| 12 |
) |
| 13 |
|
| 14 |
// Implements the json.Unmarshaler interface. |
| 15 |
func (j *Json) UnmarshalJSON(p []byte) error { |
| 16 |
dec := json.NewDecoder(bytes.NewBuffer(p)) |
| 17 |
dec.UseNumber() |
| 18 |
return dec.Decode(&j.data) |
| 19 |
} |
| 20 |
|
| 21 |
// NewFromReader returns a *Json by decoding from an io.Reader |
| 22 |
func NewFromReader(r io.Reader) (*Json, error) { |
| 23 |
j := new(Json) |
| 24 |
dec := json.NewDecoder(r) |
| 25 |
dec.UseNumber() |
| 26 |
err := dec.Decode(&j.data) |
| 27 |
return j, err |
| 28 |
} |
| 29 |
|
| 30 |
// Float64 coerces into a float64 |
| 31 |
func (j *Json) Float64() (float64, error) { |
| 32 |
switch j.data.(type) { |
| 33 |
case json.Number: |
| 34 |
return j.data.(json.Number).Float64() |
| 35 |
case float32, float64: |
| 36 |
return reflect.ValueOf(j.data).Float(), nil |
| 37 |
case int, int8, int16, int32, int64: |
| 38 |
return float64(reflect.ValueOf(j.data).Int()), nil |
| 39 |
case uint, uint8, uint16, uint32, uint64: |
| 40 |
return float64(reflect.ValueOf(j.data).Uint()), nil |
| 41 |
} |
| 42 |
return 0, errors.New("invalid value type") |
| 43 |
} |
| 44 |
|
| 45 |
// Int coerces into an int |
| 46 |
func (j *Json) Int() (int, error) { |
| 47 |
switch j.data.(type) { |
| 48 |
case json.Number: |
| 49 |
i, err := j.data.(json.Number).Int64() |
| 50 |
return int(i), err |
| 51 |
case float32, float64: |
| 52 |
return int(reflect.ValueOf(j.data).Float()), nil |
| 53 |
case int, int8, int16, int32, int64: |
| 54 |
return int(reflect.ValueOf(j.data).Int()), nil |
| 55 |
case uint, uint8, uint16, uint32, uint64: |
| 56 |
return int(reflect.ValueOf(j.data).Uint()), nil |
| 57 |
} |
| 58 |
return 0, errors.New("invalid value type") |
| 59 |
} |
| 60 |
|
| 61 |
// Int64 coerces into an int64 |
| 62 |
func (j *Json) Int64() (int64, error) { |
| 63 |
switch j.data.(type) { |
| 64 |
case json.Number: |
| 65 |
return j.data.(json.Number).Int64() |
| 66 |
case float32, float64: |
| 67 |
return int64(reflect.ValueOf(j.data).Float()), nil |
| 68 |
case int, int8, int16, int32, int64: |
| 69 |
return reflect.ValueOf(j.data).Int(), nil |
| 70 |
case uint, uint8, uint16, uint32, uint64: |
| 71 |
return int64(reflect.ValueOf(j.data).Uint()), nil |
| 72 |
} |
| 73 |
return 0, errors.New("invalid value type") |
| 74 |
} |
| 75 |
|
| 76 |
// Uint64 coerces into an uint64 |
| 77 |
func (j *Json) Uint64() (uint64, error) { |
| 78 |
switch j.data.(type) { |
| 79 |
case json.Number: |
| 80 |
return strconv.ParseUint(j.data.(json.Number).String(), 10, 64) |
| 81 |
case float32, float64: |
| 82 |
return uint64(reflect.ValueOf(j.data).Float()), nil |
| 83 |
case int, int8, int16, int32, int64: |
| 84 |
return uint64(reflect.ValueOf(j.data).Int()), nil |
| 85 |
case uint, uint8, uint16, uint32, uint64: |
| 86 |
return reflect.ValueOf(j.data).Uint(), nil |
| 87 |
} |
| 88 |
return 0, errors.New("invalid value type") |
| 89 |
} |