Beispiel #1
0
func (n *Int) UnmarshalJSON(in []byte) error {
	if len(in) == 0 {
		return &json.UnmarshalTypeError{Value: "empty JSON", Type: reflect.TypeOf(n)}
	}

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

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

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

	return nil
}
Beispiel #2
0
// convertNumber converts a json.Number to an int64 or float64, or returns an error
func convertNumber(n json.Number) (interface{}, error) {
	// Attempt to convert to an int64 first
	if i, err := n.Int64(); err == nil {
		return i, nil
	}
	// Return a float64 (default json.Decode() behavior)
	// An overflow will return an error
	return n.Float64()
}
Beispiel #3
0
// formats a number so that it is displayed as the smallest string possible
func resultErrorFormatJsonNumber(n json.Number) string {

	if int64Value, err := n.Int64(); err == nil {
		return fmt.Sprintf("%d", int64Value)
	}

	float64Value, _ := n.Float64()

	return fmt.Sprintf("%g", float64Value)
}
Beispiel #4
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()
}
Beispiel #5
0
func (e *expirationTime) UnmarshalJSON(b []byte) error {
	var n json.Number
	err := json.Unmarshal(b, &n)
	if err != nil {
		return err
	}
	i, err := n.Int64()
	if err != nil {
		return err
	}
	*e = expirationTime(i)
	return nil
}
Beispiel #6
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)
	}
}
Beispiel #7
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
}
Beispiel #8
0
func tryInt64(n json.Number) (int64, error) {
	intn, err := n.Int64()
	if err == nil {
		return intn, nil
	}
	// fallback. convert float to int
	floatn, err1 := n.Float64()
	if err1 != nil {
		return 0, err1
	}
	uintn := uint64(floatn)
	if floatn != 0 && uintn == 0 {
		return 0, err
	}
	return int64(uintn), nil
}
Beispiel #9
0
func getQueryTime(tNow time.Time, unit *string, value *json.Number) (int64, error) {
	var absTime int64

	if unit == nil || value == nil {
		return -1, errors.New("Need set relative time")
	} else {
		relativeTime, err := value.Int64()
		if err != nil {
			return -2, err
		}
		absTime, err = relativeToAbsoluteTime(tNow, int(relativeTime), *unit)
		if err != nil {
			return -3, err
		}
	}
	return absTime, nil
}
Beispiel #10
0
func (j *jsonTime) UnmarshalJSON(b []byte) error {
	var n json.Number
	if err := json.Unmarshal(b, &n); err != nil {
		return err
	}
	var unix int64

	if t, err := n.Int64(); err == nil {
		unix = t
	} else {
		f, err := n.Float64()
		if err != nil {
			return err
		}
		unix = int64(f)
	}
	*j = jsonTime(time.Unix(unix, 0))
	return nil
}