-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_object_json_source.cpp
158 lines (126 loc) · 4.98 KB
/
script_object_json_source.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
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
148
149
150
151
152
153
154
155
156
157
158
/*
* This file is part of libscriptobject.
*
* libscriptobject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* libscriptobject is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libscriptobject. If not, see <http://www.gnu.org/licenses/>.
**/
#include "script_object_json_source.h"
#include <cstring>
#include "script_object_factory.h"
#include "script_array_factory.h"
#include "script_array_json_source.h"
rs::scriptobject::ScriptObjectJsonSource::ScriptObjectJsonSource(char* json) {
char* endPtr;
JsonValue value;
auto status = jsonParse(json, &endPtr, &value, allocator_);
if (status != 0) {
throw ScriptObjectJsonSourceParseException(jsonStrError(status));
}
members_ = GetMembers(value);
}
rs::scriptobject::ScriptObjectJsonSource::ScriptObjectJsonSource(const char* json) :
ScriptObjectJsonSource(json != nullptr ? std::vector<char>{&json[0], &json[strlen(json)]}.data() : nullptr) {
}
rs::scriptobject::ScriptObjectJsonSource::ScriptObjectJsonSource(JsonValue value) : members_(GetMembers(value)) {
}
unsigned rs::scriptobject::ScriptObjectJsonSource::count() const {
return members_.size();
}
rs::scriptobject::ScriptObjectType rs::scriptobject::ScriptObjectJsonSource::type(int index) const {
if (index < 0 && index >= count()) {
throw UnknownScriptObjectFieldException();
}
return getType(members_[index]->value.getTag());
}
const char* rs::scriptobject::ScriptObjectJsonSource::name(int index) const {
if (index < 0 && index >= count()) {
throw UnknownScriptObjectFieldException();
}
return members_[index]->key;
}
unsigned rs::scriptobject::ScriptObjectJsonSource::length(int index) const {
return std::strlen(name(index));
}
bool rs::scriptobject::ScriptObjectJsonSource::getBoolean(int index) const {
if (type(index) != ScriptObjectType::Boolean) {
throw TypeCastException();
}
return members_[index]->value.getTag() == JSON_TRUE;
}
std::int32_t rs::scriptobject::ScriptObjectJsonSource::getInt32(int index) const {
throw UnknownScriptObjectFieldException();
}
std::uint32_t rs::scriptobject::ScriptObjectJsonSource::getUInt32(int index) const {
throw UnknownScriptObjectFieldException();
}
std::int64_t rs::scriptobject::ScriptObjectJsonSource::getInt64(int index) const {
throw UnknownScriptObjectFieldException();
}
std::uint64_t rs::scriptobject::ScriptObjectJsonSource::getUInt64(int index) const {
throw UnknownScriptObjectFieldException();
}
double rs::scriptobject::ScriptObjectJsonSource::getDouble(int index) const {
if (type(index) != ScriptObjectType::Double) {
throw TypeCastException();
}
return members_[index]->value.fval;
}
const char* rs::scriptobject::ScriptObjectJsonSource::getString(int index) const {
if (type(index) != ScriptObjectType::String) {
throw TypeCastException();
}
return members_[index]->value.toString();
}
int rs::scriptobject::ScriptObjectJsonSource::getStringLength(int index) const {
return std::strlen(getString(index));
}
const rs::scriptobject::ScriptObjectPtr rs::scriptobject::ScriptObjectJsonSource::getObject(int index) const {
if (type(index) != ScriptObjectType::Object) {
throw TypeCastException();
}
ScriptObjectJsonSource objectSource(members_[index]->value);
return ScriptObjectFactory::CreateObject(objectSource);
}
const rs::scriptobject::ScriptArrayPtr rs::scriptobject::ScriptObjectJsonSource::getArray(int index) const {
if (type(index) != ScriptObjectType::Array) {
throw TypeCastException();
}
ScriptArrayJsonSource arraySource(members_[index]->value);
return ScriptArrayFactory::CreateArray(arraySource);
}
rs::scriptobject::ScriptObjectType rs::scriptobject::ScriptObjectJsonSource::getType(JsonTag tag) {
switch (tag) {
case JSON_NULL:
return ScriptObjectType::Null;
case JSON_NUMBER:
return ScriptObjectType::Double;
case JSON_TRUE:
case JSON_FALSE:
return ScriptObjectType::Boolean;
case JSON_OBJECT:
return ScriptObjectType::Object;
case JSON_ARRAY:
return ScriptObjectType::Array;
case JSON_STRING:
return ScriptObjectType::String;
default:
return ScriptObjectType::Unknown;
}
}
std::vector<JsonNode*> rs::scriptobject::ScriptObjectJsonSource::GetMembers(JsonValue& value) {
std::vector<JsonNode*> members;
for (auto i : value) {
members.push_back(i);
}
return members;
}