Ignore UTF8 BOM in JSON parser (#4099)
(cherry picked from commit 9647ea88ea
)
This commit is contained in:
parent
ae9e77dab8
commit
f21f31a08a
|
@ -12,6 +12,10 @@ import (
|
|||
"github.com/influxdata/telegraf/metric"
|
||||
)
|
||||
|
||||
var (
|
||||
utf8BOM = []byte("\xef\xbb\xbf")
|
||||
)
|
||||
|
||||
type JSONParser struct {
|
||||
MetricName string
|
||||
TagKeys []string
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue