Skip to content

Commit 7162d18

Browse files
committed
add Event MarshalJSON support
1 parent 9577570 commit 7162d18

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

event.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package esl
33
import (
44
"bufio"
55
"bytes"
6+
"encoding/json"
67
"fmt"
78
"io"
89
"log/slog"
10+
"maps"
911
"net/url"
1012
"slices"
1113
"strconv"
@@ -106,6 +108,17 @@ func (e Event) String() string {
106108
return wstr(e)
107109
}
108110

111+
// MarshalJSON is a Go function that marshals the Event to JSON.
112+
func (e Event) MarshalJSON() ([]byte, error) {
113+
h := e.headers
114+
if len(e.body) > 0 {
115+
h = maps.Clone(h)
116+
h["_body"] = string(e.body)
117+
}
118+
119+
return json.Marshal(h)
120+
}
121+
109122
// LogValue returns the log value of the Event.
110123
//
111124
// It returns a slog.Value that contains the name and sequence of the Event.

0 commit comments

Comments
 (0)