Value.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/json/Value.hpp"
30 
31 namespace nom {
32 namespace JSON {
33 
34 Value::Value ( void ) :
35  object_ { Json::Value ( Json::nullValue ) }, key_ { "\0" }
36 {}
37 
38 Value::~Value ( void ) {}
39 
40 Value::Value ( const Value& copy )
41 {
42  this->object_ = copy.object_;
43  this->key_ = copy.key_;
44 }
45 
46 Value& Value::operator = ( const ValueType& other )
47 {
48  this->object_ = other;
49  return *this;
50 }
51 
52 ValueType& Value::get ( void ) const
53 {
54  return this->object_;
55 }
56 
57 uint32 Value::size ( void ) const
58 {
59  return this->object_.size();
60 }
61 
62 JSONValueType Value::type ( void ) const
63 {
64  return this->object_.type();
65 }
66 
67 JSONValueType Value::type ( int index ) const
68 {
69  return this->object_[index].type();
70 }
71 
72 JSONValueType Value::type ( const std::string& key, int index ) const
73 {
74  return this->object_[index][key].type();
75 }
76 
77 JSONMemberType Value::members ( int index ) const
78 {
79  return this->object_[index].getMemberNames();
80 }
81 
82 int Value::get_int ( int index )
83 {
84  return this->object_[index].asInt();
85 }
86 
87 std::vector<int> Value::get_ints ( const std::string& key, int index )
88 {
89  std::vector<int> members;
90  for ( uint i = 0; i < this->object_[index][key].size(); ++i )
91  {
92  members.push_back ( this->object_[index][key][i].asInt() );
93  }
94  return members;
95 }
96 
97 int Value::get_int ( const std::string& key, int index )
98 {
99  return this->object_[index][key].asInt();
100 }
101 
102 uint Value::get_uint ( int index )
103 {
104  return this->object_[index].asUInt();
105 }
106 
107 std::vector<uint> Value::get_uints ( int index )
108 {
109  std::vector<uint> members;
110  for ( uint i = 0; i < this->object_[i].size(); ++i )
111  {
112  members.push_back ( this->object_[index][i].asUInt() );
113  }
114  return members;
115 }
116 
117 uint Value::get_uint ( const std::string& key )
118 {
119  return this->object_[key].asUInt();
120 }
121 
122 const char* Value::c_str ( int index )
123 {
124  return this->object_[index].asCString();
125 }
126 
127 std::string Value::get_string ( int index )
128 {
129  return this->object_[index].asString();
130 }
131 
132 std::vector<std::string> Value::get_strings ( int index )
133 {
134  std::vector<std::string> members;
135  for ( uint i = 0; i < this->object_[i].size(); ++i )
136  {
137  members.push_back ( this->object_[index][i].asString() );
138  }
139  return members;
140 }
141 
142 const char* Value::c_str ( const std::string& key )
143 {
144  return this->object_[key].asCString();
145 }
146 
147 std::string Value::get_string ( const std::string& key, int index )
148 {
149  return this->object_[index][key].asString();
150 }
151 
152 bool Value::get_bool ( int index )
153 {
154  return this->object_[index].asBool();
155 }
156 
157 std::vector<bool> Value::get_bools ( int index )
158 {
159  std::vector<bool> members;
160  for ( uint i = 0; i < this->object_[i].size(); ++i )
161  {
162  members.push_back ( this->object_[index][i].asBool() );
163  }
164  return members;
165 }
166 
167 bool Value::get_bool ( const std::string& key )
168 {
169  return this->object_[key].asBool();
170 }
171 
172 ValueType Value::value ( int index )
173 {
174  return this->object_[index];
175 }
176 
177 std::vector<ValueType> Value::values ( int index )
178 {
179  std::vector<ValueType> members;
180  for ( uint i = 0; i < this->object_[i].size(); ++i )
181  {
182  members.push_back ( this->object_[index][i] );
183  }
184  return members;
185 }
186 
187 ValueType Value::value ( const std::string& key )
188 {
189  return this->object_[key];
190 }
191 
192 void Value::insert ( const std::string& key, int index )
193 {
194  this->object_[index][key] = Json::Value ( Json::nullValue );
195 }
196 
197 void Value::insert ( const std::string& key, int value, int index )
198 {
199  this->object_[index][key] = Json::Value ( value );
200 }
201 
202 void Value::insert ( const std::string& key, std::vector<int> values )
203 {
204  for ( auto member = values.begin(); member != values.end(); ++member )
205  {
206  this->object_[key].append ( * member );
207  }
208 }
209 
210 void Value::insert ( const std::string& key, uint value, int index )
211 {
212  this->object_[index][key] = Json::Value ( value );
213 }
214 
215 void Value::insert ( const std::string& key, const std::vector<uint>& values )
216 {
217  for ( auto member = values.begin(); member != values.end(); ++member )
218  {
219  this->object_[key].append ( * member );
220  }
221 }
222 
223 void Value::insert ( const std::string& key, double value, int index )
224 {
225  this->object_[index][key] = Json::Value ( value );
226 }
227 
228 void Value::insert ( const std::string& key, float value, int index )
229 {
230  this->object_[index][key] = Json::Value ( value );
231 }
232 
233 void Value::insert ( const std::string& key, const char* value, int index )
234 {
235  this->object_[index][key] = Json::Value ( value );
236 }
237 
238 void Value::insert ( const std::string& key, const std::string& value,
239  int index )
240 {
241  this->object_[index][key] = Json::Value ( value );
242 }
243 
244 void Value::insert ( const std::string& key,
245  const std::vector<std::string>& values )
246 {
247  for ( auto member = values.begin(); member != values.end(); ++member )
248  {
249  this->object_[key].append ( * member );
250  }
251 }
252 
253 void Value::insert ( const std::string& key, bool value, int index )
254 {
255  this->object_[index][key] = Json::Value ( value );
256 }
257 
258 void Value::insert ( const std::string& key,
259  const std::vector<ValueType>& values )
260 {
261  for ( auto member = values.begin(); member != values.end(); ++member )
262  {
263  this->object_[key].append ( *member );
264  }
265 }
266 
267 } // namespace JSON
268 } // namespace nom
Interface class for JSON data types.
Definition: Value.hpp:45
ValueType object_
Definition: Value.hpp:149
&#39;null&#39; value
Definition: json.h:347
std::string key_
Identifying access string for an object node.
Definition: Value.hpp:152
ArrayIndex size() const
Number of values in array or object.
Definition: jsoncpp.cpp:2506
Represents a JSON value.
Definition: json.h:433
ValueType
Type of the value held by a Value object.
Definition: json.h:345