json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.43k stars 1.03k forks source link

Cases where jsoniter.Valid() has different results from json.Valid() #540

Open WillAbides opened 3 years ago

WillAbides commented 3 years ago

I ran the parsing test cases from https://github.com/nst/JSONTestSuite against both json.Valid() and jsoniter.Valid(). There are several cases where they give differing results.

This workaround adapted from what @JeckieLI provided in #519 addresses a lot of the issues:

func validWorkaround(data []byte) bool {
    data = append(data, '\n')
    iter := jsoniter.ConfigCompatibleWithStandardLibrary.BorrowIterator(data)
    defer jsoniter.ConfigCompatibleWithStandardLibrary.ReturnIterator(iter)
    iter.Skip()
    if iter.Error != nil {
        return false
    }
    iter.WhatIsNext()
    return iter.Error == io.EOF
}

The table below shows the cases with differing results. I bolded the rows where the workaround doesn't address the problem. The test cases are named such that the ones starting with "y" are valid json, the ones starting with "n" are invalid json and the ones starting with "i" are ambiguous according the the relevant RFCs.

json.Valid() json.Valid validWorkaround test case
true false false i_number_huge_exp.json
true false false i_number_neg_int_huge_exp.json
true false false i_number_pos_double_huge_exp.json
true false false i_number_real_neg_overflow.json
true false false i_number_real_pos_overflow.json
false true false n_array_comma_after_close.json
false true false n_array_extra_close.json
false true false n_multidigit_number_then_00.json
false true true nnumber-01.json
false true true n_number_neg_int_starting_with_zero.json
false true true n_number_neg_real_without_int_part.json
false true false n_object_trailing_comment.json
false true false n_object_trailing_comment_open.json
false true false n_object_trailing_comment_slash_open.json
false true false n_object_trailing_comment_slash_open_incomplete.json
false true false n_object_with_trailing_garbage.json
false true false n_string_with_trailing_garbage.json
false true false n_structure_array_trailing_garbage.json
false true false n_structure_array_with_extra_array_close.json
false true false n_structure_close_unopened_array.json
false true false n_structure_double_array.json
false true false n_structure_number_with_trailing_garbage.json
false true false n_structure_object_followed_by_closing_object.json
false true false n_structure_object_with_trailing_garbage.json
false true false n_structuretrailing#.json
true false true y_structure_lonely_int.json
true false true y_structure_lonely_negative_real.json