summaryrefslogtreecommitdiff
path: root/lib/jsonutil/binstruct.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/jsonutil/binstruct.go')
-rw-r--r--lib/jsonutil/binstruct.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/jsonutil/binstruct.go b/lib/jsonutil/binstruct.go
new file mode 100644
index 0000000..7f4bd3f
--- /dev/null
+++ b/lib/jsonutil/binstruct.go
@@ -0,0 +1,48 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package jsonutil
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+
+ "git.lukeshu.com/go/lowmemjson"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/binstruct"
+ "git.lukeshu.com/btrfs-progs-ng/lib/textui"
+)
+
+type Binary[T any] struct {
+ Val T
+}
+
+var (
+ _ lowmemjson.Encodable = Binary[int]{}
+ _ lowmemjson.Decodable = (*Binary[int])(nil)
+)
+
+func (o Binary[T]) EncodeJSON(w io.Writer) error {
+ bs, err := binstruct.Marshal(o.Val)
+ if err != nil {
+ return err
+ }
+ return EncodeSplitHexString(w, bs, textui.Tunable(80))
+}
+
+func (o *Binary[T]) DecodeJSON(r io.RuneScanner) error {
+ var buf bytes.Buffer
+ if err := DecodeSplitHexString(r, &buf); err != nil {
+ return err
+ }
+ n, err := binstruct.Unmarshal(buf.Bytes(), &o.Val)
+ if err != nil {
+ return err
+ }
+ if n < buf.Len() {
+ return fmt.Errorf("%d bytes of garbage after value", n-buf.Len())
+ }
+ return nil
+}