Conscience Core
json.h
Go to the documentation of this file.
1 #ifndef csc_json_H_
2 #define csc_json_H_
3 
4 #include "Util/conscience_log.h"
5 
6 #include <any>
7 #include <variant>
8 #include <exception>
9 #include <filesystem>
10 #include <memory>
11 #include <optional>
12 #include <type_traits>
13 #include <typeinfo>
14 #include <yyjson.h>
15 
16 using std::string, std::optional, std::exception, std::unique_ptr, std::vector, std::any, std::make_shared;
17 namespace fs = std::filesystem;
18 template <class T>
19 using ptr = std::shared_ptr<T>;
20 
21 using namespace conscience_utils;
22 
23 namespace conscience_utils::JSON {
24 
25 #define JSON_BASE_TYPES std::nullptr_t, const char *, char *, bool, unsigned, int, long long, unsigned long long, string, float, double
26 
27 struct JsonSerializableData;
28 
29 using JsonArray = std::vector<JsonSerializableData>;
30 using JsonObject = std::map<std::string, JsonSerializableData>;
31 struct JsonArrayBox {
33 };
34 
35 struct JsonObjectBox {
37 };
38 
39 using JsonValue = std::variant<
43 
44 namespace typing_helper {
45 template <class T>
46 using decay_t = std::decay_t<T>;
47 
48 template <class T>
49 inline constexpr bool is_scalar_json_v =
50  std::is_same_v<decay_t<T>, std::nullptr_t> ||
51  std::is_same_v<decay_t<T>, const char *> ||
52  std::is_same_v<decay_t<T>, char *> ||
53  std::is_same_v<decay_t<T>, bool> ||
54  std::is_same_v<decay_t<T>, unsigned> ||
55  std::is_same_v<decay_t<T>, int> ||
56  std::is_same_v<decay_t<T>, long long> ||
57  std::is_same_v<decay_t<T>, unsigned long long> ||
58  std::is_same_v<decay_t<T>, std::string> ||
59  std::is_same_v<decay_t<T>, float> ||
60  std::is_same_v<decay_t<T>, double>;
61 
62 template <class T>
63 inline constexpr bool excluded_v =
64  std::is_same_v<decay_t<T>, JsonSerializableData> ||
65  std::is_same_v<decay_t<T>, JsonObject::value_type> ||
66  std::is_same_v<decay_t<T>, JsonArray> ||
67  std::is_same_v<decay_t<T>, JsonObject>;
68 
69 }
70 
72 public:
74 
75  JsonSerializableData() = default;
76  JsonSerializableData(const JsonSerializableData &) = default;
77  JsonSerializableData(JsonSerializableData &&) noexcept = default;
78  JsonSerializableData &operator=(const JsonSerializableData &) = default;
79  JsonSerializableData &operator=(JsonSerializableData &&) noexcept = default;
80 
81  // enums -> store as (u)int64 to keep std::variant construction stable across platforms
82  template <class E>
83  requires(std::is_enum_v<typing_helper::decay_t<E>> && !typing_helper::excluded_v<E>)
84  JsonSerializableData(E &&e) {
85  using U = std::underlying_type_t<typing_helper::decay_t<E>>;
86  if constexpr (std::is_signed_v<U>) {
87  value = static_cast<long long>(static_cast<U>(e));
88  } else {
89  value = static_cast<unsigned long long>(static_cast<U>(e));
90  }
91  }
92 
93  // scalar json types (non-enum)
94  template <class T>
95  requires(!std::is_enum_v<typing_helper::decay_t<T>> &&
96  typing_helper::is_scalar_json_v<T> &&
97  !typing_helper::excluded_v<T>)
99  : value(std::forward<T>(v)) {
100  }
101 
102  // array
104  : value(JsonArrayBox{v}) {
105  }
106 
108  : value(JsonArrayBox{std::move(v)}) {
109  }
110 
111  // object
113  : value(JsonObjectBox{v}) {
114  }
115 
117  : value(JsonObjectBox{std::move(v)}) {
118  }
119 
120  JsonSerializableData(std::initializer_list<JsonObject::value_type> init)
121  : value(JsonObjectBox{JsonObject(init)}) {
122  }
123 
124  JsonSerializableData(std::initializer_list<JsonSerializableData> init)
125  : value(JsonArrayBox{JsonArray(init)}) {
126  }
127 
128  operator JsonValue &() {
129  return value;
130  }
131 };
132 
133 string vectorToJson(const vector<JsonSerializableData> &list, bool noNewLines = false);
134 string mapToJson(
135  std::initializer_list<JsonObject::value_type> init,
136  bool noNewLines = false);
137 string mapToJson(const map<string, JsonSerializableData> &entries, bool noNewLines = false);
138 string pairsToJson(const vector<pair<string, JsonSerializableData>> &entries, bool noNewLines = false);
139 string jsonSerializableDataToString(JsonSerializableData data);
140 
141 template <class TValue>
142 JsonSerializableData vectorToJsonSerializable(const vector<TValue> &theVector) {
143  JsonArray out;
144  out.reserve(theVector.size());
145  for (const auto &v : theVector) {
146  out.emplace_back(static_cast<TValue>(v));
147  }
148  return JsonSerializableData(std::move(out));
149 }
150 
151 inline map<string, JsonSerializableData>
153  std::initializer_list<map<string, JsonSerializableData>::value_type> init) {
154  return map<string, JsonSerializableData>(init);
155 }
156 
157 template <class TValue>
158 map<string, JsonSerializableData> mapToJsonSerializable(const map<string, TValue> &theMap) {
159  map<string, JsonSerializableData> mapCopy;
160  for (auto entry : theMap) {
161  mapCopy[entry.first] = entry.second;
162  }
163  return mapCopy;
164 }
165 
166 class MissingRequiredPropertyException : public exception {
167 public:
168  MissingRequiredPropertyException(const string message) throw()
169  : message(message) {
170  }
171 
172  const char *what() const throw() {
173  return message.c_str();
174  }
175 
176  const string getMessage() const {
177  return message;
178  }
179 
180 private:
181  const string message;
182 };
183 
184 template <class T>
185 T getNumericValueFromJson(yyjson_val *jsonValue) {
186  ASSERT("given value is not numeric - " + to_string(yyjson_get_type(jsonValue)), yyjson_get_type(jsonValue) == YYJSON_TYPE_NUM);
187  switch (yyjson_get_subtype(jsonValue)) {
188  case YYJSON_SUBTYPE_UINT:
189  return (T)yyjson_get_uint(jsonValue);
190  case YYJSON_SUBTYPE_SINT:
191  return (T)yyjson_get_sint(jsonValue);
192  default:
193  case YYJSON_SUBTYPE_REAL:
194  return (T)yyjson_get_real(jsonValue);
195  }
196 }
197 
198 class CscJsonObject;
199 
204 public:
205  vector<string> getKeys();
206  size_t countProperties();
207 
211  bool hasKey(const string &key);
212 
216  bool isArray(const string &key);
217 
221  bool isArray();
222 
226  bool isObject();
227 
231  bool isString();
232 
236  bool isBool();
237 
241  bool isNumeric();
242 
246  bool isInteger();
247 
251  bool isReal();
255  bool isNull();
259  unsigned getArrayLength(const string &arrayKey);
260 
264  bool isNull(const string &key);
265 
269  string getRequiredStringValue(const string &key, bool allowEmpty = false);
270 
274  optional<int> getIntValue(const string &key);
275 
279  int getRequiredIntValue(const string &key);
280 
284  optional<unsigned long long> getUnsignedLongValue(const string &key);
285 
289  unsigned long long getRequiredUnsignedLongValue(const string &key);
290 
291  optional<bool> getBooleanValue(const string &key);
292 
293  bool getRequiredBooleanValue(const string &key);
294 
298  optional<double> getDoubleValue(const string &key);
299 
303  double getRequiredDoubleValue(const string &key);
304 
308  optional<float> getFloatValue(const string &key);
309 
313  float getRequiredFloatValue(const string &key);
314 
318  optional<string> getStringValue(const string &key);
319 
323  CscJsonObjectReader getRequiredObjectValue(const string &key);
324 
328  optional<CscJsonObjectReader> getObjectValue(const string &key);
329 
334 
338  optional<CscJsonObjectReader *> getObjectPtrValue(const string &key);
339 
344 
348  optional<ptr<CscJsonObjectReader>> getObjectSharedPtrValue(const string &key);
349 
353  map<string, optional<any>> getRequiredObjectValueAsMap(const string &key);
354 
359  map<string, string> getRequiredObjectValueAsMapOfStrings(const string &key);
360 
364  map<string, optional<any>> asMap();
365 
370  map<string, string> asMapOfStrings();
371 
375  optional<map<string, optional<any>>> getObjectValueAsMap(const string &key);
376 
380  optional<any> getAnyValue(string key);
381 
385  template <class T>
386  vector<T> getRequiredArrayValue(const string &key) {
387  optional<vector<T>> value = getArrayValue<T>(key);
388  if (!value.has_value()) {
389  throw MissingRequiredPropertyException("required array property " + key + " cannot be found in " + toString());
390  }
391 
392  return value.value();
393  }
394 
398  template <class T>
399  optional<vector<T>> getArrayValue(const string &key) {
400  try {
401  yyjson_val *value = getValueByPropertyPath(key);
402  return toArrayValue<T>(value, getYyjsonDoc());
403  } catch (const std::invalid_argument e) {
404  LOG_WARN(key + " cannot be read as array: " + string(e.what()));
405  }
406  return {};
407  }
408 
412  template <class T>
413  optional<T> getArrayItemValue(const string &arrayKey, size_t index) {
414  optional<T> result = {};
415  try {
416  yyjson_val *value = getValueByPropertyPath(arrayKey);
417  if (value != nullptr) {
418  yyjson_val *itemValue = yyjson_arr_get(value, index);
419  if (itemValue != nullptr) {
420  result = toValue<T>(itemValue, getYyjsonDoc());
421  }
422  }
423  } catch (const std::invalid_argument e) {
424  LOG_WARN("cannot getArrayItemValue " + arrayKey + " : " + string(e.what()));
425  }
426  return result;
427  }
431  template <class T>
432  T getRequiredArrayItemValue(const string &arrayKey, size_t index) {
433  optional<T> value = getArrayItemValue<T>(arrayKey, index);
434  if (!value.has_value()) {
435  throw MissingRequiredPropertyException("required array item: " + arrayKey + "[" + to_string(index) + "] cannot be found in " + toString());
436  }
437 
438  return value.value();
439  }
440 
444  template <class T>
445  vector<T> asArrayValue() {
446  return toArrayValue<T>(getYyjsonValue(), getYyjsonDoc()).value();
447  }
448 
453  CscJsonObjectReader(ptr<yyjson_doc> yyjsonDoc, yyjson_val *yyjsonValue = nullptr, bool doNotReleaseYyjsonObjects = false);
454  CscJsonObjectReader(const fs::path filePath);
455  CscJsonObjectReader(const string &jsonContent);
456 
457  virtual ~CscJsonObjectReader();
458 
459  string toString();
460 
461  string toJsonString(bool prettyPrint = false);
462 
466  CscJsonObject *toMutable(yyjson_mut_doc *parentDoc = nullptr);
467  static vector<CscJsonObject *> toMutableList(const vector<ptr<CscJsonObjectReader>> values);
468 
472  yyjson_val *getLowLevelParser();
473 
474 protected:
475  static ptr<yyjson_doc> createYyjsonSharedPtr(yyjson_doc *doc);
477 
478  yyjson_val *yyjsonValue = nullptr;
479  virtual ptr<yyjson_doc> getYyjsonDoc();
480  virtual yyjson_val *getYyjsonValue();
481 
482  yyjson_val *getValueByPropertyPath(const string &propertyPath);
483  static yyjson_doc *parseFromFile(const fs::path filePath);
484  static yyjson_doc *parseFromString(const string &jsonString);
485 
486 private:
487  const bool doNotReleaseYyjsonObjects;
488 
489  optional<any> jsonValueToAny(yyjson_val *jsonValue);
490 
491  template <class T>
492  static inline T toValue(yyjson_val *yyjsonValue, ptr<yyjson_doc> jsonDoc) {
493  if constexpr (std::is_arithmetic<T>::value) {
494  return getNumericValueFromJson<T>(yyjsonValue);
495  }
496  if constexpr (std::is_same<T, string>::value) {
497  const char *valueChars = yyjson_get_str(yyjsonValue);
498  return string(valueChars);
499  }
500  if constexpr (std::is_same_v<T, const CscJsonObjectReader *> || std::is_same_v<T, CscJsonObjectReader *>) {
501  return new CscJsonObjectReader(jsonDoc, yyjsonValue, true);
502  }
503  if constexpr (std::is_base_of<ptr<CscJsonObjectReader>, T>::value) {
504  return std::make_shared<CscJsonObjectReader>(jsonDoc, yyjsonValue, true);
505  }
506  throw new std::invalid_argument(
507  "unsupported data type " + string(typeid(T).name()) + " for array " +
508  string(yyjsonValue != nullptr ? string(yyjson_get_raw(yyjsonValue)) : "?"));
509  }
510 
511  template <class T>
512  static optional<vector<T>> toArrayValue(yyjson_val *arrayJsonValue, ptr<yyjson_doc> jsonDoc) {
513  try {
514  if (arrayJsonValue != nullptr && yyjson_get_type(arrayJsonValue) == YYJSON_TYPE_ARR) {
515  vector<T> items;
516 
517  string ss = string(yyjson_val_write(arrayJsonValue, YYJSON_WRITE_NOFLAG, NULL));
518 
519  yyjson_val *arrayItem;
520  yyjson_arr_iter iter = yyjson_arr_iter_with(arrayJsonValue);
521  while ((arrayItem = yyjson_arr_iter_next(&iter))) {
522  items.push_back(toValue<T>(arrayItem, jsonDoc));
523  }
524 
525  return items;
526  }
527  } catch (const std::invalid_argument e) {
528  LOG_WARN(string(arrayJsonValue != nullptr ? string(yyjson_get_raw(arrayJsonValue)) : "?") + " cannot be read as array: " + string(e.what()));
529  }
530  return {};
531  }
532 };
533 
535 public:
536  CscJsonParser(const fs::path &filePath);
537  CscJsonParser(const string &jsonContent);
538 };
539 
545 public:
546  CscJsonObject(const fs::path &jsonFilePath);
547  CscJsonObject(const string &jsonString);
548 
552  CscJsonObject(const map<string, string> &map);
553 
557  CscJsonObject(yyjson_mut_doc *existingDoc, yyjson_mut_val *existingValue, bool doNotReleaseYyjsonObjects = false, CscJsonObject *lookupParent = nullptr);
558 
562  CscJsonObject();
563  ~CscJsonObject();
564 
568  CscJsonObject getRequiredObjectMutableValue(const string &key);
569 
573  optional<CscJsonObject> getObjectMutableValue(const string &key);
574 
579 
583  optional<CscJsonObject *> getObjectMutablePtrValue(const string &key);
584 
589 
593  optional<ptr<CscJsonObject>> getObjectMutableSharedPtrValue(const string &key);
594 
595  CscJsonObject *set(const string &propertyPath, bool value);
596  CscJsonObject *set(const string &propertyPath, const char *value);
597  CscJsonObject *set(const string &propertyPath, const string &value);
598  CscJsonObject *set(const string &propertyPath, double value);
599  CscJsonObject *set(const string &propertyPath, int value);
600  CscJsonObject *set(const string &propertyPath, long long value);
601  CscJsonObject *set(const string &propertyPath, unsigned long long value);
602  CscJsonObject *set(const string &propertyPath, CscJsonObject &object);
606  CscJsonObject *set(const string &propertyPath, CscJsonObject *object);
607  CscJsonObject *set(const string &propertyPath, ptr<CscJsonObject> object);
608  CscJsonObject *set(const string &propertyPath, const map<string, string> &value);
609 
610  CscJsonObject *setArray(const string &propertyPath, const vector<bool> &value);
611  CscJsonObject *setArray(const string &propertyPath, const vector<string> &value);
612  CscJsonObject *setArray(const string &propertyPath, const vector<float> &value);
613  CscJsonObject *setArray(const string &propertyPath, const vector<double> &value);
614  CscJsonObject *setArray(const string &propertyPath, const vector<int> &value);
615  CscJsonObject *setArray(const string &propertyPath, const vector<long long> &value);
616  CscJsonObject *setArray(const string &propertyPath, const vector<unsigned long long> &value);
620  CscJsonObject *setArray(const string &propertyPath, const vector<CscJsonObject *> &objects);
621  CscJsonObject *setArray(const string &propertyPath, const vector<CscJsonObject> &objects);
622  CscJsonObject *setArray(const string &propertyPath, const vector<ptr<CscJsonObject>> &objects);
623  CscJsonObject *setArray(const string &propertyPath, const vector<map<string, string>> &value);
624 
628  CscJsonObject *remove(const string &propertyPath);
629 
630  void saveToFile(const fs::path &outPath, bool lineBreakAndTabulation = false);
631 
665  static string addLineBreakAndTabulation(const string &json);
666 
667  yyjson_mut_val *getLowLevelMutableObject();
668 
673  void mergeWith(CscJsonObject &other, bool deep = true);
674 
675  void mergeKeyWith(CscJsonObject &other, const string &subPropertyPath, bool deep = true);
676 
677 private:
678  bool doNotReleaseYyjsonObjects = false;
679  void onModified();
683  CscJsonObject *lookupParent = nullptr;
684 
685 protected:
693  yyjson_mut_doc *yyjsonMutableDoc = nullptr;
694 
698  yyjson_mut_val *yyjsonMutableValue = nullptr;
699 
703  virtual yyjson_val *getYyjsonValue() override;
707  virtual ptr<yyjson_doc> getYyjsonDoc() override;
711  yyjson_mut_doc *getYyjsonMutableDoc();
715  yyjson_mut_val *getYyjsonMutableValue();
716 
717  yyjson_mut_val *getMutableValueByPropertyPath(const string &propertyPath);
718 
719  yyjson_mut_val *getOrCreateObjectByPropertyPath(const string &propertyPath);
720 
721  CscJsonObject *set(const string &propertyPath, yyjson_mut_val *newValue);
722 
723  void mergeObjects(yyjson_mut_val *thisObj, yyjson_mut_val *otherObj, bool deep);
724 };
725 
726 template <class T>
727 concept IsString = std::is_same_v<T, string>;
728 template <IsString T>
729 yyjson_mut_val *toJsonValue(const T &value, yyjson_mut_doc *doc) {
730  char *stringCopy = strdup(value.c_str());
731  return yyjson_mut_str(doc, stringCopy);
732 }
733 
734 template <class T>
735 concept IsNotString = !IsString<T>;
736 template <IsNotString T>
737 yyjson_mut_val *toJsonValue(const T &value, yyjson_mut_doc *doc);
738 
739 inline yyjson_mut_val *mapToJsonObject(const map<string, string> &map, yyjson_mut_doc *doc) {
740  yyjson_mut_val *mapJsonValue = yyjson_mut_obj(doc);
741 
742  for (auto [key, value] : map) {
743  yyjson_mut_val *keyJsonValue = toJsonValue(key, doc);
744  yyjson_mut_val *valueJsonValue = toJsonValue(value, doc);
745  yyjson_mut_obj_put(mapJsonValue, keyJsonValue, valueJsonValue);
746  }
747 
748  return mapJsonValue;
749 }
750 
751 template <IsNotString T>
752 yyjson_mut_val *toJsonValue(T &value, yyjson_mut_doc *doc) {
753 
754  if constexpr (std::is_arithmetic<T>::value) {
755  if constexpr (std::is_unsigned<T>::value) {
756  return yyjson_mut_uint(doc, value);
757  } else if constexpr (std::is_floating_point<T>::value) {
758  return yyjson_mut_real(doc, value);
759  } else {
760  return yyjson_mut_sint(doc, value);
761  }
762 
763  } else if constexpr (std::is_same_v<T, bool>) {
764  return yyjson_mut_bool(doc, value);
765 
766  } else if constexpr (std::is_same_v<ptr<CscJsonObject>, T> || std::is_same_v<ptr<const CscJsonObject>, T>) {
767  return yyjson_mut_val_mut_copy(doc, value->getLowLevelMutableObject());
768 
769  } else if constexpr (std::is_same_v<CscJsonObject, T>) {
770  return yyjson_mut_val_mut_copy(doc, value.getLowLevelMutableObject());
771 
772  } else if constexpr (std::is_same_v<CscJsonObject *, T> || std::is_same_v<const CscJsonObject *, T>) {
773  return yyjson_mut_val_mut_copy(doc, value->getLowLevelMutableObject());
774 
775  } else if constexpr (std::is_same_v<map<string, string>, T>) {
776  return mapToJsonObject(value, doc);
777  } else {
778  throw runtime_error("unsupported type ");
779  }
780 }
781 
782 template <class T>
783 yyjson_mut_val *toJsonValue(const vector<T> &valueList, yyjson_mut_doc *doc, bool clone = false) {
784  yyjson_mut_val *arrayValue = yyjson_mut_arr(doc);
785  for (T value : valueList) {
786  yyjson_mut_val *jsonValue = toJsonValue<T>(value, doc);
787  yyjson_mut_val *newValue = jsonValue;
788  if (clone) {
789  newValue = yyjson_mut_val_mut_copy(doc, jsonValue);
790  }
791  bool success = yyjson_mut_arr_append(arrayValue, newValue);
792  if (!success) {
793  throw runtime_error("cannot add item to array");
794  }
795  }
796  return arrayValue;
797 }
798 
799 }
800 
801 #endif
conscience_utils::JSON::CscJsonObjectReader::asMapOfStrings
map< string, string > asMapOfStrings()
Definition: json.cpp:384
conscience_utils::JSON::JsonSerializableData
Definition: json.h:71
conscience_utils::JSON::mapToJsonObject
yyjson_mut_val * mapToJsonObject(const map< string, string > &map, yyjson_mut_doc *doc)
Definition: json.h:739
conscience_log.h
conscience_utils::JSON::toJsonValue
yyjson_mut_val * toJsonValue(const T &value, yyjson_mut_doc *doc)
Definition: json.h:729
conscience_utils::JSON::JsonSerializableData::value
JsonValue value
Definition: json.h:73
conscience_utils::JSON::CscJsonObjectReader::getObjectValue
optional< CscJsonObjectReader > getObjectValue(const string &key)
Definition: json.cpp:281
nlohmann::to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:26470
conscience_utils::JSON::CscJsonObjectReader::isInteger
bool isInteger()
Definition: json.cpp:37
conscience_utils::JSON::CscJsonObjectReader::~CscJsonObjectReader
virtual ~CscJsonObjectReader()
Definition: json.cpp:69
conscience_utils::JSON::CscJsonObjectReader::getBooleanValue
optional< bool > getBooleanValue(const string &key)
Definition: json.cpp:201
conscience_utils::JSON::CscJsonObject::getObjectMutableValue
optional< CscJsonObject > getObjectMutableValue(const string &key)
Definition: json.cpp:943
conscience_utils::JSON::CscJsonObjectReader::getAnyValue
optional< any > getAnyValue(string key)
Definition: json.cpp:393
conscience_utils::JSON::mapToJsonSerializable
map< string, JsonSerializableData > mapToJsonSerializable(std::initializer_list< map< string, JsonSerializableData >::value_type > init)
Definition: json.h:152
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectSharedPtrValue
ptr< CscJsonObjectReader > getRequiredObjectSharedPtrValue(const string &key)
Definition: json.cpp:314
conscience_utils::JSON::CscJsonObjectReader::asArrayValue
vector< T > asArrayValue()
Definition: json.h:445
conscience_utils::JSON::CscJsonObject::remove
CscJsonObject * remove(const string &propertyPath)
Definition: json.cpp:710
conscience_utils::JSON::CscJsonObject::getLowLevelMutableObject
yyjson_mut_val * getLowLevelMutableObject()
Definition: json.cpp:572
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectValue
CscJsonObjectReader getRequiredObjectValue(const string &key)
Definition: json.cpp:272
conscience_utils::JSON::JsonValue
std::variant< JSON_BASE_TYPES, JsonArrayBox, JsonObjectBox > JsonValue
Definition: json.h:42
conscience_utils::JSON::CscJsonObject::getObjectMutablePtrValue
optional< CscJsonObject * > getObjectMutablePtrValue(const string &key)
Definition: json.cpp:963
conscience_utils::JSON::CscJsonObject::set
CscJsonObject * set(const string &propertyPath, bool value)
Definition: json.cpp:576
conscience_utils::JSON::CscJsonObjectReader::getYyjsonDoc
virtual ptr< yyjson_doc > getYyjsonDoc()
Definition: json.cpp:55
conscience_utils::JSON::CscJsonObjectReader::getIntValue
optional< int > getIntValue(const string &key)
Definition: json.cpp:231
conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableSharedPtrValue
ptr< CscJsonObject > getRequiredObjectMutableSharedPtrValue(const string &key)
Definition: json.cpp:975
conscience_utils::JSON::CscJsonObjectReader::parseFromFile
static yyjson_doc * parseFromFile(const fs::path filePath)
Definition: json.cpp:85
conscience_utils::JSON::CscJsonObjectReader::getRequiredDoubleValue
double getRequiredDoubleValue(const string &key)
Definition: json.cpp:162
conscience_utils::JSON::JsonObject
std::map< std::string, JsonSerializableData > JsonObject
Definition: json.h:30
conscience_utils::JSON::CscJsonObject::~CscJsonObject
~CscJsonObject()
Definition: json.cpp:511
conscience_utils::JSON::CscJsonObjectReader::getObjectSharedPtrValue
optional< ptr< CscJsonObjectReader > > getObjectSharedPtrValue(const string &key)
Definition: json.cpp:323
conscience_utils::JSON::CscJsonObject::getRequiredObjectMutablePtrValue
CscJsonObject * getRequiredObjectMutablePtrValue(const string &key)
Definition: json.cpp:954
conscience_utils::JSON::CscJsonObjectReader::CscJsonObjectReader
CscJsonObjectReader(ptr< yyjson_doc > yyjsonDoc, yyjson_val *yyjsonValue=nullptr, bool doNotReleaseYyjsonObjects=false)
Definition: json.cpp:9
conscience_utils::JSON::CscJsonObjectReader::getLowLevelParser
yyjson_val * getLowLevelParser()
Definition: json.cpp:335
conscience_utils::JSON::CscJsonObjectReader::getArrayValue
optional< vector< T > > getArrayValue(const string &key)
Definition: json.h:399
conscience_utils::JSON::CscJsonObject::getYyjsonMutableDoc
yyjson_mut_doc * getYyjsonMutableDoc()
Definition: json.cpp:547
conscience_utils::JSON::CscJsonObject::getYyjsonMutableValue
yyjson_mut_val * getYyjsonMutableValue()
Definition: json.cpp:560
conscience_utils::JSON::CscJsonObjectReader::toMutableList
static vector< CscJsonObject * > toMutableList(const vector< ptr< CscJsonObjectReader >> values)
Definition: json.cpp:926
conscience_utils::JSON::IsNotString
concept IsNotString
Definition: json.h:735
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData()=default
JSON_BASE_TYPES
#define JSON_BASE_TYPES
Definition: json.h:25
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData(const JsonObject &v)
Definition: json.h:112
conscience_utils::JSON::CscJsonObjectReader::yyjsonDoc
ptr< yyjson_doc > yyjsonDoc
Definition: json.h:476
conscience_utils::JSON::JsonObjectBox
Definition: json.h:35
conscience_utils::JSON::CscJsonObjectReader::parseFromString
static yyjson_doc * parseFromString(const string &jsonString)
Definition: json.cpp:95
conscience_utils::JSON::getNumericValueFromJson
T getNumericValueFromJson(yyjson_val *jsonValue)
Definition: json.h:185
conscience_utils::JSON::CscJsonObject::yyjsonMutableDoc
yyjson_mut_doc * yyjsonMutableDoc
Definition: json.h:693
conscience_utils::JSON::MissingRequiredPropertyException::MissingRequiredPropertyException
MissingRequiredPropertyException(const string message)
Definition: json.h:168
conscience_utils::JSON::CscJsonObjectReader::toString
string toString()
Definition: json.cpp:72
conscience_utils::JSON::jsonSerializableDataToString
string jsonSerializableDataToString(JsonSerializableData data)
Definition: json.cpp:812
conscience_utils::JSON::CscJsonObject::mergeKeyWith
void mergeKeyWith(CscJsonObject &other, const string &subPropertyPath, bool deep=true)
Definition: json.cpp:1005
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData(std::initializer_list< JsonSerializableData > init)
Definition: json.h:124
conscience_utils::JSON::CscJsonObjectReader::getRequiredUnsignedLongValue
unsigned long long getRequiredUnsignedLongValue(const string &key)
Definition: json.cpp:141
conscience_utils::JSON::CscJsonObjectReader::getArrayItemValue
optional< T > getArrayItemValue(const string &arrayKey, size_t index)
Definition: json.h:413
conscience_utils::JSON::JsonArrayBox::value
JsonArray value
Definition: json.h:32
nlohmann::detail::value_t::string
@ string
string value
conscience_utils::JSON::CscJsonObjectReader::createYyjsonSharedPtr
static ptr< yyjson_doc > createYyjsonSharedPtr(yyjson_doc *doc)
Definition: json.cpp:339
conscience_utils::JSON::CscJsonObjectReader::yyjsonValue
yyjson_val * yyjsonValue
Definition: json.h:478
conscience_utils::JSON::CscJsonObject::getMutableValueByPropertyPath
yyjson_mut_val * getMutableValueByPropertyPath(const string &propertyPath)
Definition: json.cpp:416
conscience_utils::JSON::CscJsonObject::yyjsonMutableValue
yyjson_mut_val * yyjsonMutableValue
Definition: json.h:698
conscience_utils::JSON::CscJsonObjectReader::toJsonString
string toJsonString(bool prettyPrint=false)
Definition: json.cpp:76
LOG_WARN
#define LOG_WARN(message)
Definition: conscience_log.h:194
conscience_utils::JSON::CscJsonObjectReader::isNumeric
bool isNumeric()
Definition: json.cpp:32
conscience_utils::JSON::CscJsonObject::getOrCreateObjectByPropertyPath
yyjson_mut_val * getOrCreateObjectByPropertyPath(const string &propertyPath)
Definition: json.cpp:679
conscience_utils::JSON::CscJsonObjectReader::hasKey
bool hasKey(const string &key)
Definition: json.cpp:183
conscience_utils::JSON::CscJsonObject::addLineBreakAndTabulation
static string addLineBreakAndTabulation(const string &json)
Adds line breaks and tabulations to a JSON string for improved readability.
Definition: json.cpp:738
conscience_utils::JSON::CscJsonParser::CscJsonParser
CscJsonParser(const fs::path &filePath)
Definition: json.cpp:481
conscience_utils::JSON::CscJsonObject::getObjectMutableSharedPtrValue
optional< ptr< CscJsonObject > > getObjectMutableSharedPtrValue(const string &key)
Definition: json.cpp:984
conscience_utils::JSON::vectorToJson
string vectorToJson(const vector< JsonSerializableData > &list, bool noNewLines)
Definition: json.cpp:874
conscience_utils::JSON
Definition: CscCommandMetadataBuilder.h:20
conscience_utils::JSON::CscJsonObjectReader::isArray
bool isArray()
Definition: json.cpp:12
conscience_utils::JSON::CscJsonObjectReader::countProperties
size_t countProperties()
Definition: json.cpp:124
conscience_utils::JSON::CscJsonObject::saveToFile
void saveToFile(const fs::path &outPath, bool lineBreakAndTabulation=false)
Definition: json.cpp:721
conscience_utils::JSON::CscJsonObject::CscJsonObject
CscJsonObject()
Definition: json.cpp:508
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData(const JsonArray &v)
Definition: json.h:103
conscience_utils::JSON::CscJsonObjectReader::isReal
bool isReal()
Definition: json.cpp:46
conscience_utils::JSON::CscJsonObjectReader::isNull
bool isNull()
Definition: json.cpp:51
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData(JsonArray &&v)
Definition: json.h:107
conscience_utils::JSON::pairsToJson
string pairsToJson(const vector< pair< string, JsonSerializableData >> &entries, bool noNewLines)
Definition: json.cpp:889
conscience_utils::JSON::CscJsonObjectReader::isString
bool isString()
Definition: json.cpp:22
conscience_utils::JSON::mapToJson
string mapToJson(const map< string, JsonSerializableData > &map, bool noNewLines)
Definition: json.cpp:793
conscience_utils::JSON::CscJsonObjectReader::getRequiredBooleanValue
bool getRequiredBooleanValue(const string &key)
Definition: json.cpp:213
conscience_utils::JSON::JsonSerializableData::requires
requires(std::is_enum_v< typing_helper::decay_t< E >> &&!typing_helper::excluded_v< E >) JsonSerializableData(E &&e)
Definition: json.h:83
conscience_core::bridging::commands::environment_objects::optional< double >
const ptr< CscObjectModel > const string const CscPoint3d const CscPoint3d optional< double >
Definition: environmentObjectsCommands.h:367
conscience_utils::JSON::CscJsonParser
Definition: json.h:534
conscience_utils::JSON::IsString
concept IsString
Definition: json.h:727
conscience_utils::JSON::MissingRequiredPropertyException::getMessage
const string getMessage() const
Definition: json.h:176
conscience_utils
Definition: CscEntityReflexion.h:50
conscience_utils::JSON::CscJsonObject::mergeWith
void mergeWith(CscJsonObject &other, bool deep=true)
Definition: json.cpp:996
conscience_utils::JSON::CscJsonObject::getYyjsonDoc
virtual ptr< yyjson_doc > getYyjsonDoc() override
Definition: json.cpp:523
conscience_utils::JSON::CscJsonObject::getYyjsonValue
virtual yyjson_val * getYyjsonValue() override
Definition: json.cpp:537
std
Definition: json.hpp:4598
conscience_utils::JSON::JsonArrayBox
Definition: json.h:31
conscience_utils::JSON::CscJsonObjectReader::getDoubleValue
optional< double > getDoubleValue(const string &key)
Definition: json.cpp:150
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectValueAsMap
map< string, optional< any > > getRequiredObjectValueAsMap(const string &key)
Definition: json.cpp:345
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData(JsonObject &&v)
Definition: json.h:116
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectPtrValue
CscJsonObjectReader * getRequiredObjectPtrValue(const string &key)
Definition: json.cpp:293
conscience_utils::JSON::CscJsonObjectReader::isBool
bool isBool()
Definition: json.cpp:27
conscience_utils::JSON::CscJsonObjectReader::getRequiredArrayItemValue
T getRequiredArrayItemValue(const string &arrayKey, size_t index)
Definition: json.h:432
conscience_utils::JSON::JsonSerializableData::requires
requires(!std::is_enum_v< typing_helper::decay_t< T >> &&typing_helper::is_scalar_json_v< T > &&!typing_helper::excluded_v< T >) JsonSerializableData(T &&v)
Definition: json.h:95
conscience_utils::JSON::JsonObjectBox::value
JsonObject value
Definition: json.h:36
conscience_utils::JSON::CscJsonObjectReader::toMutable
CscJsonObject * toMutable(yyjson_mut_doc *parentDoc=nullptr)
Definition: json.cpp:915
conscience_utils::JSON::CscJsonObjectReader::getYyjsonValue
virtual yyjson_val * getYyjsonValue()
Definition: json.cpp:59
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectValueAsMapOfStrings
map< string, string > getRequiredObjectValueAsMapOfStrings(const string &key)
Definition: json.cpp:376
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData(std::initializer_list< JsonObject::value_type > init)
Definition: json.h:120
conscience_utils::JSON::MissingRequiredPropertyException::what
const char * what() const
Definition: json.h:172
conscience_utils::JSON::CscJsonObject::setArray
CscJsonObject * setArray(const string &propertyPath, const vector< bool > &value)
Definition: json.cpp:615
conscience_utils::JSON::CscJsonObjectReader::getRequiredArrayValue
vector< T > getRequiredArrayValue(const string &key)
Definition: json.h:386
conscience_utils::JSON::CscJsonObjectReader::getRequiredFloatValue
float getRequiredFloatValue(const string &key)
Definition: json.cpp:179
conscience_utils::JSON::JsonArray
std::vector< JsonSerializableData > JsonArray
Definition: json.h:29
conscience_utils::JSON::CscJsonObjectReader::getValueByPropertyPath
yyjson_val * getValueByPropertyPath(const string &propertyPath)
Definition: json.cpp:411
ASSERT
#define ASSERT(MSG, EXPR)
Definition: conscience_util.h:43
conscience_utils::JSON::typing_helper::excluded_v
constexpr bool excluded_v
Definition: json.h:63
conscience_utils::JSON::CscJsonObjectReader
Definition: json.h:203
conscience_utils::JSON::CscJsonObject
Definition: json.h:544
conscience_utils::JSON::vectorToJsonSerializable
JsonSerializableData vectorToJsonSerializable(const vector< TValue > &theVector)
Definition: json.h:142
conscience_utils::JSON::CscJsonObjectReader::getUnsignedLongValue
optional< unsigned long long > getUnsignedLongValue(const string &key)
Definition: json.cpp:243
conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableValue
CscJsonObject getRequiredObjectMutableValue(const string &key)
Definition: json.cpp:934
conscience_utils::JSON::CscJsonObjectReader::getFloatValue
optional< float > getFloatValue(const string &key)
Definition: json.cpp:171
conscience_core::bridging::commands::environment_objects::optional< string >
const ptr< CscObjectModel > const string const CscPoint3d const CscPoint3d optional< string >
Definition: environmentObjectsCommands.h:373
conscience_utils::JSON::CscJsonObjectReader::getObjectPtrValue
optional< CscJsonObjectReader * > getObjectPtrValue(const string &key)
Definition: json.cpp:302
conscience_utils::JSON::CscJsonObjectReader::getRequiredIntValue
int getRequiredIntValue(const string &key)
Definition: json.cpp:132
conscience_utils::JSON::CscJsonObjectReader::asMap
map< string, optional< any > > asMap()
Definition: json.cpp:367
conscience_utils::JSON::CscJsonObjectReader::getObjectValueAsMap
optional< map< string, optional< any > > > getObjectValueAsMap(const string &key)
Definition: json.cpp:354
conscience_utils::JSON::typing_helper::is_scalar_json_v
constexpr bool is_scalar_json_v
Definition: json.h:49
ptr
std::shared_ptr< T > ptr
Definition: CscCommon.h:29
conscience_utils::JSON::CscJsonObjectReader::getRequiredStringValue
string getRequiredStringValue(const string &key, bool allowEmpty=false)
Definition: json.cpp:222
conscience_utils::JSON::CscJsonObject::mergeObjects
void mergeObjects(yyjson_mut_val *thisObj, yyjson_mut_val *otherObj, bool deep)
Definition: json.cpp:1017
conscience_utils::JSON::MissingRequiredPropertyException
Definition: json.h:166
conscience_utils::JSON::CscJsonObjectReader::isObject
bool isObject()
Definition: json.cpp:17
conscience_utils::JSON::CscJsonObjectReader::getStringValue
optional< string > getStringValue(const string &key)
Definition: json.cpp:255
conscience_utils::JSON::CscJsonObjectReader::getKeys
vector< string > getKeys()
Definition: json.cpp:111
conscience_utils::JSON::typing_helper::decay_t
std::decay_t< T > decay_t
Definition: json.h:46
conscience_utils::JSON::CscJsonObjectReader::getArrayLength
unsigned getArrayLength(const string &arrayKey)
Definition: json.cpp:193