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 }
func NumberToFloat(num json.Number) (value float64) { var err error value, err = num.Float64() if err != nil { value = 0.0 } return }
// 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() }
// 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) }
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 }
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 }
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 }
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 }
//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 }
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 }
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() }
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) } }
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 }