-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonNode.cpp
More file actions
112 lines (90 loc) · 2.48 KB
/
Copy pathJsonNode.cpp
File metadata and controls
112 lines (90 loc) · 2.48 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
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
#include "JsonNode.h"
JsonValue::JsonValue() : JsonValue('?') {}
JsonValue::JsonValue(char c) : type(c){
//printf("value %c\n", c);
}
int JsonValue::toLuaObject(LS) {
switch(type){
case 'n':
lua_pushnumber(L, as<JsonNumber>()->value);
return 1;
case 'b':
lua_pushboolean(L, as<JsonBoolean>()->value);
return 1;
case 's':
lua_pushstring(L, as<JsonString>()->value);
return 1;
case 'x':
lua_pushnil(L);
return 1;
case 'o':
lua_newtable(L);
for ( auto & kv : as<JsonObject>()->ptrTable ){
kv.second->toLuaObject(L);
lua_setfield(L, -2, kv.first );
}
return 1;
case 'a':{
lua_newtable(L);
int idx = 1;
for ( auto & v : as<JsonArray>()->ptrVec ){
v->toLuaObject(L);
lua_rawseti(L, -2, idx);
idx++;
}
return 1;
}
default:
luaL_error(L, "unknown type of JsonValue: %c", type);
return 0;
}
}
const char * JsonValue::typeString() const {
switch(type){
case 'n': return "JsonNumber";
case 'b': return "JsonBoolean";
case 's': return "JsonString";
case 'o': return "JsonObject";
case 'a': return "JsonArray";
case 'x': return "JsonNull";
default:
return "JsonUnknownType";
}
}
///////////
JsonObject::JsonObject() : JsonValue('o') {}
/////////
JsonArray::JsonArray() : JsonValue('a') {}
/////
JsonString::JsonString(const char * s) : JsonValue('s'), value(s) {
}
//////
JsonNumber::JsonNumber(double v) : JsonValue('n'), value(v) {}
///
JsonBoolean::JsonBoolean(bool b) : JsonValue('b'), value(b) {}
//////// JsonNull
JsonNull::JsonNull() : JsonValue('x') {}
JsonState::JsonState() : value(nullptr), refcnt(1) {
}
const char * JsonState::getString( const char * str ){
auto s = strPool.insert(str);
return s.first->data();
}
const char * JsonState::searchString( const char * str ){
auto it = strPool.find(str);
if( it == strPool.end() ){
return nullptr;
}else{
return it->data();
}
}
//JsonValue *JsonState::getJsonValue() const { return value.get(); }
void JsonState::free() {
for( auto & v : objList ){
delete v;
v = nullptr;
}
value = nullptr;
objList.clear();
objList.shrink_to_fit();
}