-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.cpp
72 lines (54 loc) · 1.42 KB
/
json.cpp
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
#include "json.hpp"
#include <sstream>
std::string JSON_serialize(const json_t& json)
{
Json::FastWriter writer;
std::string stringified=writer.write(json);
while(stringified.size()>0&&isspace(stringified[stringified.size()-1])!=0)
stringified.pop_back();
return stringified;
}
json_t JSON_parse(const std::string& stringified)
{
std::istringstream istr(stringified);
json_t json;
istr>>json;
return json;
}
std::string to_string(const json_t& json)
{
return json.asString();
}
std::vector<double> to_double_array(const json_t& json)
{
if(!json.isArray())
throw std::runtime_error("Not a JSON double array.");
std::vector<double> array;
for(auto ii:json)
if(!ii.isDouble()&&!ii.isInt())
throw std::runtime_error("JSON array contains a non-double.");
else
array.push_back(ii.asDouble());
return array;
}
std::vector<size_t> to_size_array(const json_t& json)
{
if(!json.isArray())
throw std::runtime_error("Not a JSON unsigned integer array.");
std::vector<size_t> array;
for(auto ii:json)
if(!ii.isUInt())
throw std::runtime_error("JSON array contains a non-unsigned integer.");
else
array.push_back(ii.asUInt());
return array;
}
std::vector<std::vector<double>> to_array_double_array(const json_t& json)
{
if(!json.isArray())
throw std::runtime_error("Not a JSON array.");
std::vector<std::vector<double>> array;
for(auto ii:json)
array.push_back(to_double_array(ii));
return array;
}