mirror of
https://github.com/luxfi/api.git
synced 2026-09-24 00:24:53 +00:00
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package types
|
|
|
|
import "strconv"
|
|
|
|
const Null = "null"
|
|
|
|
type Uint32 uint32
|
|
|
|
func (u Uint32) MarshalJSON() ([]byte, error) {
|
|
return []byte(`"` + strconv.FormatUint(uint64(u), 10) + `"`), nil
|
|
}
|
|
|
|
func (u *Uint32) 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, 32)
|
|
*u = Uint32(val)
|
|
return err
|
|
}
|
|
|
|
type Uint64 uint64
|
|
|
|
func (u Uint64) MarshalJSON() ([]byte, error) {
|
|
return []byte(`"` + strconv.FormatUint(uint64(u), 10) + `"`), nil
|
|
}
|
|
|
|
func (u *Uint64) 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, 64)
|
|
*u = Uint64(val)
|
|
return err
|
|
}
|
|
|
|
type Float64 float64
|
|
|
|
func (f Float64) MarshalJSON() ([]byte, error) {
|
|
return []byte(`"` + strconv.FormatFloat(float64(f), 'f', 4, 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
|
|
}
|