summaryrefslogtreecommitdiff
path: root/lib/jsonutil
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-06 00:52:43 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-07-23 00:07:30 -0600
commitc65d6effc26c3d97a6193f65c5b7698c830d9ff0 (patch)
treeed498b394332d888477c75fcb3b12f51bcf70c62 /lib/jsonutil
parent325178506187df037f9a85eb2e09100eb794f4f9 (diff)
btrfssum: Don't emit JSON strings that are too long
Split it, and wrap it in an array.
Diffstat (limited to 'lib/jsonutil')
-rw-r--r--lib/jsonutil/hex_string.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/jsonutil/hex_string.go b/lib/jsonutil/hex_string.go
index 06970ce..3e0b154 100644
--- a/lib/jsonutil/hex_string.go
+++ b/lib/jsonutil/hex_string.go
@@ -40,3 +40,47 @@ func DecodeHexString(r io.RuneScanner, dst io.ByteWriter) error {
}
return dec.Close()
}
+
+func EncodeSplitHexString[T ~[]byte | ~string](w io.Writer, str T, maxStrLen int) error {
+ if maxStrLen <= 0 || len(str) <= maxStrLen/2 {
+ return EncodeHexString(w, str)
+ }
+ var buf [1]byte
+ buf[0] = '['
+ if _, err := w.Write(buf[:]); err != nil {
+ return err
+ }
+ for len(str) > maxStrLen/2 {
+ if err := EncodeHexString(w, str[:maxStrLen/2]); err != nil {
+ return err
+ }
+ str = str[maxStrLen/2:]
+ if len(str) > 0 {
+ buf[0] = ','
+ if _, err := w.Write(buf[:]); err != nil {
+ return err
+ }
+ }
+ }
+ if len(str) > 0 {
+ if err := EncodeHexString(w, str); err != nil {
+ return err
+ }
+ }
+ buf[0] = ']'
+ if _, err := w.Write(buf[:]); err != nil {
+ return err
+ }
+ return nil
+}
+
+func DecodeSplitHexString(r io.RuneScanner, dst io.ByteWriter) error {
+ c, _, _ := r.ReadRune()
+ _ = r.UnreadRune()
+ if c == '"' {
+ return DecodeHexString(r, dst)
+ }
+ return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error {
+ return DecodeHexString(r, dst)
+ })
+}