Files
realy/json/unsigned.go
mleku 5699562396 converge all json marshal and unmarshal to less wordy
also changes the interface of unmarshal to not return an error, which is something that should really belong elsewhere, both a thing to forget and a thing to have divergences in, not the right way to do it
2024-12-07 15:57:36 +00:00

36 lines
826 B
Go

package json
import (
"realy.lol/ints"
)
// Unsigned integers have no possible `-` prefix nor a decimal place.
//
// Initialize these values as follows:
//
// num := &json.Unsigned{}
//
// to get a default value which is zero.
//
// There is a generic NewUnsigned function which will automatically convert any integer type to
// the internal uint64 type, saving the caller from needing to cast it in their code.
type Unsigned struct{ V uint64 }
func NewUnsigned[V int64 | int32 | int16 | int8 | uint64 | uint32 | uint16 |
uint8](i V) *Signed {
return &Signed{int64(i)}
}
func (u *Unsigned) Marshal(dst by) (b by) { return ints.New(u.V).Marshal(dst) }
func (u *Unsigned) Unmarshal(dst by) (rem by, err er) {
rem = dst
n := ints.New(u.V)
if rem, err = n.Unmarshal(dst); chk.E(err) {
return
}
u.V = n.N
return
}