本文整理匯總了Golang中encoding/json.Number.Int64方法的典型用法代碼示例。如果您正苦於以下問題:Golang Number.Int64方法的具體用法?Golang Number.Int64怎麽用?Golang Number.Int64使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類encoding/json.Number
的用法示例。
在下文中一共展示了Number.Int64方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: UnmarshalJSON
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
}
示例2: convertNumber
// 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()
}
示例3: resultErrorFormatJsonNumber
// 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)
}
示例4: transformNumber
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()
}
示例5: UnmarshalJSON
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
}
示例6: loadNumberIntoValues
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)
}
}
示例7: parseNumber
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
}
示例8: tryInt64
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
}
示例9: getQueryTime
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
}
示例10: UnmarshalJSON
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
}