Files
zeekay 659563d801 add json subpackage — canonical JSON-RPC numeric types + lowercase codec
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.
2026-06-06 02:50:51 -07:00

30 lines
706 B
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package json
import "strconv"
// Float32 is a float32 that marshals/unmarshals as a JSON string with 4 digit
// fixed-point precision.
type Float32 float32
func (f Float32) MarshalJSON() ([]byte, error) {
return []byte(`"` + strconv.FormatFloat(float64(f), 'f', 4, 32) + `"`), nil
}
func (f *Float32) 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, 32)
*f = Float32(val)
return err
}