Invalid JSON is unmarshaled correctly
Hello,
When attempting to Unmarshal something like so:
type Dog struct {
Name *string
Color *string
Age *int
Tricks []string
}
jsonStr := `
{
"name" : "Buddy",
"color" : "brindle",
"tricks": ["a","b","c",],
"age": 5,
}`
var dog Dog
payload := &dog
unmarshalErr := ffjson.Unmarshal([]byte(jsonStr), payload)
And generating a type to match this struct, ffjson will happily unmarshal the json even though it has an extra comma after "age":5,
It would seem to be in violation of the following test in: tests/ff_obj_test.go
func TestInvalidTrailingComma(t *testing.T) {
testExpectedError(t,
&fflib.LexerError{},
`{"X":"foo",}`,
&Xobj{})
}
Just curious if this should be regarded as a bug or expected?
Thanks in advance,
@deckarep
It is a bug -- as encoding/json would error, so should ffjson, we want to be a drop-in replacement in as many cases as possible.
Ok thanks for confirming @pquerna. Additionally, the same thing occurs for json arrays, i updated the example.
Confirmed the lexer is correct:
ffl = NewFFLexer([]byte(`{"hello": 1,}`))
toks = scanAll(ffl)
assertTokensEqual(t, []FFTok{
FFTok_left_bracket,
FFTok_string,
FFTok_colon,
FFTok_integer,
FFTok_comma,
FFTok_right_bracket,
FFTok_eof,
}, toks)
So the bug is likely in the parsing phase.