func()

in document/json/decoder.go [97:181]


func (d *Decoder) decodeJSONNumber(tv json.Number, rv reflect.Value) error {
	switch rv.Kind() {
	case reflect.Interface:
		rv.Set(reflect.ValueOf(document.Number(tv)))
	case reflect.String:
		// covers document.Number
		rv.SetString(tv.String())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		i, err := tv.Int64()
		if err != nil {
			return err
		}
		if rv.OverflowInt(i) {
			return &document.UnmarshalTypeError{
				Value: fmt.Sprintf("number overflow, %s", tv.String()),
				Type:  rv.Type(),
			}
		}
		rv.SetInt(i)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		u, err := document.Number(tv).Uint64()
		if err != nil {
			return err
		}
		if rv.OverflowUint(u) {
			return &document.UnmarshalTypeError{
				Value: fmt.Sprintf("number overflow, %s", tv.String()),
				Type:  rv.Type(),
			}
		}
		rv.SetUint(u)
	case reflect.Float32:
		f, err := document.Number(tv).Float32()
		if err != nil {
			return err
		}
		if rv.OverflowFloat(f) {
			return &document.UnmarshalTypeError{
				Value: fmt.Sprintf("float overflow, %s", tv.String()),
				Type:  rv.Type(),
			}
		}
		rv.SetFloat(f)
	case reflect.Float64:
		f, err := document.Number(tv).Float64()
		if err != nil {
			return err
		}
		if rv.OverflowFloat(f) {
			return &document.UnmarshalTypeError{
				Value: fmt.Sprintf("float overflow, %s", tv.String()),
				Type:  rv.Type(),
			}
		}
		rv.SetFloat(f)
	default:
		rvt := rv.Type()
		switch {
		case rvt.ConvertibleTo(serde.ReflectTypeOf.BigFloat):
			sv := tv.String()
			f, ok := (&big.Float{}).SetString(sv)
			if !ok {
				return &document.UnmarshalTypeError{
					Value: fmt.Sprintf("invalid number format, %s", sv),
					Type:  rv.Type(),
				}
			}
			rv.Set(reflect.ValueOf(*f).Convert(rvt))
		case rvt.ConvertibleTo(serde.ReflectTypeOf.BigInt):
			sv := tv.String()
			i, ok := (&big.Int{}).SetString(sv, 10)
			if !ok {
				return &document.UnmarshalTypeError{
					Value: fmt.Sprintf("invalid number format, %s", sv),
					Type:  rv.Type(),
				}
			}
			rv.Set(reflect.ValueOf(*i).Convert(rvt))
		default:
			return &document.UnmarshalTypeError{Value: "number", Type: rv.Type()}
		}
	}

	return nil
}