Implement MarshalJSON and UnmarshalJSON for tag.T, update relayinfo marshaling comments, and adjust tests for consistent JSON handling.

This commit is contained in:
2025-08-23 02:26:47 +01:00
parent ddb4c486cb
commit 0f7f3017ea
3 changed files with 21 additions and 2 deletions

View File

@@ -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) {

View File

@@ -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
}

View File

@@ -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
}