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
25namespace armarx
26{
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 {
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
uint8_t index
void add(const std::string &key, const JsonDataPtr &value)
JsonObjectPtr toSharedPtr() const
std::vector< std::string > getKeys() const
void set(const std::string &key, const JsonDataPtr &value)
bool remove(const std::string &key)
void writeJson(const JsonWriterPtr &writer) override
JsonDataPtr get(const std::string &key)
JsonDataPtr clone() override
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< JsonObject > JsonObjectPtr
Definition JsonObject.h:34
std::shared_ptr< JsonData > JsonDataPtr
Definition JsonData.h:31
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition JsonWriter.h:30