simdjson/tests/readme_examples.cpp

71 lines
1.7 KiB
C++
Raw Normal View History

2020-02-18 11:59:30 +08:00
#include <iostream>
#include "simdjson/document.h"
#include "simdjson/jsonioutil.h"
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);
if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); }
doc.print_json(cout);
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);
doc.print_json(cout);
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);
doc.print_json(cout);
cout << endl;
}
void document_parse_get_corpus() {
2020-02-25 12:59:38 +08:00
cout << __func__ << endl;
2020-02-18 11:59:30 +08:00
padded_string json(get_corpus("jsonexamples/small/demo.json"));
document doc = document::parse(json);
doc.print_json(cout);
cout << endl;
}
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;
if (!parser.allocate_capacity(1024*1024)) { exit(1); }
// 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);
if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); }
doc.print_json(cout);
cout << endl;
}
}
int main() {
cout << "Running examples." << endl;
document_parse_error_code();
document_parse_exception();
document_parse_padded_string();
document_parse_get_corpus();
parser_parse();
cout << "Ran to completion!" << endl;
return 0;
}