-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils_jsoncpp.cpp
59 lines (49 loc) · 1.27 KB
/
utils_jsoncpp.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
//utils_jsoncpp.cpp
#include "utils_jsoncpp.h"
using namespace std;
template <typename Iterable>
Json::Value util::iterable2json(Iterable const& cont) {
Json::Value v;
for (auto&& element: cont) {
v.append(element);
}
return v;
}
void util::PrintJSONValue( Json::Value val ){
if( val.isString() ) {
printf( "string(%s)", val.asString().c_str() );
} else if( val.isBool() ) {
printf( "bool(%d)", val.asBool() );
} else if( val.isInt() ) {
printf( "int(%d)", val.asInt() );
} else if( val.isUInt() ) {
printf( "uint(%u)", val.asUInt() );
} else if( val.isDouble() ) {
printf( "double(%f)", val.asDouble() );
} else {
printf( "unknown type=[%d]", val.type() );
}
}
bool util::PrintJSONTree( Json::Value &root, unsigned short depth /* = 0 */) {
depth += 1;
printf( " {type=[%d], size=%d}", root.type(), root.size() );
if( root.size() > 0 ) {
printf("\n");
for( Json::ValueIterator itr = root.begin() ; itr != root.end() ; itr++ ) {
// Print depth.
for( int tab = 0 ; tab < depth; tab++) {
printf("-");
}
printf(" subvalue(");
PrintJSONValue(itr.key());
printf(") -");
PrintJSONTree( *itr, depth);
}
return true;
} else {
printf(" ");
PrintJSONValue(root);
printf( "\n" );
}
return true;
}