Golang encoding/json.Number type example

package encoding/json

Golang encoding/json.Number type usage example

 func (j *Json) Uint64() (uint64, error) {

 switch j.data.(type) {

 case json.Number: // <------ here
 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("invalid value type")
 }

Reference :

http://golang.org/pkg/encoding/json/#Number

Advertisement