Exemplo n.º 1
0
Arquivo: field.go Projeto: nilium/dagr
func (f *Float) UnmarshalJSON(in []byte) error {
	if len(in) == 0 {
		return &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(f)}
	}

	var err error
	var next float64
	switch in[0] {
	case 'n', 't', 'f', '{', '[':
		return &json.UnmarshalTypeError{Value: badJSONValue(in), Type: reflect.TypeOf(f)}
	case '"':
		var new json.Number
		err = json.Unmarshal(in, &new)
		if err == nil {
			next, err = new.Float64()

			if err != nil {
				err = &json.UnmarshalTypeError{Value: "quoted number " + new.String(), Type: reflect.TypeOf(f)}
			}
		}
	default:
		err = json.Unmarshal(in, &next)
	}

	if err == nil {
		f.Set(next)
	}

	return err
}
Exemplo n.º 2
0
func transformNumber(value json.Number) interface{} {
	i64, err := value.Int64()
	if err == nil {
		return i64
	}
	f64, err := value.Float64()
	if err == nil {
		return f64
	}
	return value.String()
}
Exemplo n.º 3
0
func (l *Loader) loadNumberIntoValues(key config.Key, values *config.Values, num jsonlib.Number) {
	if l.NumberAsString {
		values.Put(key, num.String())
		return
	}
	i64, err := num.Int64()
	if err != nil {
		f64, _ := num.Float64()
		values.Put(key, f64)
	} else {
		values.Put(key, i64)
	}
}
Exemplo n.º 4
0
func (d Decoder) parseNumber(x json.Number) (interface{}, error) {
	var s = x.String()
	var err error

	var result interface{}

	if strings.ContainsAny(s, ".Ee") {
		result, err = x.Float64()
	} else {
		result, err = x.Int64()
	}

	return result, err
}
Exemplo n.º 5
0
func jsonToConst(partial *json.RawMessage) (*ch.Const, error) {
	var value string

	if partial == nil {
		return ch.NullConst(), nil
	}

	asString := string(*partial)

	if asString == "null" {
		return ch.NullConst(), nil
	}

	if err := json.Unmarshal(*partial, &value); err != nil {
		if err, ok := err.(*json.UnmarshalTypeError); ok {
			// we failed to unmarshal into a string, let's try the other types
			switch err.Value {
			case "number":
				var n json.Number
				if err := json.Unmarshal(*partial, &n); err != nil {
					return nil, err
				}

				value = n.String()

			case "bool":
				value = asString

			default:
				return nil, err
			}
		}
	}

	return ch.ConstFromString(value), nil
}