forked from carlren/ORUtils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJsonUtil.h
147 lines (140 loc) · 4.94 KB
/
JsonUtil.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
141
142
143
144
145
146
147
//
// Created by sc on 9/1/20.
//
#ifndef GRAPHSLAM_JSONUTIL_H
#define GRAPHSLAM_JSONUTIL_H
#ifdef COMPILE_WITH_JSON
#include <json11.hpp>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "Logging.h"
#include <cassert>
static size_t indent_increment=4;
class JsonUtils {
public:
static json11::Json LoadJson(const std::string &path){
std::ifstream is(path, std::ios::in);
std::string dataset((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
std::string err;
auto json = json11::Json::parse(dataset, err);
if (!err.empty())
SCLOG(ERROR) << "Error reading " << path << " " << err;
return json;
}
static std::string GetType(const json11::Json &json){
switch (json.type()) {
case json11::Json::NUL:
return "NUL";
case json11::Json::NUMBER:
return "NUMBER";
case json11::Json::BOOL:
return "BOOL";
case json11::Json::STRING:
return "STRING";
case json11::Json::ARRAY:
return "ARRAY";
case json11::Json::OBJECT:
return "OBJECT";
default:
return "UNKNOWN";
}
}
static void GetLayoutInfo(const json11::Json &json, size_t depth){
printf("[%zu]Current type %s\n",depth, GetType(json).c_str());
switch (json.type()) {
case json11::Json::NUL:
break;
case json11::Json::NUMBER:
printf("[%zu] number value: %f\n",depth, json.number_value());
break;
case json11::Json::BOOL:
printf("[%zu] bool: %d\n",depth, json.bool_value());
break;
case json11::Json::STRING:
printf("[%zu] string: %s\n",depth, json.string_value().c_str());
break;
case json11::Json::ARRAY:
printf("[%zu] array size: %zu\n",depth, json.array_items().size());
if(depth>0)
for(auto &arr:json.array_items()) GetLayoutInfo(arr,depth-1);
break;
case json11::Json::OBJECT:
printf("[%zu] object size: %zu\n",depth, json.object_items().size());
if(depth>0)
for(auto &arr:json.object_items()){
printf("[%zu] obj[%s]: \n",depth,arr.first.c_str());
GetLayoutInfo(arr.second,depth-1);
}
break;
}
}
static void PrintValueType(const json11::Json &json) {
switch (json.type()) {
case json11::Json::NUMBER:
std::cout << json.number_value();
return;
case json11::Json::BOOL:
std::cout << json.bool_value();
return;
case json11::Json::STRING:
std::cout << json.string_value();
return;
default:
std::cout << "unprintable type: " << GetType(json);
}
}
static void write_name(const std::string &name, std::fstream &file){
file << "\"" << name << "\"";
}
static void _dump(const json11::Json &json, size_t indent, std::fstream &file){
if(json.is_object()){
auto iter = json.object_items().begin();
auto end = json.object_items().end();
file << "{\n";
while(iter != end) {
if(iter != json.object_items().begin()) file << ",\n";
file << std::setw(indent);
write_name(iter->first,file);
file << ": ";
_dump(iter->second,indent+indent_increment,file);
std::advance(iter,1);
}
file << "\n";
file << std::setw(indent);
file << "}";
} else if (json.is_array()) {
auto iter = json.array_items().begin();
auto end = json.array_items().end();
file << "[";
while(iter != end) {
file << "\n";
file << std::setw(indent);
_dump(*iter,indent+indent_increment,file);
std::advance(iter,1);
if(iter != end) file << ",";
else {
file << "\n";
file << std::setw(indent);
}
}
file << "]";
} else if (json.is_bool()) {
if(json.bool_value())
file << "true";
else
file << "false";
} else if (json.is_string() ) {
write_name(json.string_value(), file);
} else if (json.is_number()){
file << json.number_value();
}
}
static void Dump(const json11::Json &json, const std::string &path){
std::fstream file(path, std::ios::out);
assert(file.is_open());
_dump(json,4,file);
}
};
#endif
#endif //GRAPHSLAM_JSONUTIL_H