当前位置: 首页>>代码示例>>Golang>>正文


Golang json.Number类代码示例

本文整理汇总了Golang中encoding/json.Number的典型用法代码示例。如果您正苦于以下问题:Golang Number类的具体用法?Golang Number怎么用?Golang Number使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Number类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: UnmarshalJSON

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
}
开发者ID:nilium,项目名称:dagr,代码行数:30,代码来源:field.go

示例2: NumberToFloat

func NumberToFloat(num json.Number) (value float64) {
	var err error
	value, err = num.Float64()
	if err != nil {
		value = 0.0
	}
	return
}
开发者ID:ifzz,项目名称:btcrobot,代码行数:8,代码来源:kline.go

示例3: 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()
}
开发者ID:CodeJuan,项目名称:kubernetes,代码行数:10,代码来源:json.go

示例4: 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)
}
开发者ID:haj,项目名称:kompose,代码行数:11,代码来源:utils.go

示例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
}
开发者ID:st3v3nle,项目名称:cadvisor,代码行数:13,代码来源:oauth2.go

示例6: 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
}
开发者ID:cch123,项目名称:jsonport,代码行数:16,代码来源:jsonport.go

示例7: 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
}
开发者ID:zzzmanzzz,项目名称:KsanaDB,代码行数:17,代码来源:Utility.go

示例8: 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
}
开发者ID:coreos,项目名称:go-oidc,代码行数:19,代码来源:oidc.go

示例9: ExecuteQuery

//ExecuteQuery executes an InfluxDB query and returns the resultant value
func (db *Scheduler) ExecuteQuery(query string, database string) (float64, error) {
	host, err := url.Parse(fmt.Sprintf("http://%s:%d", db.InfluxDBConfig.InfluxDBHost, db.InfluxDBConfig.InfluxDBPort))
	if err != nil {
		log.Fatal(err)
	}
	con, err := client.NewClient(client.Config{
		URL: *host,
		//Username: db.InfluxDBConfig.InfluxDBUsername,
		//Password: db.InfluxDBConfig.InfluxDBPassword,
	})

	if err != nil {
		return 0, err
	}

	q := client.Query{
		Command:  query,
		Database: database,
	}

	response, err := con.Query(q)

	if err == nil {
		var jsonValue json.Number
		//This somewhat unpleasant looking line goes through several arrays nested structs
		//to get to the actual value.
		responseValue := response.Results[0].Series[0].Values[0][1]
		//the value is encoded as a JSON number as it comes from the web API
		jsonValue = responseValue.(json.Number)
		//parse a float from the json value
		value, _ := jsonValue.Float64()

		if response.Err == nil {
			return value, nil
		}
		return 0, fmt.Errorf("Unable to parse value from InfluxDB query, ensure that query returns a single value and that the series contains data")
	}
	return 0, err
}
开发者ID:ccpgames,项目名称:ccpalert,代码行数:40,代码来源:scheduler.go

示例10: jsonToConst

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
}
开发者ID:BatchLabs,项目名称:charlatan,代码行数:36,代码来源:json.go

示例11: 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()
}
开发者ID:ruflin,项目名称:beats,代码行数:11,代码来源:transform.go

示例12: 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)
	}
}
开发者ID:gogolfing,项目名称:config,代码行数:13,代码来源:json.go

示例13: 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
}
开发者ID:russolsen,项目名称:transit,代码行数:14,代码来源:decode.go


注:本文中的encoding/json.Number类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。