]> Cypherpunks repositories - keks.git/commitdiff
Ability to dump as JSON
authorSergey Matveev <stargrave@stargrave.org>
Sun, 13 Apr 2025 10:39:43 +0000 (13:39 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 13 Apr 2025 10:39:43 +0000 (13:39 +0300)
go/blob.go
go/cmd/pp/main.go
go/hexlet.go
go/magic.go
go/raw.go

index ee45abb89b08366b35424f7ce5bdb81d7f81e2952f7b4309e1b6b24adddc3878..0b7c1c6739b656c062293a36b8a0ab862bf83cb1a687b30b41c40be995c00b74 100644 (file)
@@ -49,6 +49,10 @@ func (blob *BlobChunked) String() string {
        return fmt.Sprintf("BLOB(%d, %d)", blob.ChunkLen, blob.Len())
 }
 
+func (blob BlobChunked) MarshalJSON() ([]byte, error) {
+       return []byte(`"` + blob.String() + `"`), nil
+}
+
 type BlobReader struct {
        R        io.Reader
        ChunkLen int64
index 3099959924b26fc2a4cf48751600684aa2b3bdc280807a5675421d618e159455..17895f5378b4e61ce7b05e7337fb8bc59fbb05d469166b353715e800c6f0430e 100644 (file)
@@ -18,6 +18,7 @@ package main
 import (
        "bufio"
        "encoding/hex"
+       "encoding/json"
        "flag"
        "fmt"
        "io"
@@ -47,6 +48,7 @@ value of the signature's sid.
 }
 
 var (
+       dumpJSON  = flag.Bool("json", false, "Dump as JSON")
        MaxStrLen = flag.Uint("max-str-len", 64,
                "Maximal string length to print")
        MaxParseCycles = flag.Uint64("max-parse-cycles", 1<<63,
@@ -276,10 +278,23 @@ func main() {
                        }
                        log.Fatal(err)
                }
-               printer(ctx.Iter(), []string{}, 1, false, false)
+               if *dumpJSON {
+                       v, err := ctx.Unmarshal()
+                       if err != nil {
+                               log.Fatal(err)
+                       }
+                       e := json.NewEncoder(os.Stdout)
+                       e.SetIndent("", "  ")
+                       err = e.Encode(&v)
+                       if err != nil {
+                               log.Fatal(err)
+                       }
+               } else {
+                       printer(ctx.Iter(), []string{}, 1, false, false)
+               }
                off = ctx.Read
        }
-       if !*NoTotals {
+       if !*NoTotals && !*dumpJSON {
                fmt.Println(off, "bytes")
        }
 }
index ed15291c18236f51e1c016b8eea32e1020bc95a207090f1c5379efc005a6743a..963cbc02b51708fa7760ebd652ef839ff877d63feeae918e9e442afb9611e894 100644 (file)
@@ -16,3 +16,7 @@ func (h *Hexlet) UUID() (u uuid.UUID) {
 func (h *Hexlet) IP() (ip net.IP) {
        return net.IP(h[:])
 }
+
+func (h *Hexlet) MarshalJSON() ([]byte, error) {
+       return []byte(`"HEXLET[` + h.UUID().String() + `]"`), nil
+}
index 3016347fd3b782cefa8a9eb698e36ca746fc22807201a3aac994e2833d3e5425..9336c327ba3a680fdbc9442b1675da0b53499d6c879b6631de1e6e53cb4be654 100644 (file)
@@ -40,3 +40,7 @@ func StripMagic(data []byte) (Magic, []byte) {
        }
        return d.Iter().Magic(), data[d.Read:]
 }
+
+func (m Magic) MarshalJSON() ([]byte, error) {
+       return []byte(`"MAGIC[` + m + `]"`), nil
+}
index 22555bf55df319afdc36105ea70bd18f77235498fb5315e55b35ea966870d224..965974639c1394bb37baf292d0d48cf3877f2e06991d0073c9d493028a99ab08 100644 (file)
--- a/go/raw.go
+++ b/go/raw.go
@@ -1,3 +1,9 @@
 package keks
 
+import "encoding/hex"
+
 type Raw []byte
+
+func (raw Raw) MarshalJSON() ([]byte, error) {
+       return []byte(`"RAW[` + hex.EncodeToString(raw) + `]"`), nil
+}