Skip to content

Instantly share code, notes, and snippets.

@erichutchins
Created June 18, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichutchins/62665586246555ac885a4a3cf8d8ea23 to your computer and use it in GitHub Desktop.
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
#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;
}
@erichutchins
Copy link
Author

; c++ -o test test.cpp simdjson.cpp
; ./test | tabulate -s “;” -f fancy_grid
╒════════════════════════════════════════════╤══════════╤════════════════════╤════════════════════════════════════════════════════════════════════════════════════════════════════════╤════════════════════════════════╕
│ invalid escape in key before jsonpointer   │ dom      │ iterate error      │ Problem while parsing a string                                                                         │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ invalid escape in key before jsonpointer   │ ondemand │ at_pointer success │ "yes"                                                                                                  │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ invalid escape in value before jsonpointer │ dom      │ iterate error      │ Problem while parsing a string                                                                         │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ invalid escape in value before jsonpointer │ ondemand │ at_pointer success │ "yes"                                                                                                  │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ invalid escape in value at the jsonpointer │ dom      │ iterate error      │ Problem while parsing a string                                                                         │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ invalid escape in value at the jsonpointer │ ondemand │ at_pointer success │ *but* error reading val                                                                                │ Problem while parsing a string │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ root object is not closed                  │ dom      │ iterate error      │ The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc. │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ root object is not closed                  │ ondemand │ at_pointer error   │ The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc. │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ missing bracket before jsonpointer         │ dom      │ iterate error      │ The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc. │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ missing bracket before jsonpointer         │ ondemand │ at_pointer error   │ The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc. │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ missing bracket after jsonpointer          │ dom      │ iterate error      │ The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc. │                                │
├────────────────────────────────────────────┼──────────┼────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────┤
│ missing bracket after jsonpointer          │ ondemand │ at_pointer success │ "yes"                                                                                                  │                                │
╘════════════════════════════════════════════╧══════════╧════════════════════╧════════════════════════════════════════════════════════════════════════════════════════════════════════╧════════════════════════════════╛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment