ffjson icon indicating copy to clipboard operation
ffjson copied to clipboard

Invalid JSON is unmarshaled correctly

Open deckarep opened this issue 9 years ago • 3 comments

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

deckarep avatar Jun 07 '16 00:06 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.

pquerna avatar Jun 07 '16 02:06 pquerna

Ok thanks for confirming @pquerna. Additionally, the same thing occurs for json arrays, i updated the example.

deckarep avatar Jun 07 '16 03:06 deckarep

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.

deckarep avatar Jun 07 '16 03:06 deckarep