Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza/operator/parser/json] json parse error #16261

Closed
maokitty opened this issue Nov 11, 2022 · 3 comments
Closed

[pkg/stanza/operator/parser/json] json parse error #16261

maokitty opened this issue Nov 11, 2022 · 3 comments
Labels
bug Something isn't working needs triage New item requiring triage pkg/stanza Stale

Comments

@maokitty
Copy link

maokitty commented Nov 11, 2022

Component(s)

pkg/stanza

What happened?

Description

if the content contains '\n' ,the parser will report an error

Steps to Reproduce

func main() {
	str :="{\"body\":\"a \n b\"}"
	p :=Parser{json:jsoniter.ConfigFastest}
	parse, e := p.parse(str)
	if e!=nil {
		fmt.Println(e)
	}else{
		fmt.Println(parse)
	}
}
type Parser struct {
	json jsoniter.API
}
func (j *Parser) parse(value interface{}) (interface{}, error) {
	var parsedValue map[string]interface{}
	switch m := value.(type) {
	case string:
		err := j.json.UnmarshalFromString(m, &parsedValue)
		if err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("type %T cannot be parsed as JSON", value)
	}
	return parsedValue, nil
}

Expected Result

map[body:a
b]

Actual Result

ReadString: invalid control character found: 10, error found in #9 byte of ...|{"body":"a
b"}|..., bigger context ...|{"body":"a
b"}|...

Collector version

0.61.0

Environment information

Environment

go version go1.18.4 darwin/amd64

OpenTelemetry Collector configuration

No response

Log output

No response

Additional context

No response

@maokitty maokitty added bug Something isn't working needs triage New item requiring triage labels Nov 11, 2022
@github-actions
Copy link
Contributor

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@djaglowski
Copy link
Member

I believe this is the correct behavior. According to the json specification, newline characters must be escaped.

You can read more about this here. The first comment on the accepted answer states it quite well:

... the JSON spec at [ietf.org/rfc/rfc4627.txt](http://www.ietf.org/rfc/rfc4627.txt) contains this sentence in section 2.5: "All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)." Since a newline is a control character, it must be escaped.

@github-actions
Copy link
Contributor

This issue has been inactive for 60 days. It will be closed in 60 days if there is no activity. To ping code owners by adding a component label, see Adding Labels via Comments, or if you are unsure of which component this issue relates to, please ping @open-telemetry/collector-contrib-triagers. If this issue is still relevant, please ping the code owners or leave a comment explaining why it is still relevant. Otherwise, please close it.

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage New item requiring triage pkg/stanza Stale
Projects
None yet
Development

No branches or pull requests

2 participants