mirror of
https://github.com/luxfi/utils.git
synced 2026-10-30 15:13:57 +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
654 B
Go
29 lines
654 B
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package json
|
|
|
|
import "strconv"
|
|
|
|
// Uint16 is a uint16 that marshals/unmarshals as a JSON string.
|
|
type Uint16 uint16
|
|
|
|
func (u Uint16) MarshalJSON() ([]byte, error) {
|
|
return []byte(`"` + strconv.FormatUint(uint64(u), 10) + `"`), nil
|
|
}
|
|
|
|
func (u *Uint16) 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.ParseUint(str, 10, 16)
|
|
*u = Uint16(val)
|
|
return err
|
|
}
|