Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Recover from Parsing Errors and Continue JSON Processing With Ondemand Parser #2347

@Cirnoo

Description

@Cirnoo

Request: Recover from Parsing Errors and Continue JSON Processing

Description

Hi~, I am using simdjson::ondemand::parser to parse JSON data, but sometimes my JSON objects may contain malformed elements (e.g., trailing commas). While I understand that such structures are invalid according to strict JSON specifications, I would like to have an option to skip or recover from errors rather than having the entire parsing process fail.

Some JSON libraries, such as yyjson, provide support for trailing commas or other minor syntax relaxations. My main concern is not just about trailing commas but about having a way to recover from an error and continue parsing the rest of the JSON object.

Example Code

simdjson::ondemand::parser parser;
simdjson::ondemand::document doc;
std::string data = R"({
    "errorObj": {
        "key" : "value", // Trailing comma, invalid JSON
    },
    "extData": {
        "key2" : "some data in here"
    }
})";

auto error = parser.iterate(data).get(doc);
auto root = doc.get_object().value();

// If I try to access `errorObj` first, my code crashes.
// However, if I avoid accessing `errorObj`, the code runs fine.

// try {
//     auto errorObj = root["errorObj"].take_value();
//     auto size = errorObj.count_fields().take_value();
//     std::cout << "error object size = " << size << std::endl;
// } catch (const std::exception & e) {
//     std::cout << "error: " << e.what() << std::endl;
//     // Is there a way to reset the error and continue parsing the rest of the JSON?
// }

try {
    auto extData = root["extData"].take_value();
    auto size = extData.count_fields();
    std::cout << "extData object size = " << size;
} catch (const std::exception & e) {
    std::cout << "error: " << e.what() << std::endl;
}

I hope that the ondemand parser could incorporate some level of error tolerance and recovery capabilities to support more complex and customized scenarios for users.

Thank you for considering this feature request. Your work on simdjson is greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions