mirror of
https://github.com/luxfi/pubsub.git
synced 2026-10-30 15:20:52 +00:00
Replace codec.Uint64/Float64 with in-package Uint64/Float64 JSON wrappers (jsonnum.go). The pubsub messages need string-encoded numerics at the JSON-RPC boundary; no broader codec dependency is needed for that. Files touched: - jsonnum.go (new): Uint64/Float64 with MarshalJSON/UnmarshalJSON - messages.go: drop codec import, use local Uint64/Float64 luxfi/codec is now indirect-only via workspace transitives.
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
// Copyright (C) 2019-2026, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package pubsub
|
|
|
|
import "strconv"
|
|
|
|
// Uint64 is a uint64 that JSON marshals as a quoted decimal string. Used at
|
|
// the JSON-RPC boundary where JS clients lose precision on integers > 2^53.
|
|
type Uint64 uint64
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
func (u Uint64) MarshalJSON() ([]byte, error) {
|
|
return []byte(`"` + strconv.FormatUint(uint64(u), 10) + `"`), nil
|
|
}
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
func (u *Uint64) UnmarshalJSON(b []byte) error {
|
|
s := string(b)
|
|
if s == "null" {
|
|
return nil
|
|
}
|
|
if n := len(s); n >= 2 && s[0] == '"' && s[n-1] == '"' {
|
|
s = s[1 : n-1]
|
|
}
|
|
v, err := strconv.ParseUint(s, 10, 64)
|
|
*u = Uint64(v)
|
|
return err
|
|
}
|
|
|
|
// Float64 is a float64 that JSON marshals as a quoted decimal string.
|
|
type Float64 float64
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
func (f Float64) MarshalJSON() ([]byte, error) {
|
|
return []byte(`"` + strconv.FormatFloat(float64(f), 'f', -1, 64) + `"`), nil
|
|
}
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
func (f *Float64) UnmarshalJSON(b []byte) error {
|
|
s := string(b)
|
|
if s == "null" {
|
|
return nil
|
|
}
|
|
if n := len(s); n >= 2 && s[0] == '"' && s[n-1] == '"' {
|
|
s = s[1 : n-1]
|
|
}
|
|
v, err := strconv.ParseFloat(s, 64)
|
|
*f = Float64(v)
|
|
return err
|
|
}
|