Skip to content

Commit

Permalink
Ignore UTF8 BOM in JSON parser (influxdata#4099)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeiners88 authored and arkady-emelyanov committed May 18, 2018
1 parent 1278a8d commit 1f07f47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugins/parsers/json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/influxdata/telegraf/metric"
)

var (
utf8BOM = []byte("\xef\xbb\xbf")
)

type JSONParser struct {
MetricName string
TagKeys []string
Expand Down Expand Up @@ -68,6 +72,7 @@ func (p *JSONParser) parseObject(metrics []telegraf.Metric, jsonOut map[string]i

func (p *JSONParser) Parse(buf []byte) ([]telegraf.Metric, error) {
buf = bytes.TrimSpace(buf)
buf = bytes.TrimPrefix(buf, utf8BOM)
if len(buf) == 0 {
return make([]telegraf.Metric, 0), nil
}
Expand Down
12 changes: 12 additions & 0 deletions plugins/parsers/json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,15 @@ func TestParseArrayWithTagKeys(t *testing.T) {
"othertag": "baz",
}, metrics[1].Tags())
}

var jsonBOM = []byte("\xef\xbb\xbf[{\"value\":17}]")

func TestHttpJsonBOM(t *testing.T) {
parser := JSONParser{
MetricName: "json_test",
}

// Most basic vanilla test
_, err := parser.Parse(jsonBOM)
assert.NoError(t, err)
}

0 comments on commit 1f07f47

Please sign in to comment.