JPathNavigator.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5 * Karlsruhe Institute of Technology (KIT), all rights reserved.
6 *
7 * ArmarX is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24#include "JPathNavigator.h"
25
26#include <SimoxUtility/algorithm/string/string_tools.h>
27
30
31namespace armarx
32{
33 JPathNavigator::JPathNavigator(const JsonDataPtr& data, int index, const std::string& key) :
34 data(data), index(index), key(key)
35 {
36 }
37
38 JPathNavigator::JPathNavigator(const JsonDataPtr& data) : JPathNavigator(data, -1, "")
39 {
40 }
41
42 bool
44 {
45 return std::dynamic_pointer_cast<JsonArray>(data) != 0;
46 }
47
48 bool
50 {
51 return std::dynamic_pointer_cast<JsonObject>(data) != 0;
52 }
53
54 bool
56 {
57 return std::dynamic_pointer_cast<JsonValue>(data) != 0;
58 }
59
60 bool
62 {
63 JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
64 return val != 0 && val->getType() == JsonValue::eString;
65 }
66
67 bool
69 {
70 JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
71 return val != 0 && val->getType() == JsonValue::eBool;
72 }
73
74 bool
76 {
77 JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
78 return val != 0 && val->getType() == JsonValue::eNull;
79 }
80
81 bool
83 {
84 JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
85 return val != 0 && val->getType() == JsonValue::eNumber;
86 }
87
88 bool
90 {
91 JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
92 return val != 0 && val->getType() == JsonValue::eNumber &&
93 JsonValue::CheckInt(val->rawValue());
94 }
95
96 bool
98 {
99 return data != 0;
100 }
101
102 void
104 {
105 if (!isValid())
106 {
107 throw LocalException("JPathNavigator is not valid.");
108 }
109 }
110
111 std::vector<JPathNavigator>
112 JPathNavigator::select(const std::string& expr, bool limitToOne) const
113 {
114 checkValid();
115 std::vector<std::string> parts = simox::alg::split(expr, "/");
116 std::vector<JPathNavigator> result;
117 select(parts, 0, result, limitToOne);
118 return result;
119 }
120
122 JPathNavigator::selectSingleNode(const std::string& expr) const
123 {
124 std::vector<JPathNavigator> result = select(expr, true);
125 return result.size() > 0 ? result.at(0) : JPathNavigator(0);
126 }
127
128 void
129 JPathNavigator::select(const std::vector<std::string>& parts,
130 size_t partIndex,
131 std::vector<JPathNavigator>& result,
132 bool limitToOne) const
133 {
134 if (partIndex >= parts.size())
135 {
136 result.push_back(JPathNavigator(data, index, key));
137 return;
138 }
139 std::string part = parts.at(partIndex);
140 if (part == "*" && isArray())
141 {
142 JsonArrayPtr a = asArray();
143 for (size_t i = 0; i < a->elements.size(); i++)
144 {
145 JPathNavigator(a->elements.at(i), i, "").select(parts, partIndex + 1, result);
146 if (limitToOne && result.size() > 0)
147 {
148 return;
149 }
150 }
151 }
152 else if (part == "*" && isObject())
153 {
155 for (const std::pair<std::string, JsonDataPtr>& pair : o->elements)
156 {
157 JPathNavigator(pair.second, -1, pair.first).select(parts, partIndex + 1, result);
158 if (limitToOne && result.size() > 0)
159 {
160 return;
161 }
162 }
163 }
164 else if (isObject())
165 {
167 JsonDataPtr child = o->get(part);
168 if (child)
169 {
170 JPathNavigator(child, -1, part).select(parts, partIndex + 1, result);
171 }
172 }
173 }
174
175 bool
176 JPathNavigator::remove(const std::string& key)
177 {
178 return asObject()->remove(key);
179 }
180
181 void
183 {
184 asArray()->remove(index);
185 }
186
189 {
190 checkValid();
191 JsonObjectPtr o = std::dynamic_pointer_cast<JsonObject>(data);
192 if (o == 0)
193 {
194 throw LocalException("Invalid cast");
195 }
196 return o;
197 }
198
201 {
202 checkValid();
203 JsonArrayPtr a = std::dynamic_pointer_cast<JsonArray>(data);
204 if (a == 0)
205 {
206 throw LocalException("Invalid cast");
207 }
208 return a;
209 }
210
213 {
214 checkValid();
215 JsonValuePtr v = std::dynamic_pointer_cast<JsonValue>(data);
216 if (v == 0)
217 {
218 throw LocalException("Invalid cast");
219 }
220 return v;
221 }
222
223 std::string
225 {
226 return asValue()->asString();
227 }
228
229 float
231 {
232 return asValue()->asFloat();
233 }
234
235 int
237 {
238 return asValue()->asInt();
239 }
240
241 bool
243 {
244 return asValue()->asBool();
245 }
246
249 {
250 checkValid();
251 return data;
252 }
253
254 int
256 {
257 checkValid();
258 return index;
259 }
260
261 std::string
263 {
264 checkValid();
265 return key;
266 }
267} // namespace armarx
uint8_t index
std::string asString() const
JsonArrayPtr asArray() const
JPathNavigator selectSingleNode(const std::string &expr) const
JsonValuePtr asValue() const
std::vector< JPathNavigator > select(const std::string &expr, bool limitToOne=false) const
bool remove(const std::string &key)
std::string getKey() const
JsonObjectPtr asObject() const
JsonDataPtr getData() const
static bool CheckInt(const std::string &value)
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< JsonValue > JsonValuePtr
Definition JsonValue.h:30
std::shared_ptr< JsonObject > JsonObjectPtr
Definition JsonObject.h:34
std::shared_ptr< JsonData > JsonDataPtr
Definition JsonData.h:31
std::shared_ptr< JsonArray > JsonArrayPtr
Definition JsonArray.h:32