fix(parsers.json_v2): Remove BOM before parsing (#11926)
This commit is contained in:
parent
08c1ce9cb6
commit
7c1d1755d8
|
|
@ -2,16 +2,19 @@ package json_v2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/dimchansky/utfbom"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/internal"
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/metric"
|
"github.com/influxdata/telegraf/metric"
|
||||||
"github.com/influxdata/telegraf/plugins/parsers"
|
"github.com/influxdata/telegraf/plugins/parsers"
|
||||||
"github.com/influxdata/telegraf/plugins/parsers/temporary/json_v2"
|
"github.com/influxdata/telegraf/plugins/parsers/temporary/json_v2"
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parser adheres to the parser interface, contains the parser configuration, and data required to parse JSON
|
// Parser adheres to the parser interface, contains the parser configuration, and data required to parse JSON
|
||||||
|
|
@ -68,6 +71,13 @@ func (p *Parser) Init() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
|
func (p *Parser) Parse(input []byte) ([]telegraf.Metric, error) {
|
||||||
|
reader := strings.NewReader(string(input))
|
||||||
|
body, _ := utfbom.Skip(reader)
|
||||||
|
input, err := io.ReadAll(body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to read body after BOM removal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Only valid JSON is supported
|
// Only valid JSON is supported
|
||||||
if !gjson.Valid(string(input)) {
|
if !gjson.Valid(string(input)) {
|
||||||
return nil, fmt.Errorf("invalid JSON provided, unable to parse")
|
return nil, fmt.Errorf("invalid JSON provided, unable to parse")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue