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

Parser does not fully validate the JSON #88

Open
mna opened this issue Jan 20, 2023 · 1 comment
Open

Parser does not fully validate the JSON #88

mna opened this issue Jan 20, 2023 · 1 comment

Comments

@mna
Copy link

mna commented Jan 20, 2023

Hello,

I was checking out fastjson and noticed that some invalid JSON strings were accepted without error, e.g. the following program:

func main() {
	s := `"foo
bar"`
	err := fastjson.Validate(s)
	fmt.Printf(">>> Validate: %v\n", err)
	var p fastjson.Parser
	v, err := p.Parse(s)
	fmt.Printf(">>> Parse: %s - %v\n", v.GetStringBytes(), err)

	s = `"ab\c\de"`
	err = fastjson.Validate(s)
	fmt.Printf(">>> Validate: %v\n", err)
	v, err = p.Parse(s)
	fmt.Printf(">>> Parse: %s - %v\n", v.GetStringBytes(), err)
}

// prints:
// >>> Validate: cannot parse JSON: string cannot contain control char 0x0A; unparsed tail: ""
// >>> Parse: foo
// bar - <nil>
// >>> Validate: cannot parse JSON: cannot parse string: unknown escape sequence \c; unparsed tail: ""
// >>> Parse: ab\c\de - <nil>

That is, calling fastjson.Validate properly catches the error, but Parser.Parse doesn't. Is that to be expected (the README does mention "Validates the parsed JSON unlike ...")? Having to call Validate prior to parsing any value to ensure its correctness would obviously make the performance benefits much less interesting, but maybe I'm missing something.

Thanks,
Martin

@vearutop
Copy link

vearutop commented Feb 2, 2023

Another example:

package main

import (
	"fmt"

	"github.com/valyala/fastjson"
)

func main() {
	v, err := fastjson.ParseBytes([]byte(`[127.0.0.1]`))
	if err != nil {
		fmt.Println("failed:", err.Error())
	} else {
		fmt.Println("passed:", v.Type())
	}

	err = fastjson.ValidateBytes([]byte(`[127.0.0.1]`))
	if err != nil {
		fmt.Println("validation failed:", err.Error())
	}

	// Output:
	// passed: array
	// validation failed: cannot parse JSON: cannot parse array: missing ',' after array value; unparsed tail: ".0.1]"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants