summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsitem
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-30 02:36:25 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-08-17 22:50:57 -0600
commit13b65d51ffc9768464e8330fdb73b6febd7410af (patch)
tree271fe78abc75573a330353632171284ccf2ecd47 /lib/btrfs/btrfsitem
parenteb4e771e2a99edeb5cc3a1511ab611480f98255d (diff)
json: Implement a bunch of streaming JSON encoding/decoding
Diffstat (limited to 'lib/btrfs/btrfsitem')
-rw-r--r--lib/btrfs/btrfsitem/item_extentcsum.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/btrfs/btrfsitem/item_extentcsum.go b/lib/btrfs/btrfsitem/item_extentcsum.go
index b35d333..eedd044 100644
--- a/lib/btrfs/btrfsitem/item_extentcsum.go
+++ b/lib/btrfs/btrfsitem/item_extentcsum.go
@@ -5,7 +5,11 @@
package btrfsitem
import (
+ "encoding/hex"
"fmt"
+ "io"
+
+ "git.lukeshu.com/go/lowmemjson"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
)
@@ -43,3 +47,33 @@ func (o ExtentCSum) MarshalBinary() ([]byte, error) {
}
return dat, nil
}
+
+var (
+ _ lowmemjson.Encodable = ExtentCSum{}
+)
+
+func (o ExtentCSum) EncodeJSON(w io.Writer) error {
+ if _, err := fmt.Fprintf(w, `{"ChecksumSize":%d,"Sums":[`, o.ChecksumSize); err != nil {
+ return err
+ }
+ for i, sum := range o.Sums {
+ if i > 0 {
+ if _, err := w.Write([]byte(",")); err != nil {
+ return err
+ }
+ }
+ if _, err := w.Write([]byte(`"`)); err != nil {
+ return err
+ }
+ if _, err := hex.NewEncoder(w).Write(sum[:o.ChecksumSize]); err != nil {
+ return err
+ }
+ if _, err := w.Write([]byte(`"`)); err != nil {
+ return err
+ }
+ }
+ if _, err := w.Write([]byte(`]}`)); err != nil {
+ return err
+ }
+ return nil
+}