ohler55 / ojg

Optimized JSON for Go
MIT License
839 stars 50 forks source link

Parse '1,2,3' should not succeed #127

Closed zzzzwc closed 1 year ago

zzzzwc commented 1 year ago

There may be a bug of oj.ParseString:

func TestOjgParse(t *testing.T) {
    v, err := oj.ParseString("1,2,3\n")
    fmt.Println(v)
    fmt.Println(err)
    fmt.Println("----------")
    v, err = oj.ParseString("1,2,a\n")
    fmt.Println(v)
    fmt.Println(err)
}

It outputs:

=== RUN   TestOjgParse
1
<nil>
----------
<nil>
unexpected character 'a' at 1:5
--- PASS: TestOjgParse (0.00s)
PASS

the string '1,2,3\n' isn't to be a JSON either, but ParseString didn't return an error.

ohler55 commented 1 year ago

The comma should cause an error to be returned. I'll debug it this evening.

What is happening is that parsing is reading the valid JSON of 1 but not giving an error on the comma and continues parsing which it should not do. In the second case you can see that the parser continues and errors on the a but would probably be okay with "a" which is a valid json.

ohler55 commented 1 year ago

Please try the no-comma-error branch.

zzzzwc commented 1 year ago

It fixed: the code

func TestOjgParse(t *testing.T) {
    v, err := oj.ParseString("1,2,3\n")
    fmt.Println(v)
    fmt.Println(err)
    fmt.Println("----------")
    v, err = oj.ParseString("1,2,a\n")
    fmt.Println(v)
    fmt.Println(err)
}

outputs

=== RUN   TestOjgParse
<nil>
unexpected comma at 1:2
----------
<nil>
unexpected comma at 1:2
--- PASS: TestOjgParse (0.00s)
PASS

as expected. Thank you!