forked from simdjson/simdjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme_examples.cpp
More file actions
144 lines (117 loc) · 3.98 KB
/
readme_examples.cpp
File metadata and controls
144 lines (117 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include "simdjson.h"
using namespace std;
using namespace simdjson;
void document_parse_error_code() {
cout << __func__ << endl;
auto [doc, error] = document::parse("[ 1, 2, 3 ]"_padded);
if (error) { cerr << "Error: " << error << endl; exit(1); }
cout << doc << endl;
}
void parser_parse_error_code() {
cout << __func__ << endl;
// Allocate a parser big enough for all files
document::parser parser;
// 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 << endl; exit(1); }
cout << doc << endl;
}
}
void parser_parse_many_error_code() {
cout << __func__ << endl;
// Read files with the parser
auto json = "[1, 2, 3] true [ true, false ]"_padded;
cout << "Parsing " << json.data() << " ..." << endl;
document::parser parser;
for (auto [doc, error] : parser.parse_many(json)) {
if (error) { cerr << "Error: " << error << endl; exit(1); }
cout << doc << endl;
}
}
void parser_parse_max_capacity() {
cout << __func__ << endl;
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); }
cout << doc << endl;
}
}
void parser_parse_fixed_capacity() {
cout << __func__ << endl;
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); }
cout << doc << endl;
}
}
#if SIMDJSON_EXCEPTIONS
void document_parse_exception() {
cout << __func__ << endl;
cout << document::parse("[ 1, 2, 3 ]"_padded) << endl;
}
void document_parse_padded_string() {
cout << __func__ << endl;
auto json = "[ 1, 2, 3 ]"_padded;
cout << document::parse(json) << endl;
}
void document_parse_get_corpus() {
cout << __func__ << endl;
auto json = get_corpus("jsonexamples/small/demo.json");
cout << document::parse(json) << endl;
}
void document_load() {
cout << __func__ << endl;
cout << document::load("jsonexamples/small/demo.json") << endl;
}
void parser_parse_exception() {
cout << __func__ << endl;
// Allocate a parser big enough for all files
document::parser parser;
// 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;
cout << parser.parse(json) << endl;
}
}
void parser_parse_many_exception() {
cout << __func__ << endl;
// Read files with the parser
auto json = "[1, 2, 3] true [ true, false ]"_padded;
cout << "Parsing " << json.data() << " ..." << endl;
document::parser parser;
for (const document &doc : parser.parse_many(json)) {
cout << doc << endl;
}
}
#endif // SIMDJSON_EXCEPTIONS
int main() {
cout << "Running examples." << endl;
document_parse_error_code();
parser_parse_error_code();
parser_parse_many_error_code();
parser_parse_max_capacity();
parser_parse_fixed_capacity();
#if SIMDJSON_EXCEPTIONS
document_parse_exception();
parser_parse_exception();
parser_parse_many_exception();
document_parse_padded_string();
document_parse_get_corpus();
document_load();
#endif // SIMDJSON_EXCEPTIONS
cout << "Ran to completion!" << endl;
return 0;
}