-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest_gason.cpp
More file actions
59 lines (50 loc) · 1.31 KB
/
test_gason.cpp
File metadata and controls
59 lines (50 loc) · 1.31 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
#include "gason.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <string.h>
#include <libnotify.hpp>
#include <unistd.h>
using namespace std;
void read_file(string filename, stringstream &buffer){
ifstream f(filename.c_str());
if (f)
{
buffer << f.rdbuf();
f.close();
}
}
int main() {
stringstream ss;
read_file("/tmp/1.json", ss);
string text = ss.str();
stringstream ostr;
ostr << "C++ gason\t" << getpid();
notify(ostr.str());
char *endptr;
JsonValue jobj;
JsonAllocator allocator;
int status = jsonParse((char *)text.c_str(), &endptr, &jobj, allocator);
if (status != JSON_OK) return 1;
JsonValue coordinates;
for (auto data : jobj) {
if (strcmp(data->key, "coordinates") == 0) { coordinates = data->value; }
}
double x = 0, y = 0, z = 0;
int len = 0;
for (auto coord : coordinates) {
len++;
for (auto c : coord->value) {
char *key = c->key;
if (strcmp(key, "x") == 0) { x += c->value.toNumber(); } else
if (strcmp(key, "y") == 0) { y += c->value.toNumber(); } else
if (strcmp(key, "z") == 0) { z += c->value.toNumber(); }
}
}
cout << x / len << endl;
cout << y / len << endl;
cout << z / len << endl;
notify("stop");
return 0;
}