-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.h
140 lines (114 loc) · 3.76 KB
/
json.h
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
#ifndef JH_JSON_H_
#define JH_JSON_H_
#include<map>
#include<string>
#include<fstream>
#include<sstream>
#include"ezlog.h"
#undef max
#undef min
//#define JSON_WITHOUT_CPP11
// exclude unsupported compilers
#if defined(__clang__)
#if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
#define JSON_WITHOUT_CPP11
#include"3rdparty/picojson.h"
#else
#include"3rdparty/json.hpp"
#endif
#elif defined(__GNUC__)
#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40900
#define JSON_WITHOUT_CPP11
#include"3rdparty/picojson.h"
#else
#include"3rdparty/json.hpp"
#endif
#elif defined(_MSC_VER)// just support vs2015 or above
#ifndef JSON_WITHOUT_CPP11
#include"3rdparty/json.hpp"
#else
#include"3rdparty/picojson.h"
#endif // !JSON_WITHOUT_CPP11
#endif
namespace jheaders
{
inline std::map<std::string, std::string> parse_simple_json_str (const std::string&json_str);
inline std::map<std::string, std::string> parse_simple_json_from_file (const std::string & json_file_path);
#ifndef JSON_WITHOUT_CPP11
using Json = nlohmann::json;
//Json without nest,just key and value(int,float,double,string)
inline std::map<std::string, std::string> parse_simple_json_str (const std::string&json_str)
{
Json jn;
try
{
jn = Json::parse (json_str.c_str());
}
catch (std::exception &e)
{
EZLOG (Log_level::FATAL) << "parse Json string failed: " << e.what();
}
std::map<std::string, std::string> results;
for (Json::iterator it = jn.begin(); it != jn.end(); ++it)
{
std::string key = it.key();
std::string value = it.value().dump();
if (it.value().is_string())
{
if (value.size() == 2) //just has ""
{
value.clear();
}
else
{
value = std::string (value.begin() + 1, value.end() - 1);//delete " from string
}
}
results[key] = value;
}
return results;
}
#else
inline std::map<std::string, std::string> parse_simple_json_str (const std::string&json_str)
{
picojson::value v;
std::string err = picojson::parse (v, json_str);
std::map<std::string, std::string> results;
if (!err.empty())
{
EZLOG_ (fatal) << err;
}
else
{
const picojson::value::object& obj = v.get<picojson::object>();
for (picojson::value::object::const_iterator i = obj.begin();
i != obj.end();
++i)
{
//std::cout << i->first << ": " << i->second.to_str() << std::endl;
results[i->first] = i->second.to_str();
}
}
return results;
}
#endif // !JSON_WITHOUT_CPP11
//Json without nest,just key and value(int,float,double,string)
inline std::map<std::string, std::string> parse_simple_json_from_file (const std::string & json_file_path)
{
std::ifstream in (json_file_path);
std::map<std::string, std::string> result;
std::string contents;
if (!in)
{
EZLOG (Log_level::FATAL) << "read file failed: " << json_file_path;
}
else
{
std::ostringstream out_str;
out_str << in.rdbuf();
contents = out_str.str();
}
return parse_simple_json_str (contents);
}
}
#endif