Created
June 18, 2021 15:03
-
-
Save erichutchins/62665586246555ac885a4a3cf8d8ea23 to your computer and use it in GitHub Desktop.
Demonstrations of various invalid JSON strings and how simdjson OnDemand vs DOM API’s perform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<fstream> | |
#include "simdjson.h" | |
using namespace simdjson; | |
void process_dom(padded_string& input, std::string_view jsonpointer, std::string_view label) { | |
dom::parser parser; | |
dom::element elem; | |
dom::object val; | |
simdjson::error_code err; | |
std::cout << label << ";dom;"; | |
err = parser.parse(input).get(elem); | |
if (err != simdjson::SUCCESS) { | |
std::cout << "iterate error;" << err << std::endl; | |
return; | |
} | |
err = elem.at_pointer(jsonpointer).get(val); | |
if (err != simdjson::SUCCESS) { | |
std::cout << "at_pointer error;" << err << std::endl; | |
} | |
else { | |
try { | |
std::cout << "at_pointer success;" << val << std::endl; | |
} catch (simdjson_error &e) { | |
std::cout << "*but* error reading val;" << e.what() << std::endl; | |
} | |
} | |
return; | |
} | |
void process_ondemand(padded_string& input, std::string_view jsonpointer, std::string_view label) { | |
ondemand::parser parser; | |
ondemand::document doc; | |
ondemand::value val; | |
simdjson::error_code err; | |
std::cout << label << ";ondemand;"; | |
err = parser.iterate(input).get(doc); | |
if (err != simdjson::SUCCESS) { | |
std::cout << "iterate error;" << err << std::endl; | |
return; | |
} | |
err = doc.at_pointer(jsonpointer).get(val); | |
if (err != simdjson::SUCCESS) { | |
std::cout << "at_pointer error;" << err << std::endl; | |
} | |
else { | |
try { | |
std::cout << "at_pointer success;" << val << std::endl; | |
} catch (simdjson_error &e) { | |
std::cout << "*but* error reading val;" << e.what() << std::endl; | |
} | |
} | |
return; | |
} | |
int main(int argc, char* argv[]) { | |
// Invalid escape sequence in a key prior to the jsonpointer | |
// Dom will error; On Demand will work | |
auto invalid_escape_key = R"( {"hello": [0,1,2,3], "te\est": "foo", "bool": true, "num":1234, "success":"yes"} )"_padded; | |
process_dom(invalid_escape_key, "/success", "invalid escape in key before jsonpointer"); | |
process_ondemand(invalid_escape_key, "/success", "invalid escape in key before jsonpointer"); | |
// Invalid escape sequence in a value prior to the jsonpointer | |
// Dom will error; On Demand will work | |
auto invalid_escape_value = R"( {"hello": [0,1,2,3], "test": "fo\eo", "bool": true, "num":1234, "success":"yes"} )"_padded; | |
process_dom(invalid_escape_value, "/success", "invalid escape in value before jsonpointer"); | |
process_ondemand(invalid_escape_value, "/success", "invalid escape in value before jsonpointer"); | |
// Invalid escape sequence in value at the jsonpointer | |
// Both will error | |
auto invalid_escape_value_at_jp = R"( {"hello": [0,1,2,3], "test": "foo", "bool": true, "num":1234, "success":"y\es"} )"_padded; | |
process_dom(invalid_escape_value_at_jp, "/success", "invalid escape in value at the jsonpointer"); | |
process_ondemand(invalid_escape_value_at_jp, "/success", "invalid escape in value at the jsonpointer"); | |
// The root object is missing the closing } curly bracket | |
// Both will error | |
auto unclosed_object = R"( {"test": "foo", "bool": true, "num":1234, "success":"yes" )"_padded; | |
process_dom(unclosed_object, "/success", "root object is not closed"); | |
process_ondemand(unclosed_object, "/success", "root object is not closed"); | |
// An array is missing its closing ] bracket at a position before the jsonpointer | |
// Both will error | |
auto missing_bracket_before = R"( {"hello": [0,1,2,3, "test": "foo", "bool": true, "num":1234, "success":"yes"} )"_padded; | |
process_dom(missing_bracket_before, "/success", "missing bracket before jsonpointer"); | |
process_ondemand(missing_bracket_before, "/success", "missing bracket before jsonpointer"); | |
// An array is missing its closing ] bracket at a position *after* the jsonpointer | |
// Dom will error; On Demand will work | |
auto missing_bracket_after = R"( {"test": "foo", "bool": true, "num":1234, "success":"yes", "hello":[0,1,2,3} )"_padded; | |
process_dom(missing_bracket_after, "/success", "missing bracket after jsonpointer"); | |
process_ondemand(missing_bracket_after, "/success", "missing bracket after jsonpointer"); | |
return EXIT_SUCCESS; | |
} |
Author
erichutchins
commented
Jun 18, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment