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 
32  {
33  writer->startObject();
34 
35  for (std::pair<std::string, JsonDataPtr> pair : elements)
36  {
37  writer->writeKey(pair.first);
38  pair.second->writeJson(writer);
39  }
40 
41  writer->endObject();
42  }
43 
44 
45  void armarx::JsonObject::add(const std::string& key, const armarx::JsonDataPtr& value)
46  {
47  if (getKeyIndex(key) >= 0)
48  {
49  throw LocalException("Key \"") << key << "\" exists already in this json object.";
50  }
51 
52  elements.push_back(std::make_pair(key, value));
53  }
54 
55  void JsonObject::add(const std::string& key, const JsonValue& value)
56  {
57  add(key, value.toSharedPtr());
58  }
59 
60  void JsonObject::add(const std::string& key, const JsonArray& value)
61  {
62  add(key, value.toSharedPtr());
63  }
64 
65  void JsonObject::set(const std::string& key, const JsonDataPtr& value)
66  {
67  int index = getKeyIndex(key);
68 
69  if (index < 0)
70  {
71  elements.push_back(std::make_pair(key, value));
72  }
73  else
74  {
75  elements.at(index).second = value;
76  }
77  }
78 
79  void JsonObject::set(const std::string& key, const JsonValue& value)
80  {
81  JsonDataPtr valuePtr(new JsonValue(value));
82  set(key, valuePtr);
83  }
84 
85  bool JsonObject::remove(const std::string& key)
86  {
87  int index = getKeyIndex(key);
88 
89  if (index >= 0)
90  {
91  elements.erase(elements.begin() + 1);
92  }
93 
94  return index >= 0;
95  }
96 
97  JsonDataPtr JsonObject::get(const std::string& key)
98  {
99  int index = getKeyIndex(key);
100 
101  if (index < 0)
102  {
103  return JsonDataPtr();
104  }
105  else
106  {
107  return elements.at(index).second;
108  }
109  }
110 
112  {
113  return elements.size();
114  }
115 
117  {
118  elements.clear();
119  }
120 
122  {
123  JsonObjectPtr ptr(new JsonObject(*this));
124  return ptr;
125  }
126 
128  {
129  JsonObjectPtr o(new JsonObject);
130  for (const std::pair<std::string, JsonDataPtr>& pair : elements)
131  {
132  o->add(pair.first, pair.second->clone());
133  }
134  return o;
135  }
136 
137  std::vector<std::string> JsonObject::getKeys() const
138  {
139  std::vector<std::string> v;
140  for (uint i = 0; i < elements.size(); i++)
141  {
142  v.push_back(elements.at(i).first);
143  }
144  return v;
145  }
146 
147  int JsonObject::getKeyIndex(const std::string& key)
148  {
149  for (uint i = 0; i < elements.size(); i++)
150  {
151  if (elements.at(i).first == key)
152  {
153  return i;
154  }
155  }
156 
157  return -1;
158  }
159 }
armarx::JsonObject::size
uint size()
Definition: JsonObject.cpp:111
armarx::JsonObject::getKeys
std::vector< std::string > getKeys() const
Definition: JsonObject.cpp:137
index
uint8_t index
Definition: EtherCATFrame.h:59
JsonObject.h
armarx::JsonObject::writeJson
void writeJson(const JsonWriterPtr &writer) override
Definition: JsonObject.cpp:31
armarx::JsonObject::set
void set(const std::string &key, const JsonDataPtr &value)
Definition: JsonObject.cpp:65
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:116
armarx::JsonObject::get
JsonDataPtr get(const std::string &key)
Definition: JsonObject.cpp:97
armarx::JsonObject::add
void add(const std::string &key, const JsonDataPtr &value)
Definition: JsonObject.cpp:45
armarx::JsonValue
Definition: JsonValue.h:34
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::JsonObject::remove
bool remove(const std::string &key)
Definition: JsonObject.cpp:85
armarx::JsonObject::toSharedPtr
JsonObjectPtr toSharedPtr() const
Definition: JsonObject.cpp:121
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:28
armarx::JsonObject::clone
JsonDataPtr clone() override
Definition: JsonObject.cpp:127
Exception.h
armarx::JsonObjectPtr
std::shared_ptr< JsonObject > JsonObjectPtr
Definition: JsonObject.h:34