summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-07-22 22:52:13 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2023-07-22 23:00:34 -0600
commitab268a66f3554976cc1ffac9dbf03c6ad2bcdf0c (patch)
tree11cfe6110aa9bd987bf5dc905dcfe3b4610f4aec
parent77628ce11ce3693d8ac06f1a404a1005ba05f190 (diff)
containers: Optional: Migrate from stdlib json to lowmemjson
-rw-r--r--lib/containers/optional.go26
1 files changed, 17 insertions, 9 deletions
diff --git a/lib/containers/optional.go b/lib/containers/optional.go
index 26ec494..f29665e 100644
--- a/lib/containers/optional.go
+++ b/lib/containers/optional.go
@@ -5,7 +5,9 @@
package containers
import (
- "encoding/json"
+ "io"
+
+ "git.lukeshu.com/go/lowmemjson"
)
type Optional[T any] struct {
@@ -27,22 +29,28 @@ func OptionalNil[T any]() Optional[T] {
}
var (
- _ json.Marshaler = Optional[bool]{}
- _ json.Unmarshaler = (*Optional[bool])(nil)
+ _ lowmemjson.Encodable = Optional[bool]{}
+ _ lowmemjson.Decodable = (*Optional[bool])(nil)
)
-func (o Optional[T]) MarshalJSON() ([]byte, error) {
+func (o Optional[T]) EncodeJSON(w io.Writer) error {
if !o.OK {
- return []byte("null"), nil
+ _, err := io.WriteString(w, "null")
+ return err
}
- return json.Marshal(o.Val)
+ return lowmemjson.NewEncoder(w).Encode(o.Val)
}
-func (o *Optional[T]) UnmarshalJSON(dat []byte) error {
- if string(dat) == "null" {
+func (o *Optional[T]) DecodeJSON(r io.RuneScanner) error {
+ c, _, _ := r.ReadRune()
+ if c == 'n' {
+ _, _, _ = r.ReadRune() // u
+ _, _, _ = r.ReadRune() // l
+ _, _, _ = r.ReadRune() // l
*o = Optional[T]{}
return nil
}
+ _ = r.UnreadRune()
o.OK = true
- return json.Unmarshal(dat, &o.Val)
+ return lowmemjson.NewDecoder(r).Decode(&o.Val)
}