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 
28 
29 #include <SimoxUtility/algorithm/string/string_tools.h>
30 
31 namespace 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)
39  : JPathNavigator(data, -1, "")
40  {
41  }
42 
44  {
45  return std::dynamic_pointer_cast<JsonArray>(data) != 0;
46  }
47 
49  {
50  return std::dynamic_pointer_cast<JsonObject>(data) != 0;
51  }
52 
54  {
55  return std::dynamic_pointer_cast<JsonValue>(data) != 0;
56  }
57 
59  {
60  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
61  return val != 0 && val->getType() == JsonValue::eString;
62  }
63 
65  {
66  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
67  return val != 0 && val->getType() == JsonValue::eBool;
68  }
69 
71  {
72  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
73  return val != 0 && val->getType() == JsonValue::eNull;
74  }
75 
77  {
78  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
79  return val != 0 && val->getType() == JsonValue::eNumber;
80  }
81 
82  bool JPathNavigator::isInt() const
83  {
84  JsonValuePtr val = std::dynamic_pointer_cast<JsonValue>(data);
85  return val != 0 && val->getType() == JsonValue::eNumber && JsonValue::CheckInt(val->rawValue());
86  }
87 
89  {
90  return data != 0;
91  }
92 
94  {
95  if (!isValid())
96  {
97  throw LocalException("JPathNavigator is not valid.");
98  }
99  }
100 
101  std::vector<JPathNavigator> JPathNavigator::select(const std::string& expr, bool limitToOne) const
102  {
103  checkValid();
104  std::vector<std::string> parts = simox::alg::split(expr, "/");
105  std::vector<JPathNavigator> result;
106  select(parts, 0, result, limitToOne);
107  return result;
108  }
109 
110  JPathNavigator JPathNavigator::selectSingleNode(const std::string& expr) const
111  {
112  std::vector<JPathNavigator> result = select(expr, true);
113  return result.size() > 0 ? result.at(0) : JPathNavigator(0);
114  }
115 
116  void JPathNavigator::select(const std::vector<std::string>& parts, size_t partIndex, std::vector<JPathNavigator>& result, bool limitToOne) const
117  {
118  if (partIndex >= parts.size())
119  {
120  result.push_back(JPathNavigator(data, index, key));
121  return;
122  }
123  std::string part = parts.at(partIndex);
124  if (part == "*" && isArray())
125  {
126  JsonArrayPtr a = asArray();
127  for (size_t i = 0; i < a->elements.size(); i++)
128  {
129  JPathNavigator(a->elements.at(i), i, "").select(parts, partIndex + 1, result);
130  if (limitToOne && result.size() > 0)
131  {
132  return;
133  }
134  }
135  }
136  else if (part == "*" && isObject())
137  {
138  JsonObjectPtr o = asObject();
139  for (const std::pair<std::string, JsonDataPtr>& pair : o->elements)
140  {
141  JPathNavigator(pair.second, -1, pair.first).select(parts, partIndex + 1, result);
142  if (limitToOne && result.size() > 0)
143  {
144  return;
145  }
146  }
147  }
148  else if (isObject())
149  {
150  JsonObjectPtr o = asObject();
151  JsonDataPtr child = o->get(part);
152  if (child)
153  {
154  JPathNavigator(child, -1, part).select(parts, partIndex + 1, result);
155  }
156  }
157  }
158 
159 
160  bool JPathNavigator::remove(const std::string& key)
161  {
162  return asObject()->remove(key);
163  }
164 
166  {
167  asArray()->remove(index);
168  }
169 
171  {
172  checkValid();
173  JsonObjectPtr o = std::dynamic_pointer_cast<JsonObject>(data);
174  if (o == 0)
175  {
176  throw LocalException("Invalid cast");
177  }
178  return o;
179  }
180 
182  {
183  checkValid();
184  JsonArrayPtr a = std::dynamic_pointer_cast<JsonArray>(data);
185  if (a == 0)
186  {
187  throw LocalException("Invalid cast");
188  }
189  return a;
190  }
191 
193  {
194  checkValid();
195  JsonValuePtr v = std::dynamic_pointer_cast<JsonValue>(data);
196  if (v == 0)
197  {
198  throw LocalException("Invalid cast");
199  }
200  return v;
201  }
202 
203  std::string JPathNavigator::asString() const
204  {
205  return asValue()->asString();
206  }
207 
208 
210  {
211  return asValue()->asFloat();
212  }
213 
215  {
216  return asValue()->asInt();
217  }
218 
220  {
221  return asValue()->asBool();
222  }
223 
225  {
226  checkValid();
227  return data;
228  }
229 
231  {
232  checkValid();
233  return index;
234  }
235 
236  std::string JPathNavigator::getKey() const
237  {
238  checkValid();
239  return key;
240  }
241 }
armarx::JsonValue::eNumber
@ eNumber
Definition: JsonValue.h:38
armarx::JPathNavigator::isObject
bool isObject() const
Definition: JPathNavigator.cpp:48
armarx::JPathNavigator::isString
bool isString() const
Definition: JPathNavigator.cpp:58
armarx::JsonValuePtr
std::shared_ptr< JsonValue > JsonValuePtr
Definition: JsonValue.h:30
armarx::JPathNavigator::isValue
bool isValue() const
Definition: JPathNavigator.cpp:53
index
uint8_t index
Definition: EtherCATFrame.h:59
part
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in part
Definition: license.txt:18
armarx::JPathNavigator::asBool
bool asBool() const
Definition: JPathNavigator.cpp:219
armarx::JPathNavigator::isNull
bool isNull() const
Definition: JPathNavigator.cpp:70
armarx::JPathNavigator::asString
std::string asString() const
Definition: JPathNavigator.cpp:203
armarx::JsonArrayPtr
std::shared_ptr< JsonArray > JsonArrayPtr
Definition: JsonArray.h:32
armarx::JsonValue::CheckInt
static bool CheckInt(const std::string &value)
Definition: JsonValue.cpp:131
armarx::JPathNavigator
Definition: JPathNavigator.h:35
armarx::JPathNavigator::getKey
std::string getKey() const
Definition: JPathNavigator.cpp:236
StringHelpers.h
armarx::JsonValue::eString
@ eString
Definition: JsonValue.h:38
armarx::JPathNavigator::remove
bool remove(const std::string &key)
Definition: JPathNavigator.cpp:160
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
armarx::JPathNavigator::select
std::vector< JPathNavigator > select(const std::string &expr, bool limitToOne=false) const
Definition: JPathNavigator.cpp:101
armarx::JPathNavigator::checkValid
void checkValid() const
Definition: JPathNavigator.cpp:93
armarx::JsonValue::eNull
@ eNull
Definition: JsonValue.h:38
JPathNavigator.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::JPathNavigator::isBool
bool isBool() const
Definition: JPathNavigator.cpp:64
armarx::JPathNavigator::asArray
JsonArrayPtr asArray() const
Definition: JPathNavigator.cpp:181
armarx::JsonValue::eBool
@ eBool
Definition: JsonValue.h:38
armarx::JPathNavigator::isArray
bool isArray() const
Definition: JPathNavigator.cpp:43
armarx::JPathNavigator::getIndex
int getIndex() const
Definition: JPathNavigator.cpp:230
armarx::JPathNavigator::isInt
bool isInt() const
Definition: JPathNavigator.cpp:82
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::JPathNavigator::getData
JsonDataPtr getData() const
Definition: JPathNavigator.cpp:224
armarx::JPathNavigator::selectSingleNode
JPathNavigator selectSingleNode(const std::string &expr) const
Definition: JPathNavigator.cpp:110
armarx::JsonDataPtr
std::shared_ptr< JsonData > JsonDataPtr
Definition: JsonData.h:31
armarx::JPathNavigator::asFloat
float asFloat() const
Definition: JPathNavigator.cpp:209
armarx::JPathNavigator::asValue
JsonValuePtr asValue() const
Definition: JPathNavigator.cpp:192
armarx::JPathNavigator::isNumber
bool isNumber() const
Definition: JPathNavigator.cpp:76
armarx::JPathNavigator::isValid
bool isValid() const
Definition: JPathNavigator.cpp:88
armarx::JPathNavigator::asInt
int asInt() const
Definition: JPathNavigator.cpp:214
armarx::JPathNavigator::asObject
JsonObjectPtr asObject() const
Definition: JPathNavigator.cpp:170
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
Exception.h
armarx::JsonObjectPtr
std::shared_ptr< JsonObject > JsonObjectPtr
Definition: JsonObject.h:34
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36