TrueReality  v0.1.1912
Value.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #include <trUtil/JSON/Value.h>
23 
24 #include <trUtil/JSON/Array.h>
25 #include <trUtil/JSON/Object.h>
26 
27 #include <json/value.h>
28 #include <json/writer.h>
29 
30 namespace trUtil::JSON
31 {
42  {
43  switch (val)
44  {
46  return Json::ValueType::nullValue;
47  break;
49  return Json::ValueType::intValue;
50  break;
52  return Json::ValueType::uintValue;
53  break;
55  return Json::ValueType::realValue;
56  break;
58  return Json::ValueType::stringValue;
59  break;
61  return Json::ValueType::booleanValue;
62  break;
64  return Json::ValueType::arrayValue;
65  break;
67  return Json::ValueType::objectValue;
68  break;
69  default:
70  return Json::ValueType::nullValue;
71  break;
72  }
73  }
74 
85  {
86  switch (val)
87  {
88  case Json::ValueType::nullValue:
89  return ValueType::NullValue;
90  break;
91  case Json::ValueType::intValue:
92  return ValueType::IntValue;
93  break;
94  case Json::ValueType::uintValue:
95  return ValueType::UintValue;
96  break;
97  case Json::ValueType::realValue:
98  return ValueType::RealValue;
99  break;
100  case Json::ValueType::stringValue:
101  return ValueType::StringValue;
102  break;
103  case Json::ValueType::booleanValue:
105  break;
106  case Json::ValueType::arrayValue:
107  return ValueType::ArrayValue;
108  break;
109  case Json::ValueType::objectValue:
110  return ValueType::ObjectValue;
111  break;
112  default:
113  return ValueType::NullValue;
114  break;
115  }
116  }
117 
119  Value::Value(Json::Value& value)
120  {
121  clearInternalVal = false;
122  mValuePtr = &value;
123  }
124 
127  {
128  mValuePtr = new Json::Value(ToValueType(type));
129  }
130 
133  {
134  mValuePtr = new Json::Value(value);
135  }
136 
139  {
140  mValuePtr = new Json::Value(value);
141  }
142 
145  {
146  mValuePtr = new Json::Value(value);
147  }
148 
151  {
152  mValuePtr = new Json::Value(value);
153  }
154 
156  Value::Value(double value)
157  {
158  mValuePtr = new Json::Value(value);
159  }
160 
162  Value::Value(const char* value)
163  {
164  mValuePtr = new Json::Value(value);
165  }
166 
168  Value::Value(const char* begin, const char* end)
169  {
170  mValuePtr = new Json::Value(begin, end);
171  }
172 
174  Value::Value(const std::string& value)
175  {
176  mValuePtr = new Json::Value(value);
177  }
178 
180  Value::Value(bool value)
181  {
182  mValuePtr = new Json::Value(value);
183  }
184 
186  Value::Value(const Value& other)
187  {
188  mValuePtr = new Json::Value(other);
189  }
190 
193  {
195  {
196  delete mValuePtr;
197  mValuePtr = nullptr;
198  }
199  }
200 
202  Json::Value& Value::GetJsonValue()
203  {
204  return *mValuePtr;
205  }
206 
208  const Json::Value& Value::GetJsonValue() const
209  {
210  return *mValuePtr;
211  }
212 
214  void Value::SetComment(const std::string & comment)
215  {
216  mValuePtr->setComment("//" + comment, Json::CommentPlacement::commentAfterOnSameLine);
217  }
218 
220  bool Value::HasComment() const
221  {
222  return mValuePtr->hasComment(Json::CommentPlacement::commentAfterOnSameLine);
223  }
224 
226  std::string Value::GetComment() const
227  {
228  //Get the comment in a tem variable so we can remove the // prefix
229  std::string comment = mValuePtr->getComment(Json::CommentPlacement::commentAfterOnSameLine);
230 
231  if (comment.size() > 1)
232  {
233  comment.erase(0, 2);
234  }
235 
236  return comment;
237  }
238 
241  {
242  mValuePtr->clear();
243  }
244 
247  {
248  return mValuePtr->size();
249  }
250 
252  void Value::Resize(int newSize)
253  {
254  mValuePtr->resize(newSize);
255  }
256 
258  bool Value::IsValidIndex(int index) const
259  {
260  return mValuePtr->isValidIndex(index);
261  }
262 
264  bool Value::RemoveIndex(int index, Value* removedVal)
265  {
266  return mValuePtr->removeIndex(index, &removedVal->GetJsonValue());
267  }
268 
270  Value Value::Index(int index)
271  {
272  return Value((*mValuePtr)[index]);
273  }
274 
277  {
278  return mValuePtr->getMemberNames();
279  }
280 
282  bool Value::HasMember(const std::string& key) const
283  {
284  return mValuePtr->isMember(key);
285  }
286 
288  bool Value::KeyPresent(const std::string& key) const
289  {
290  return mValuePtr->isMember(key);
291  }
292 
294  bool Value::IsNull() const
295  {
296  return mValuePtr->isNull();
297  }
298 
300  bool Value::IsNull(const int index) const
301  {
302  return (*mValuePtr)[index].isNull();
303  }
304 
306  bool Value::IsNull(const std::string& key) const
307  {
308  return (*mValuePtr)[key].isNull();
309  }
310 
312  void Value::SetNull(const std::string & key) const
313  {
314  (*mValuePtr)[key] = Json::Value();
315  }
316 
318  bool Value::IsEmpty() const
319  {
320  return mValuePtr->empty();
321  }
322 
324  bool Value::IsEmpty(const int index) const
325  {
326  return (*mValuePtr)[index].empty();
327  }
328 
330  bool Value::IsEmpty(const std::string& key) const
331  {
332  return (*mValuePtr)[key].empty();
333  }
334 
336  void Value::Append(const Value& val)
337  {
338  mValuePtr->append(*new Json::Value(val));
339  }
340 
342  bool Value::IsBool() const
343  {
344  return mValuePtr->isBool();
345  }
346 
348  bool Value::IsBool(const int index) const
349  {
350  return (*mValuePtr)[index].isBool();
351  }
352 
354  bool Value::IsBool(const std::string& key) const
355  {
356  return (*mValuePtr)[key].isBool();
357  }
358 
360  bool Value::GetBool() const
361  {
362  return mValuePtr->asBool();
363  }
364 
366  bool Value::GetBool(const int index) const
367  {
368  return (*mValuePtr)[index].asBool();
369  }
370 
372  bool Value::GetBool(const std::string& key) const
373  {
374  return (*mValuePtr)[key].asBool();
375  }
376 
378  void Value::SetBool(const std::string& key, bool value)
379  {
380  (*mValuePtr)[key] = value;
381  }
382 
384  bool Value::IsNumber() const
385  {
386  return mValuePtr->isNumeric();
387  }
388 
390  bool Value::IsNumber(const int index) const
391  {
392  return (*mValuePtr)[index].isNumeric();
393  }
394 
396  bool Value::IsNumber(const std::string& key) const
397  {
398  return (*mValuePtr)[key].isNumeric();
399  }
400 
402  bool Value::IsInt() const
403  {
404  return mValuePtr->isInt();
405  }
406 
408  bool Value::IsInt(const int index) const
409  {
410  return (*mValuePtr)[index].isInt();
411  }
412 
414  bool Value::IsInt(const std::string& key) const
415  {
416  return (*mValuePtr)[key].isInt();
417  }
418 
420  int Value::GetInt() const
421  {
422  return mValuePtr->asInt();
423  }
424 
426  int Value::GetInt(const int index) const
427  {
428  return (*mValuePtr)[index].asInt();
429  }
430 
432  int Value::GetInt(const std::string& key) const
433  {
434  return (*mValuePtr)[key].asInt();
435  }
436 
438  void Value::SetInt(const std::string& key, int value)
439  {
440  (*mValuePtr)[key] = value;
441  }
442 
444  bool Value::IsDouble() const
445  {
446  return mValuePtr->isDouble();
447  }
448 
450  bool Value::IsDouble(const int index) const
451  {
452  return (*mValuePtr)[index].isDouble();
453  }
454 
456  bool Value::IsDouble(const std::string & key) const
457  {
458  return (*mValuePtr)[key].isDouble();
459  }
460 
462  double Value::GetDouble() const
463  {
464  return mValuePtr->asDouble();
465  }
466 
468  double Value::GetDouble(const int index) const
469  {
470  return (*mValuePtr)[index].asDouble();
471  }
472 
474  double Value::GetDouble(const std::string& key) const
475  {
476  return (*mValuePtr)[key].asDouble();
477  }
478 
480  void Value::SetDouble(const std::string& key, double value)
481  {
482  (*mValuePtr)[key] = value;
483  }
484 
486  bool Value::IsUInt() const
487  {
488  return mValuePtr->isUInt();
489  }
490 
492  bool Value::IsUInt(const int index) const
493  {
494  return (*mValuePtr)[index].isUInt();
495  }
496 
498  bool Value::IsUInt(const std::string & key) const
499  {
500  return (*mValuePtr)[key].isUInt();
501  }
502 
504  unsigned int Value::GetUInt() const
505  {
506  return mValuePtr->asUInt();
507  }
508 
510  unsigned int Value::GetUInt(const int index) const
511  {
512  return (*mValuePtr)[index].asUInt();
513  }
514 
516  unsigned int Value::GetUInt(const std::string& key) const
517  {
518  return (*mValuePtr)[key].asUInt();
519  }
520 
522  void Value::SetUInt(const std::string& key, unsigned int value)
523  {
524  (*mValuePtr)[key] = value;
525  }
526 
528  bool Value::IsInt64() const
529  {
530  return mValuePtr->isInt64();
531  }
532 
534  bool Value::IsInt64(const int index) const
535  {
536  return (*mValuePtr)[index].isInt64();
537  }
538 
540  bool Value::IsInt64(const std::string& key) const
541  {
542  return (*mValuePtr)[key].isInt64();
543  }
544 
547  {
548  return mValuePtr->asInt64();
549  }
550 
552  Int64 Value::GetInt64(const int index) const
553  {
554  return (*mValuePtr)[index].asInt64();
555  }
556 
558  Int64 Value::GetInt64(const std::string& key) const
559  {
560  return (*mValuePtr)[key].asInt64();
561  }
562 
564  void Value::SetInt64(const std::string & key, Int64 value)
565  {
566  (*mValuePtr)[key] = value;
567  }
568 
570  bool Value::IsUInt64() const
571  {
572  return mValuePtr->isUInt64();
573  }
574 
576  bool Value::IsUInt64(const int index) const
577  {
578  return (*mValuePtr)[index].isUInt64();
579  }
580 
582  bool Value::IsUInt64(const std::string& key) const
583  {
584  return (*mValuePtr)[key].isUInt64();
585  }
586 
589  {
590  return mValuePtr->asUInt64();
591  }
592 
594  UInt64 Value::GetUInt64(const int index) const
595  {
596  return (*mValuePtr)[index].asUInt64();
597  }
598 
600  UInt64 Value::GetUInt64(const std::string& key) const
601  {
602  return (*mValuePtr)[key].asUInt64();
603  }
604 
606  void Value::SetUInt64(const std::string& key, UInt64 value)
607  {
608  (*mValuePtr)[key] = value;
609  }
610 
612  bool Value::IsFloat() const
613  {
614  return mValuePtr->isNumeric();
615  }
616 
618  bool Value::IsFloat(const int index) const
619  {
620  return (*mValuePtr)[index].isNumeric();
621  }
622 
624  bool Value::IsFloat(const std::string& key) const
625  {
626  return (*mValuePtr)[key].isNumeric();
627  }
628 
630  float Value::GetFloat() const
631  {
632  return mValuePtr->asFloat();
633  }
634 
636  float Value::GetFloat(const int index) const
637  {
638  return (*mValuePtr)[index].asFloat();
639  }
640 
642  float Value::GetFloat(const std::string& key) const
643  {
644  return (*mValuePtr)[key].asFloat();
645  }
646 
648  void Value::SetFloat(const std::string& key, float value)
649  {
650  (*mValuePtr)[key] = value;
651  }
652 
654  bool Value::IsString() const
655  {
656  return mValuePtr->isString();
657  }
658 
660  bool Value::IsString(const int index) const
661  {
662  return (*mValuePtr)[index].isString();
663  }
664 
666  bool Value::IsString(const std::string& key) const
667  {
668  return (*mValuePtr)[key].isString();
669  }
670 
672  const std::string Value::GetString() const
673  {
674  return mValuePtr->asString();
675  }
676 
678  const std::string Value::GetString(const int index) const
679  {
680  return (*mValuePtr)[index].asString();
681  }
682 
684  const std::string Value::GetString(const std::string& key) const
685  {
686  return (*mValuePtr)[key].asString();
687  }
688 
690  void Value::SetString(const std::string& key, const std::string& value)
691  {
692  (*mValuePtr)[key] = value;
693  }
694 
696  bool Value::IsArray() const
697  {
698  return mValuePtr->isArray();
699  }
700 
702  bool Value::IsArray(const int index) const
703  {
704  return (*mValuePtr)[index].isArray();
705  }
706 
708  bool Value::IsArray(const std::string& key) const
709  {
710  return (*mValuePtr)[key].isArray();
711  }
712 
714  const Array Value::GetArray() const
715  {
716  Array tempArray(*mValuePtr);
717  return tempArray;
718  }
719 
721  const Array Value::GetArray(const int index) const
722  {
723  Array tempArray((*mValuePtr)[index]);
724  return tempArray;
725  }
726 
728  const Array Value::GetArray(const std::string& key) const
729  {
730  Array tempArray((*mValuePtr)[key]);
731  return tempArray;
732  }
733 
735  void Value::SetArray(const std::string& key, Array& value)
736  {
737  (*mValuePtr)[key] = value.GetJSONRoot();
738  }
739 
741  bool Value::IsObject() const
742  {
743  return mValuePtr->isObject();
744  }
745 
747  bool Value::IsObject(const int index) const
748  {
749  return (*mValuePtr)[index].isObject();
750  }
751 
753  bool Value::IsObject(const std::string& key) const
754  {
755  return (*mValuePtr)[key].isObject();
756  }
757 
759  const Object Value::GetObject() const
760  {
761  Object tempObject(*mValuePtr);
762  return tempObject;
763  }
764 
766  const Object Value::GetObject(const int index) const
767  {
768  Object tempObject((*mValuePtr)[index]);
769  return tempObject;
770  }
771 
773  const Object Value::GetObject(const std::string& key) const
774  {
775  Object tempObject((*mValuePtr)[key]);
776  return tempObject;
777  }
778 
780  void Value::SetObject(const std::string& key, Object& value)
781  {
782  (*mValuePtr)[key] = value.GetJSONRoot();
783  }
784 
786  Value::operator Json::Value() const
787  {
788  return *mValuePtr;
789  }
790 
792  Value::operator Json::Value&()
793  {
794  return *mValuePtr;
795  }
796 
798  Value::operator const Json::Value&() const
799  {
800  return *mValuePtr;
801  }
802 
804  Value::operator Json::Value*()
805  {
806  return mValuePtr;
807  }
808 
810  //Value Value::operator[](int index)
811  //{
812  // return Value((*mValuePtr)[index]);
813  //}
814 
816  //const Value Value::operator[](int index) const
817  //{
818  // return Value((*mValuePtr)[index]);
819  //}
820 
822  //Value Value::operator[](const char* key)
823  //{
824  // return Value((*mValuePtr)[key]);
825  //}
826 
828  //const Value Value::operator[](const char* key) const
829  //{
830  // return Value((*mValuePtr)[key]);
831  //}
832 
834  //Value Value::operator[](const std::string& key)
835  //{
836  // //return Value(mValuePtr->operator[](key));
837 // Json::Value val = (*mValuePtr)[key];
838 // Value newVal(val);
839 // return newVal;
840  //}
841 
843  //const Value Value::operator[](const std::string& key) const
844  //{
845  // return Value((*mValuePtr)[key]);
846  //}
847 
850  {
851  mValuePtr->operator=(other);
852  return *this;
853  }
854 
856  std::ostream& operator<<(std::ostream& out, const Value& root)
857  {
858  return Json::operator<<(out, root);
859  }
860 }
virtual bool IsArray() const
Checks if the value stored is an Array.
Definition: Value.cpp:696
virtual bool IsUInt64() const
Checks if the value stored is a 64bit Unsigned Integer.
Definition: Value.cpp:570
virtual bool IsBool() const
Checks if the value stored is a Boolean.
Definition: Value.cpp:342
virtual void SetInt64(const std::string &key, Int64 value)
Sets the Int64 value in the Object.
Definition: Value.cpp:564
std::string GetComment() const
Returns the internal comment.
Definition: Value.cpp:226
int Size()
Returns the size of the array.
Definition: Value.cpp:246
virtual bool IsInt() const
Checks if the value stored is an Integer.
Definition: Value.cpp:402
virtual bool KeyPresent(const std::string &key) const
Checks if the JSON Root Node has an entry with a given key present.
Definition: Value.cpp:288
void Resize(int newSize)
Change the size of the array.
Definition: Value.cpp:252
array value (ordered list)
Definition: Value.h:98
virtual bool IsUInt() const
Checks if the value stored Unsigned Integer.
Definition: Value.cpp:486
bool IsValidIndex(int index) const
Return true if index < Size()
Definition: Value.cpp:258
virtual bool IsFloat() const
Checks if the value stored is a float.
Definition: Value.cpp:612
virtual unsigned int GetUInt() const
Returns the Unsigned Integer value.
Definition: Value.cpp:504
bool clearInternalVal
True to clear internal value.
Definition: Value.h:1320
TR_BASE_EXPORT std::ostream & operator<<(std::ostream &ios, const Matrixd &q)
Stream insertion operator.
unsigned integer value
Definition: Value.h:94
A JSON array.
Definition: Array.h:46
ValueType
Type of the value held by a Value object.
Definition: Value.h:89
virtual void SetUInt(const std::string &key, unsigned int value)
Sets the Unsigned Int value in the Object.
Definition: Value.cpp:522
virtual int GetInt() const
Returns the Integer value.
Definition: Value.cpp:420
bool RemoveIndex(int index, Value *removedVal)
Remove the given Index value.
Definition: Value.cpp:264
virtual void SetString(const std::string &key, const std::string &value)
Sets the String value in the Object.
Definition: Value.cpp:690
virtual bool IsString() const
Checks if the value stored is a String.
Definition: Value.cpp:654
virtual void Clear()
Clears the internal JSON Root node.
Definition: Value.cpp:240
virtual Value & GetJSONRoot() override
Returns a reference to the internal JSON Root node.
Definition: Object.cpp:75
Represents a JSON value.
Definition: Value.h:139
int64_t Int64
The fourth int 6.
Definition: Value.h:72
virtual double GetDouble() const
Returns the Double value.
Definition: Value.cpp:462
virtual bool IsEmpty() const
Return true if empty array, empty object, or null, otherwise, false.
Definition: Value.cpp:318
virtual bool IsNull() const
Checks if the value stored is a NULL.
Definition: Value.cpp:294
virtual void SetNull(const std::string &key) const
Sets the NULL value in the Object.
Definition: Value.cpp:312
virtual bool GetBool() const
Returns the Boolean value.
Definition: Value.cpp:360
Value & operator=(Value other)
// Access an object value by name, returns null if there is no member with // that name...
Definition: Value.cpp:849
virtual Value & GetJSONRoot() override
Returns a reference to the internal JSON Root node.
Definition: Array.cpp:100
Forward declaration.
Definition: Object.h:46
virtual void SetUInt64(const std::string &key, UInt64 value)
Sets the Unsigned Int64 value in the Object.
Definition: Value.cpp:606
virtual bool IsDouble() const
Checks if the value stored is a Double.
Definition: Value.cpp:444
unsigned int UInt
The int.
Definition: Value.h:57
virtual float GetFloat() const
Returns the float value stored.
Definition: Value.cpp:630
virtual UInt64 GetUInt64() const
Returns the Unsigned 64bit Integer value.
Definition: Value.cpp:588
virtual void Append(const Value &val)
Add a value at the end of the array.
Definition: Value.cpp:336
virtual void SetInt(const std::string &key, int value)
Sets the Integer value in the Object.
Definition: Value.cpp:438
Json::ValueType ToValueType(ValueType val)
Converts JSON::ValueType to Json::ValueType.
Definition: Value.cpp:41
int Int
The int.
Definition: Value.h:55
Value(Json::Value &value)
ctor.
Definition: Value.cpp:119
double value
Definition: Value.h:95
virtual void SetObject(const std::string &key, Object &value)
Sets the Object value in the Object.
Definition: Value.cpp:780
virtual const Array GetArray() const
Returns the Array value stored.
Definition: Value.cpp:714
void SetComment(const std::string &comment)
Adds a comment to the internal value.
Definition: Value.cpp:214
const Value::Members GetMemberNames() const
Returns the value names contained in the object.
Definition: Value.cpp:276
virtual void SetFloat(const std::string &key, float value)
Sets the Float value in the Object.
Definition: Value.cpp:648
virtual void SetBool(const std::string &key, bool value)
Sets the Boolean value in the Object.
Definition: Value.cpp:378
virtual bool IsInt64() const
Checks if the value stored is a 64bit Integer.
Definition: Value.cpp:528
&#39;null&#39; value
Definition: Value.h:92
Value Index(int index)
Returns the Value at the given index if this Value is an Array The return is by value, but the internal json object is stored by reference.
Definition: Value.cpp:270
virtual const Object GetObject() const
Returns the Object value stored.
Definition: Value.cpp:759
virtual void SetArray(const std::string &key, Array &value)
Sets the Array value in the Object.
Definition: Value.cpp:735
signed integer value
Definition: Value.h:93
virtual void SetDouble(const std::string &key, double value)
Sets the Double value in the Object.
Definition: Value.cpp:480
bool HasMember(const std::string &key) const
Returns true if a member with the passed in key is present.
Definition: Value.cpp:282
Json::Value * mValuePtr
The value pointer.
Definition: Value.h:1322
TR_UTIL_EXPORT std::ostream & operator<<(std::ostream &, const Value &root)
OStream operator.
Definition: Value.cpp:856
virtual bool IsObject() const
Checks if the value stored is an Object.
Definition: Value.cpp:741
virtual const std::string GetString() const
Returns the String value stored.
Definition: Value.cpp:672
bool value
Definition: Value.h:97
uint64_t UInt64
The fourth u int 6.
Definition: Value.h:74
std::vector< std::string > Members
The members.
Definition: Value.h:144
virtual Int64 GetInt64() const
Returns the 64bit Integer value.
Definition: Value.cpp:546
bool HasComment() const
Checks if the internal value has a comment.
Definition: Value.cpp:220
UTF-8 string value.
Definition: Value.h:96
Json::Value & GetJsonValue()
Returns a reference to the internal Json::Value.
Definition: Value.cpp:202
virtual bool IsNumber() const
Checks if the value stored is a Number.
Definition: Value.cpp:384