JsonObject.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
17 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18 * GNU General Public License
19 */
20 
21 #include "JsonObject.h"
22 
24 
25 namespace armarx
26 {
28  {
29  }
30 
31  void
33  {
34  writer->startObject();
35 
36  for (std::pair<std::string, JsonDataPtr> pair : elements)
37  {
38  writer->writeKey(pair.first);
39  pair.second->writeJson(writer);
40  }
41 
42  writer->endObject();
43  }
44 
45  void
46  armarx::JsonObject::add(const std::string& key, const armarx::JsonDataPtr& value)
47  {
48  if (getKeyIndex(key) >= 0)
49  {
50  throw LocalException("Key \"") << key << "\" exists already in this json object.";
51  }
52 
53  elements.push_back(std::make_pair(key, value));
54  }
55 
56  void
57  JsonObject::add(const std::string& key, const JsonValue& value)
58  {
59  add(key, value.toSharedPtr());
60  }
61 
62  void
63  JsonObject::add(const std::string& key, const JsonArray& value)
64  {
65  add(key, value.toSharedPtr());
66  }
67 
68  void
69  JsonObject::set(const std::string& key, const JsonDataPtr& value)
70  {
71  int index = getKeyIndex(key);
72 
73  if (index < 0)
74  {
75  elements.push_back(std::make_pair(key, value));
76  }
77  else
78  {
79  elements.at(index).second = value;
80  }
81  }
82 
83  void
84  JsonObject::set(const std::string& key, const JsonValue& value)
85  {
86  JsonDataPtr valuePtr(new JsonValue(value));
87  set(key, valuePtr);
88  }
89 
90  bool
91  JsonObject::remove(const std::string& key)
92  {
93  int index = getKeyIndex(key);
94 
95  if (index >= 0)
96  {
97  elements.erase(elements.begin() + 1);
98  }
99 
100  return index >= 0;
101  }
102 
104  JsonObject::get(const std::string& key)
105  {
106  int index = getKeyIndex(key);
107 
108  if (index < 0)
109  {
110  return JsonDataPtr();
111  }
112  else
113  {
114  return elements.at(index).second;
115  }
116  }
117 
118  uint
120  {
121  return elements.size();
122  }
123 
124  void
126  {
127  elements.clear();
128  }
129 
132  {
133  JsonObjectPtr ptr(new JsonObject(*this));
134  return ptr;
135  }
136 
139  {
140  JsonObjectPtr o(new JsonObject);
141  for (const std::pair<std::string, JsonDataPtr>& pair : elements)
142  {
143  o->add(pair.first, pair.second->clone());
144  }
145  return o;
146  }
147 
148  std::vector<std::string>
150  {
151  std::vector<std::string> v;
152  for (uint i = 0; i < elements.size(); i++)
153  {
154  v.push_back(elements.at(i).first);
155  }
156  return v;
157  }
158 
159  int
160  JsonObject::getKeyIndex(const std::string& key)
161  {
162  for (uint i = 0; i < elements.size(); i++)
163  {
164  if (elements.at(i).first == key)
165  {
166  return i;
167  }
168  }
169 
170  return -1;
171  }
172 } // namespace armarx
armarx::JsonObject::size
uint size()
Definition: JsonObject.cpp:119
armarx::JsonObject::getKeys
std::vector< std::string > getKeys() const
Definition: JsonObject.cpp:149
index
uint8_t index
Definition: EtherCATFrame.h:59
JsonObject.h
armarx::JsonObject::writeJson
void writeJson(const JsonWriterPtr &writer) override
Definition: JsonObject.cpp:32
armarx::JsonObject::set
void set(const std::string &key, const JsonDataPtr &value)
Definition: JsonObject.cpp:69
armarx::JsonArray
Definition: JsonArray.h:34
armarx::JsonWriterPtr
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition: JsonWriter.h:30
armarx::JsonObject::clear
void clear()
Definition: JsonObject.cpp:125
armarx::JsonObject::get
JsonDataPtr get(const std::string &key)
Definition: JsonObject.cpp:104
armarx::JsonObject::add
void add(const std::string &key, const JsonDataPtr &value)
Definition: JsonObject.cpp:46
armarx::JsonValue
Definition: JsonValue.h:34
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::JsonObject::remove
bool remove(const std::string &key)
Definition: JsonObject.cpp:91
armarx::JsonObject::toSharedPtr
JsonObjectPtr toSharedPtr() const
Definition: JsonObject.cpp:131
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::JsonObject
Definition: JsonObject.h:36
armarx::JsonObject::JsonObject
JsonObject()
Definition: JsonObject.cpp:27
armarx::JsonDataPtr
std::shared_ptr< JsonData > JsonDataPtr
Definition: JsonData.h:31
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::JsonObject::clone
JsonDataPtr clone() override
Definition: JsonObject.cpp:138
Exception.h
armarx::JsonObjectPtr
std::shared_ptr< JsonObject > JsonObjectPtr
Definition: JsonObject.h:34