diff --git a/pkg/encoders/event/event_test.go b/pkg/encoders/event/event_test.go index 5d2db20..3ad2503 100644 --- a/pkg/encoders/event/event_test.go +++ b/pkg/encoders/event/event_test.go @@ -43,7 +43,7 @@ func TestMarshalJSONUnmarshalJSON(t *testing.T) { // b, err := ev.MarshalJSON() var err error var b []byte - if b, err = json.Marshal(ev); chk.E(err) { + if b, err = ev.MarshalJSON(); chk.E(err) { t.Fatal(err) } var bc []byte @@ -53,7 +53,7 @@ func TestMarshalJSONUnmarshalJSON(t *testing.T) { t.Fatal(err) } var b2 []byte - if b2, err = json.Marshal(ev2); err != nil { + if b2, err = ev2.MarshalJSON(); err != nil { t.Fatal(err) } if !utils.FastEqual(bc, b2) { diff --git a/pkg/encoders/tag/tag.go b/pkg/encoders/tag/tag.go index e946fb6..064392d 100644 --- a/pkg/encoders/tag/tag.go +++ b/pkg/encoders/tag/tag.go @@ -45,6 +45,16 @@ func (t *T) Marshal(dst []byte) (b []byte) { return dst } +// MarshalJSON encodes a tag.T as standard minified JSON array of strings. +// +// Warning: this will mangle the output if the tag fields contain <, > or & +// characters. do not use json.Marshal in the hopes of rendering tags verbatim +// in an event as you will have a bad time. +func (t *T) MarshalJSON() (b []byte, err error) { + b = t.Marshal(nil) + return +} + // Unmarshal decodes a standard minified JSON array of strings to a tags.T. // // Call bufpool.PutBytes(b) to return the buffer to the bufpool after use. @@ -72,3 +82,8 @@ func (t *T) Unmarshal(b []byte) (r []byte, err error) { } return } + +func (t *T) UnmarshalJSON(b []byte) (err error) { + _, err = t.Unmarshal(b) + return +} diff --git a/pkg/protocol/relayinfo/types.go b/pkg/protocol/relayinfo/types.go index a50991b..0670602 100644 --- a/pkg/protocol/relayinfo/types.go +++ b/pkg/protocol/relayinfo/types.go @@ -291,6 +291,8 @@ func NewInfo(inf *T) (info *T) { func (ri *T) Clone() (r2 *T, err error) { r2 = new(T) var b []byte + // beware, this will escape <, > and & to unicode escapes but that should be + // ok since this data is not signed in events until after it is marshaled. if b, err = json.Marshal(ri); chk.E(err) { return } @@ -330,6 +332,8 @@ func (ri *T) Save(filename string) (err error) { return } var b []byte + // beware, this will escape <, > and & to unicode escapes but that should be + // ok since this data is not signed in events until after it is marshaled. if b, err = json.MarshalIndent(ri, "", " "); chk.E(err) { return }