Skip to content

Commit

Permalink
Disable HTML escaping during JSON Marshaling
Browse files Browse the repository at this point in the history
By default the golang JSON Marshaller escapes HTML entries. As this is not required for beats this is disabling this option.

* Unification of json marshalling

Closes elastic#2581
  • Loading branch information
ruflin committed May 23, 2017
1 parent 7adc6f0 commit 12b3623
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
28 changes: 28 additions & 0 deletions libbeat/common/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package common

import (
"bytes"
"encoding/json"

"fmt"
)

// JSONEncode encodes the given interface to JSON
func JSONEncode(data interface{}, pretty bool) ([]byte, error) {

buffer := &bytes.Buffer{}
enc := json.NewEncoder(buffer)
enc.SetEscapeHTML(true)

enc.SetIndent("", "")
if pretty {
enc.SetIndent("", " ")
}

err := enc.Encode(data)
if err != nil {
return nil, fmt.Errorf("failed to convert the data to JSON (%v): %#v", err, data)
}

return buffer.Bytes(), nil
}
11 changes: 8 additions & 3 deletions libbeat/common/mapstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/logp"
)

// Event metadata constants. These keys are used within libbeat to identify
Expand Down Expand Up @@ -142,11 +144,14 @@ func (m MapStr) Put(key string, value interface{}) (interface{}, error) {

// StringToPrint returns the MapStr as pretty JSON.
func (m MapStr) StringToPrint() string {
json, err := json.MarshalIndent(m, "", " ")

buffer, err := JSONEncode(m, true)
if err != nil {
return fmt.Sprintf("Not valid json: %v", err)
logp.Err("Fail to convert the event to JSON (%v): %#v", err, m)
return ""
}
return string(json)

return string(buffer)
}

// String returns the MapStr as JSON.
Expand Down
1 change: 0 additions & 1 deletion libbeat/logp/file_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func (rotator *FileRotator) WriteLine(line []byte) error {
}
}

line = append(line, '\n')
_, err := rotator.current.Write(line)
if err != nil {
return err
Expand Down
13 changes: 3 additions & 10 deletions libbeat/outputs/codecs/json/json.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package json

import (
"encoding/json"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
Expand Down Expand Up @@ -38,17 +36,12 @@ func New(pretty bool) *Encoder {
}

func (e *Encoder) Encode(event common.MapStr) ([]byte, error) {
var err error
var serializedEvent []byte

if e.Pretty {
serializedEvent, err = json.MarshalIndent(event, "", " ")
} else {
serializedEvent, err = json.Marshal(event)
}
buffer, err := common.JSONEncode(event, e.Pretty)
if err != nil {
logp.Err("Fail to convert the event to JSON (%v): %#v", err, event)
return nil, err
}

return serializedEvent, err
return buffer, nil
}

0 comments on commit 12b3623

Please sign in to comment.