-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest_simdjson.cpp
More file actions
91 lines (76 loc) · 2.3 KB
/
test_simdjson.cpp
File metadata and controls
91 lines (76 loc) · 2.3 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
#include <iostream>
#include <libnotify.hpp>
#include "simdjson/jsonparser.h"
#include <sstream>
#include <unistd.h>
#include <fstream>
using namespace simdjson;
void read_file(const std::string& filename, std::stringstream &buffer) {
std::ifstream f(filename);
if (f.good()) {
buffer << f.rdbuf();
}
}
int main(int argc, char *argv[]) {
std::stringstream ss;
read_file("/tmp/1.json", ss);
std::string text = ss.str();
std::stringstream ostr;
ostr << "C++ simdjson\t" << getpid();
notify(ostr.str());
ParsedJson pj;
int res = simdjson::SUCCESS;
if (pj.allocate_capacity(text.size())) { // allocate memory for parsing up to p.size() bytes
res = json_parse(text, pj); // do the parsing, return 0 on success
}
if (res != simdjson::SUCCESS) {
std::cout << pj.get_error_message() << std::endl;
}
ParsedJson::Iterator pjh(pj);
if (!pjh.is_ok()) {
std::cerr << " Could not iterate parsed result. " << std::endl;
return EXIT_FAILURE;
}
double x = 0, y = 0, z = 0;
int len = 0;
if (pjh.is_object()) {
if (pjh.move_to_key("coordinates")) {
if (pjh.is_array()) {
if (pjh.down()) {
do { // moving through array
if (pjh.is_object()) {
len++;
if (pjh.down()) {
do { // moving through hash {x:, y:, z:}
if (pjh.get_string_length() == 1) {
char c = pjh.get_string()[0];
pjh.next();
switch(c) {
case 'x':
x += pjh.get_double();
break;
case 'y':
y += pjh.get_double();
break;
case 'z':
z += pjh.get_double();
break;
}
} else {
pjh.next();
}
} while(pjh.next()); // moving through hash {x:, y:, z:}
pjh.up();
}
}
} while(pjh.next()); // moving through array
}
}
}
}
std::cout << x / len << std::endl;
std::cout << y / len << std::endl;
std::cout << z / len << std::endl;
notify("stop");
return EXIT_SUCCESS;
}