From a00e8b1edb2531a861bde94a34b66e581a88a495cdda852835600c041b131c89 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 13 Apr 2025 13:39:43 +0300 Subject: [PATCH] Ability to dump as JSON --- go/blob.go | 4 ++++ go/cmd/pp/main.go | 19 +++++++++++++++++-- go/hexlet.go | 4 ++++ go/magic.go | 4 ++++ go/raw.go | 6 ++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/go/blob.go b/go/blob.go index ee45abb..0b7c1c6 100644 --- a/go/blob.go +++ b/go/blob.go @@ -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 diff --git a/go/cmd/pp/main.go b/go/cmd/pp/main.go index 3099959..17895f5 100644 --- a/go/cmd/pp/main.go +++ b/go/cmd/pp/main.go @@ -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") } } diff --git a/go/hexlet.go b/go/hexlet.go index ed15291..963cbc0 100644 --- a/go/hexlet.go +++ b/go/hexlet.go @@ -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 +} diff --git a/go/magic.go b/go/magic.go index 3016347..9336c32 100644 --- a/go/magic.go +++ b/go/magic.go @@ -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 +} diff --git a/go/raw.go b/go/raw.go index 22555bf..9659746 100644 --- 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 +} -- 2.48.1