danielaparker / jsoncons

A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
https://danielaparker.github.io/jsoncons
Other
699 stars 158 forks source link

json parser runtime error #462

Closed vksinghbhu2 closed 11 months ago

vksinghbhu2 commented 11 months ago

I have compiled jsoncons library in C++20. I see integer/float value reading error while reading file ".\jsoncons_examples\output\booklist2.json"

Describe the bug

Parser should also read integer value .

In the given sample "price" value is not being read by parser. While reading integer value program is throwing exception in file json_parser.hpp at line number 1113. I have also attached image where error is occurring.

[ { "author": "Haruki Murakami", "category": "Fiction", "date": "2006-01-03", "isbn": "1400079276", "price": 13.45, "title": "Kafka on the Shore" }, { "author": "Charles Bukowski", "category": "Fiction", "date": "2004-07-08", "isbn": "1852272007", "price": 22.48, "ratings": { "*****": 4 }, "title": "Pulp" }, { "author": "Haruki Murakami", "category": "Fiction", "date": "2002-04-09", "isbn": "037571894X", "price": 9.01, "title": "A Wild Sheep Chase: A Novel" }, { "author": "George Crile", "category": "History", "date": "2007-11-06", "isbn": "0802143415", "price": 10.5, "title": "Charlie Wilson's War" } ]

Enumerate the steps to reproduce the bug

Run given sample code.

C+++ code

void read_nested_objects_to_basic_json() { std::ifstream is("C:\vsts\ABI\Src\jsoncons_examples\output\booklist2.json"); assert(is);

json_stream_cursor cursor(is);

json_decoder<json> decoder;
for (; !cursor.done(); cursor.next())
{
    const auto& event = cursor.current();
    switch (event.event_type())
    {
        case staj_event_type::begin_array:
        {
            std::cout << event.event_type() << " " << "\n";
            break;
        }
        case staj_event_type::end_array:
        {
            std::cout << event.event_type() << " " << "\n";
            break;
        }
        case staj_event_type::begin_object:
        {
            std::cout << event.event_type() << " " << "\n";
            cursor.read_to(decoder);
            json j = decoder.get_result();
            std::cout << pretty_print(j) << "\n";
            break;
        }
        default:
        {
            std::cout << "Unhandled event type: " << event.event_type() << " " << "\n";
            break;
        }
    }
}

}

Include a small, self-contained example if possible

What compiler, architecture, and operating system? os - windows 10 compiler - visual studio visual c++ version 2022

**What jsoncons library version? Latest release 0.171.0

parse-error

danielaparker commented 11 months ago

I'm unable to replicate this issue, using Visual Studio 2022 (v143) and ISO C++20 Standard (/std:c++20) on Windows 11. In my tests, your example gives the expected results. I tried both reading from the file jsoncons_examples\output\booklist2.json, and copying and pasting your JSON into a string, and reading from that.

Could you try your test again, but reading from a string, i.e., using

std::string data = R"(
[
{
"author": "Haruki Murakami",
"category": "Fiction",
"date": "2006-01-03",
"isbn": "1400079276",
"price": 13.45,
"title": "Kafka on the Shore"
},
{
"author": "Charles Bukowski",
"category": "Fiction",
"date": "2004-07-08",
"isbn": "1852272007",
"price": 22.48,
"ratings": {
"*****": 4
},
"title": "Pulp"
},
{
"author": "Haruki Murakami",
"category": "Fiction",
"date": "2002-04-09",
"isbn": "037571894X",
"price": 9.01,
"title": "A Wild Sheep Chase: A Novel"
},
{
"author": "George Crile",
"category": "History",
"date": "2007-11-06",
"isbn": "0802143415",
"price": 10.5,
"title": "Charlie Wilson's War"
}
])";

std::istringstream is(data);
vksinghbhu2 commented 11 months ago

Issue is resolved. Thanks.