simdjson/tests/readme_examples.cpp

137 lines
4.0 KiB
C++
Raw Normal View History

2020-02-18 11:59:30 +08:00
#include <iostream>
#include "simdjson.h"
2020-02-18 11:59:30 +08:00
using namespace std;
using namespace simdjson;
void document_parse_error_code() {
2020-02-25 12:59:38 +08:00
cout << __func__ << endl;
2020-02-18 11:59:30 +08:00
string json("[ 1, 2, 3 ]");
auto [doc, error] = document::parse(json);
2020-03-07 04:14:23 +08:00
if (error) { cerr << "Error: " << error << endl; exit(1); }
if (!doc.print_json(cout)) { exit(1); }
2020-02-18 11:59:30 +08:00
cout << endl;
}
void document_parse_exception() {
2020-02-25 12:59:38 +08:00
cout << __func__ << endl;
2020-02-18 11:59:30 +08:00
string json("[ 1, 2, 3 ]");
document doc = document::parse(json);
if (!doc.print_json(cout)) { exit(1); }
2020-02-18 11:59:30 +08:00
cout << endl;
}
void document_parse_padded_string() {
2020-02-25 12:59:38 +08:00
cout << __func__ << endl;
2020-02-18 11:59:30 +08:00
padded_string json(string("[ 1, 2, 3 ]"));
document doc = document::parse(json);
if (!doc.print_json(cout)) { exit(1); }
2020-02-18 11:59:30 +08:00
cout << endl;
}
void document_parse_get_corpus() {
2020-02-25 12:59:38 +08:00
cout << __func__ << endl;
auto json(get_corpus("jsonexamples/small/demo.json"));
2020-02-18 11:59:30 +08:00
document doc = document::parse(json);
if (!doc.print_json(cout)) { exit(1); }
2020-02-18 11:59:30 +08:00
cout << endl;
}
void document_load() {
cout << __func__ << endl;
document doc = document::load("jsonexamples/small/demo.json");
if (!doc.print_json(cout)) { exit(1); }
cout << endl;
}
2020-02-18 11:59:30 +08:00
void parser_parse() {
2020-02-25 12:59:38 +08:00
cout << __func__ << endl;
2020-02-18 11:59:30 +08:00
// Allocate a parser big enough for all files
document::parser parser;
simdjson::error_code capacity_error = parser.set_capacity(1024*1024);
if (capacity_error) { cerr << "Error setting capacity: " << capacity_error << endl; exit(1); }
2020-02-18 11:59:30 +08:00
// Read files with the parser, one by one
for (padded_string json : { string("[1, 2, 3]"), string("true"), string("[ true, false ]") }) {
cout << "Parsing " << json.data() << " ..." << endl;
auto [doc, error] = parser.parse(json);
2020-03-07 04:14:23 +08:00
if (error) { cerr << "Error: " << error << endl; exit(1); }
if (!doc.print_json(cout)) { cerr << "print failed!\n"; exit(1); }
2020-02-18 11:59:30 +08:00
cout << endl;
}
}
2020-03-05 00:42:29 +08:00
void parser_parse_many_error_code() {
cout << __func__ << endl;
// Read files with the parser
padded_string json = string("[1, 2, 3] true [ true, false ]");
cout << "Parsing " << json.data() << " ..." << endl;
document::parser parser;
for (auto [doc, error] : parser.parse_many(json)) {
2020-03-07 04:14:23 +08:00
if (error) { cerr << "Error: " << error << endl; exit(1); }
2020-03-05 00:42:29 +08:00
if (!doc.print_json(cout)) { exit(1); }
cout << endl;
}
}
void parser_parse_many_exception() {
cout << __func__ << endl;
// Read files with the parser
padded_string json = string("[1, 2, 3] true [ true, false ]");
cout << "Parsing " << json.data() << " ..." << endl;
document::parser parser;
for (const document &doc : parser.parse_many(json)) {
if (!doc.print_json(cout)) { exit(1); }
cout << endl;
}
}
void parser_parse_max_capacity() {
int argc = 2;
padded_string argv[] { string("[1,2,3]"), string("true") };
document::parser parser(1024*1024); // Set max capacity to 1MB
for (int i=0;i<argc;i++) {
auto [doc, error] = parser.parse(argv[i]);
if (error == CAPACITY) { cerr << "JSON files larger than 1MB are not supported!" << endl; exit(1); }
if (error) { cerr << error << endl; exit(1); }
doc.print_json(cout);
}
}
void parser_parse_fixed_capacity() {
int argc = 2;
padded_string argv[] { string("[1,2,3]"), string("true") };
document::parser parser(0); // This parser is not allowed to auto-allocate
auto alloc_error = parser.set_capacity(1024*1024);
if (alloc_error) { cerr << alloc_error << endl; exit(1); }; // Set initial capacity to 1MB
for (int i=0;i<argc;i++) {
auto [doc, error] = parser.parse(argv[i]);
if (error == CAPACITY) { cerr << "JSON files larger than 1MB are not supported!" << endl; exit(1); }
if (error) { cerr << error << endl; exit(1); }
doc.print_json(cout);
}
}
2020-02-18 11:59:30 +08:00
int main() {
cout << "Running examples." << endl;
document_parse_error_code();
document_parse_exception();
document_parse_padded_string();
document_parse_get_corpus();
document_load();
2020-02-18 11:59:30 +08:00
parser_parse();
2020-03-05 00:42:29 +08:00
parser_parse_many_error_code();
parser_parse_many_exception();
parser_parse_max_capacity();
parser_parse_fixed_capacity();
2020-02-18 11:59:30 +08:00
cout << "Ran to completion!" << endl;
return 0;
}