mirror of
https://github.com/luxfi/util.git
synced 2026-10-30 15:13:56 +00:00
Moves the JSON-RPC numeric wrappers (Uint8/16/32/64, Float32/64) and the gorilla/rpc lowercase Codec helper into the canonical luxfi/utils module. Replaces github.com/luxfi/codec/jsonrpc which has been retained only as a historical artefact of the codec module rip. All Lux modules that need JSON-RPC numeric quoting now have one canonical import: import "github.com/luxfi/utils/json" Identical API to codec/jsonrpc: Uint64.MarshalJSON quotes as decimal string, NewCodec returns a gorilla/rpc Codec that uppercases the first method letter.
29 lines
669 B
Go
29 lines
669 B
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package json
|
|
|
|
import "strconv"
|
|
|
|
// Float64 is a float64 that marshals/unmarshals as a JSON string.
|
|
type Float64 float64
|
|
|
|
func (f Float64) MarshalJSON() ([]byte, error) {
|
|
return []byte(`"` + strconv.FormatFloat(float64(f), 'f', -1, 64) + `"`), nil
|
|
}
|
|
|
|
func (f *Float64) UnmarshalJSON(b []byte) error {
|
|
str := string(b)
|
|
if str == Null {
|
|
return nil
|
|
}
|
|
if len(str) >= 2 {
|
|
if lastIndex := len(str) - 1; str[0] == '"' && str[lastIndex] == '"' {
|
|
str = str[1:lastIndex]
|
|
}
|
|
}
|
|
val, err := strconv.ParseFloat(str, 64)
|
|
*f = Float64(val)
|
|
return err
|
|
}
|