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;
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 
210  bool hasKey(const string &key);
211 
215  bool isArray(const string &key);
216 
220  unsigned getArrayLength(const string &arrayKey);
221 
225  bool isNull(const string &key);
226 
230  string getRequiredStringValue(const string &key, bool allowEmpty = false);
231 
235  optional<int> getIntValue(const string &key);
236 
240  int getRequiredIntValue(const string &key);
241 
245  optional<unsigned long long> getUnsignedLongValue(const string &key);
246 
250  unsigned long long getRequiredUnsignedLongValue(const string &key);
251 
252  optional<bool> getBooleanValue(const string &key);
253 
254  bool getRequiredBooleanValue(const string &key);
255 
259  optional<double> getDoubleValue(const string &key);
260 
264  double getRequiredDoubleValue(const string &key);
265 
269  optional<float> getFloatValue(const string &key);
270 
274  float getRequiredFloatValue(const string &key);
275 
279  optional<string> getStringValue(const string &key);
280 
284  CscJsonObjectReader getRequiredObjectValue(const string &key);
285 
289  optional<CscJsonObjectReader> getObjectValue(const string &key);
290 
295 
299  optional<CscJsonObjectReader *> getObjectPtrValue(const string &key);
300 
305 
309  optional<ptr<CscJsonObjectReader>> getObjectSharedPtrValue(const string &key);
310 
314  map<string, optional<any>> getRequiredObjectValueAsMap(const string &key);
315 
320  map<string, string> getRequiredObjectValueAsMapOfStrings(const string &key);
321 
325  optional<map<string, optional<any>>> getObjectValueAsMap(const string &key);
326 
330  optional<any> getAnyValue(string key);
331 
335  template <class T>
336  vector<T> getRequiredArrayValue(const string &key) {
337  optional<vector<T>> value = getArrayValue<T>(key);
338  if (!value.has_value()) {
339  throw MissingRequiredPropertyException("required array property " + key + " cannot be found in " + toString());
340  }
341 
342  return value.value();
343  }
344 
348  template <class T>
349  optional<vector<T>> getArrayValue(const string &key) {
350  try {
351  yyjson_val *value = getValueByPropertyPath(key);
352  return toArrayValue<T>(value, getYyjsonDoc());
353  } catch (const std::invalid_argument e) {
354  LOG_WARN(key + " cannot be read as array: " + string(e.what()));
355  }
356  return {};
357  }
358 
362  template <class T>
363  optional<T> getArrayItemValue(const string &arrayKey, size_t index) {
364  optional<T> result = {};
365  try {
366  yyjson_val *value = getValueByPropertyPath(arrayKey);
367  if (value != nullptr) {
368  yyjson_val *itemValue = yyjson_arr_get(value, index);
369  if (itemValue != nullptr) {
370  result = toValue<T>(itemValue, getYyjsonDoc());
371  }
372  }
373  } catch (const std::invalid_argument e) {
374  LOG_WARN("cannot getArrayItemValue " + arrayKey + " : " + string(e.what()));
375  }
376  return result;
377  }
381  template <class T>
382  T getRequiredArrayItemValue(const string &arrayKey, size_t index) {
383  optional<T> value = getArrayItemValue<T>(arrayKey, index);
384  if (!value.has_value()) {
385  throw MissingRequiredPropertyException("required array item: " + arrayKey + "[" + to_string(index) + "] cannot be found in " + toString());
386  }
387 
388  return value.value();
389  }
390 
394  template <class T>
395  vector<T> asArrayValue() {
396  return toArrayValue<T>(getYyjsonValue(), getYyjsonDoc()).value();
397  }
398 
403  CscJsonObjectReader(ptr<yyjson_doc> yyjsonDoc, yyjson_val *yyjsonValue = nullptr, bool doNotReleaseYyjsonObjects = false);
404  CscJsonObjectReader(const fs::path filePath);
405  CscJsonObjectReader(const string &jsonContent);
406 
407  virtual ~CscJsonObjectReader();
408 
409  string toString();
410 
411  string toJsonString(bool prettyPrint = false);
412 
416  CscJsonObject *toMutable(yyjson_mut_doc *parentDoc = nullptr);
417  static vector<CscJsonObject *> toMutableList(const vector<ptr<CscJsonObjectReader>> values);
418 
422  yyjson_val *getLowLevelParser();
423 
424 protected:
425  static ptr<yyjson_doc> createYyjsonSharedPtr(yyjson_doc *doc);
427 
428  yyjson_val *yyjsonValue = nullptr;
429  virtual ptr<yyjson_doc> getYyjsonDoc();
430  virtual yyjson_val *getYyjsonValue();
431 
432  yyjson_val *getValueByPropertyPath(const string &propertyPath);
433  static yyjson_doc *parseFromFile(const fs::path filePath);
434  static yyjson_doc *parseFromString(const string &jsonString);
435 
436 private:
437  const bool doNotReleaseYyjsonObjects;
438 
439  optional<any> jsonValueToAny(yyjson_val *jsonValue);
440 
441  template <class T>
442  static inline T toValue(yyjson_val *yyjsonValue, ptr<yyjson_doc> jsonDoc) {
443  if constexpr (std::is_arithmetic<T>::value) {
444  return getNumericValueFromJson<T>(yyjsonValue);
445  }
446  if constexpr (std::is_same<T, string>::value) {
447  const char *valueChars = yyjson_get_str(yyjsonValue);
448  return string(valueChars);
449  }
450  if constexpr (std::is_same_v<T, const CscJsonObjectReader *> || std::is_same_v<T, CscJsonObjectReader *>) {
451  return new CscJsonObjectReader(jsonDoc, yyjsonValue, true);
452  }
453  if constexpr (std::is_base_of<ptr<CscJsonObjectReader>, T>::value) {
454  return std::make_shared<CscJsonObjectReader>(jsonDoc, yyjsonValue, true);
455  }
456  throw new std::invalid_argument(
457  "unsupported data type " + string(typeid(T).name()) + " for array " +
458  string(yyjsonValue != nullptr ? string(yyjson_get_raw(yyjsonValue)) : "?"));
459  }
460 
461  template <class T>
462  static optional<vector<T>> toArrayValue(yyjson_val *arrayJsonValue, ptr<yyjson_doc> jsonDoc) {
463  try {
464  if (arrayJsonValue != nullptr && yyjson_get_type(arrayJsonValue) == YYJSON_TYPE_ARR) {
465  vector<T> items;
466 
467  string ss = string(yyjson_val_write(arrayJsonValue, YYJSON_WRITE_NOFLAG, NULL));
468 
469  yyjson_val *arrayItem;
470  yyjson_arr_iter iter = yyjson_arr_iter_with(arrayJsonValue);
471  while ((arrayItem = yyjson_arr_iter_next(&iter))) {
472  items.push_back(toValue<T>(arrayItem, jsonDoc));
473  }
474 
475  return items;
476  }
477  } catch (const std::invalid_argument e) {
478  LOG_WARN(string(arrayJsonValue != nullptr ? string(yyjson_get_raw(arrayJsonValue)) : "?") + " cannot be read as array: " + string(e.what()));
479  }
480  return {};
481  }
482 };
483 
485 public:
486  CscJsonParser(const fs::path &filePath);
487  CscJsonParser(const string &jsonContent);
488 };
489 
495 public:
496  CscJsonObject(const fs::path &jsonFilePath);
497  CscJsonObject(const string &jsonString);
498 
502  CscJsonObject(const map<string, string> &map);
503 
507  CscJsonObject(yyjson_mut_doc *existingDoc, yyjson_mut_val *existingValue, bool doNotReleaseYyjsonObjects = false, CscJsonObject *lookupParent = nullptr);
508 
512  CscJsonObject();
513  ~CscJsonObject();
514 
518  CscJsonObject getRequiredObjectMutableValue(const string &key);
519 
523  optional<CscJsonObject> getObjectMutableValue(const string &key);
524 
529 
533  optional<CscJsonObject *> getObjectMutablePtrValue(const string &key);
534 
539 
543  optional<ptr<CscJsonObject>> getObjectMutableSharedPtrValue(const string &key);
544 
545  CscJsonObject *set(const string &propertyPath, bool value);
546  CscJsonObject *set(const string &propertyPath, const char *value);
547  CscJsonObject *set(const string &propertyPath, const string &value);
548  CscJsonObject *set(const string &propertyPath, double value);
549  CscJsonObject *set(const string &propertyPath, int value);
550  CscJsonObject *set(const string &propertyPath, long long value);
551  CscJsonObject *set(const string &propertyPath, unsigned long long value);
552  CscJsonObject *set(const string &propertyPath, CscJsonObject &object);
556  CscJsonObject *set(const string &propertyPath, CscJsonObject *object);
557  CscJsonObject *set(const string &propertyPath, ptr<CscJsonObject> object);
558  CscJsonObject *set(const string &propertyPath, const map<string, string> &value);
559 
560  CscJsonObject *setArray(const string &propertyPath, const vector<bool> &value);
561  CscJsonObject *setArray(const string &propertyPath, const vector<string> &value);
562  CscJsonObject *setArray(const string &propertyPath, const vector<float> &value);
563  CscJsonObject *setArray(const string &propertyPath, const vector<double> &value);
564  CscJsonObject *setArray(const string &propertyPath, const vector<int> &value);
565  CscJsonObject *setArray(const string &propertyPath, const vector<long long> &value);
566  CscJsonObject *setArray(const string &propertyPath, const vector<unsigned long long> &value);
570  CscJsonObject *setArray(const string &propertyPath, const vector<CscJsonObject *> &objects);
571  CscJsonObject *setArray(const string &propertyPath, const vector<CscJsonObject> &objects);
572  CscJsonObject *setArray(const string &propertyPath, const vector<ptr<CscJsonObject>> &objects);
573  CscJsonObject *setArray(const string &propertyPath, const vector<map<string, string>> &value);
574 
578  CscJsonObject *remove(const string &propertyPath);
579 
580  void saveToFile(const fs::path &outPath, bool lineBreakAndTabulation = false);
581 
615  static string addLineBreakAndTabulation(const string &json);
616 
617  yyjson_mut_val *getLowLevelMutableObject();
618 
623  void mergeWith(CscJsonObject &other, bool deep = true);
624 
625  void mergeKeyWith(CscJsonObject &other, const string &subPropertyPath, bool deep = true);
626 
627 private:
628  bool doNotReleaseYyjsonObjects = false;
629  void onModified();
633  CscJsonObject *lookupParent = nullptr;
634 
635 protected:
643  yyjson_mut_doc *yyjsonMutableDoc = nullptr;
644 
648  yyjson_mut_val *yyjsonMutableValue = nullptr;
649 
653  virtual yyjson_val *getYyjsonValue() override;
657  virtual ptr<yyjson_doc> getYyjsonDoc() override;
661  yyjson_mut_doc *getYyjsonMutableDoc();
665  yyjson_mut_val *getYyjsonMutableValue();
666 
667  yyjson_mut_val *getMutableValueByPropertyPath(const string &propertyPath);
668 
669  yyjson_mut_val *getOrCreateObjectByPropertyPath(const string &propertyPath);
670 
671  CscJsonObject *set(const string &propertyPath, yyjson_mut_val *newValue);
672 
673  void mergeObjects(yyjson_mut_val *thisObj, yyjson_mut_val *otherObj, bool deep);
674 };
675 
676 template <class T>
677 concept IsString = std::is_same_v<T, string>;
678 template <IsString T>
679 yyjson_mut_val *toJsonValue(const T &value, yyjson_mut_doc *doc) {
680  char *stringCopy = strdup(value.c_str());
681  return yyjson_mut_str(doc, stringCopy);
682 }
683 
684 template <class T>
685 concept IsNotString = !IsString<T>;
686 template <IsNotString T>
687 yyjson_mut_val *toJsonValue(const T &value, yyjson_mut_doc *doc);
688 
689 inline yyjson_mut_val *mapToJsonObject(const map<string, string> &map, yyjson_mut_doc *doc) {
690  yyjson_mut_val *mapJsonValue = yyjson_mut_obj(doc);
691 
692  for (auto [key, value] : map) {
693  yyjson_mut_val *keyJsonValue = toJsonValue(key, doc);
694  yyjson_mut_val *valueJsonValue = toJsonValue(value, doc);
695  yyjson_mut_obj_put(mapJsonValue, keyJsonValue, valueJsonValue);
696  }
697 
698  return mapJsonValue;
699 }
700 
701 template <IsNotString T>
702 yyjson_mut_val *toJsonValue(T &value, yyjson_mut_doc *doc) {
703 
704  if constexpr (std::is_arithmetic<T>::value) {
705  if constexpr (std::is_unsigned<T>::value) {
706  return yyjson_mut_uint(doc, value);
707  } else if constexpr (std::is_floating_point<T>::value) {
708  return yyjson_mut_real(doc, value);
709  } else {
710  return yyjson_mut_sint(doc, value);
711  }
712 
713  } else if constexpr (std::is_same_v<T, bool>) {
714  return yyjson_mut_bool(doc, value);
715 
716  } else if constexpr (std::is_same_v<ptr<CscJsonObject>, T> || std::is_same_v<ptr<const CscJsonObject>, T>) {
717  return yyjson_mut_val_mut_copy(doc, value->getLowLevelMutableObject());
718 
719  } else if constexpr (std::is_same_v<CscJsonObject, T>) {
720  return yyjson_mut_val_mut_copy(doc, value.getLowLevelMutableObject());
721 
722  } else if constexpr (std::is_same_v<CscJsonObject *, T> || std::is_same_v<const CscJsonObject *, T>) {
723  return yyjson_mut_val_mut_copy(doc, value->getLowLevelMutableObject());
724 
725  } else if constexpr (std::is_same_v<map<string, string>, T>) {
726  return mapToJsonObject(value, doc);
727  } else {
728  throw runtime_error("unsupported type ");
729  }
730 }
731 
732 template <class T>
733 yyjson_mut_val *toJsonValue(const vector<T> &valueList, yyjson_mut_doc *doc, bool clone = false) {
734  yyjson_mut_val *arrayValue = yyjson_mut_arr(doc);
735  for (T value : valueList) {
736  yyjson_mut_val *jsonValue = toJsonValue<T>(value, doc);
737  yyjson_mut_val *newValue = jsonValue;
738  if (clone) {
739  newValue = yyjson_mut_val_mut_copy(doc, jsonValue);
740  }
741  bool success = yyjson_mut_arr_append(arrayValue, newValue);
742  if (!success) {
743  throw runtime_error("cannot add item to array");
744  }
745  }
746  return arrayValue;
747 }
748 
749 }
750 
751 #endif
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:689
conscience_log.h
conscience_utils::JSON::toJsonValue
yyjson_mut_val * toJsonValue(const T &value, yyjson_mut_doc *doc)
Definition: json.h:679
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:232
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::~CscJsonObjectReader
virtual ~CscJsonObjectReader()
Definition: json.cpp:27
conscience_utils::JSON::CscJsonObjectReader::getBooleanValue
optional< bool > getBooleanValue(const string &key)
Definition: json.cpp:152
conscience_utils::JSON::CscJsonObject::getObjectMutableValue
optional< CscJsonObject > getObjectMutableValue(const string &key)
Definition: json.cpp:884
conscience_utils::JSON::CscJsonObjectReader::getAnyValue
optional< any > getAnyValue(string key)
Definition: json.cpp:334
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:265
conscience_utils::JSON::CscJsonObjectReader::asArrayValue
vector< T > asArrayValue()
Definition: json.h:395
conscience_utils::JSON::CscJsonObject::remove
CscJsonObject * remove(const string &propertyPath)
Definition: json.cpp:651
conscience_utils::JSON::CscJsonObject::getLowLevelMutableObject
yyjson_mut_val * getLowLevelMutableObject()
Definition: json.cpp:513
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectValue
CscJsonObjectReader getRequiredObjectValue(const string &key)
Definition: json.cpp:223
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:904
conscience_utils::JSON::CscJsonObject::set
CscJsonObject * set(const string &propertyPath, bool value)
Definition: json.cpp:517
conscience_utils::JSON::CscJsonObjectReader::getYyjsonDoc
virtual ptr< yyjson_doc > getYyjsonDoc()
Definition: json.cpp:13
conscience_utils::JSON::CscJsonObjectReader::getIntValue
optional< int > getIntValue(const string &key)
Definition: json.cpp:182
conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableSharedPtrValue
ptr< CscJsonObject > getRequiredObjectMutableSharedPtrValue(const string &key)
Definition: json.cpp:916
conscience_utils::JSON::CscJsonObjectReader::parseFromFile
static yyjson_doc * parseFromFile(const fs::path filePath)
Definition: json.cpp:43
conscience_utils::JSON::CscJsonObjectReader::getRequiredDoubleValue
double getRequiredDoubleValue(const string &key)
Definition: json.cpp:113
conscience_utils::JSON::JsonObject
std::map< std::string, JsonSerializableData > JsonObject
Definition: json.h:30
conscience_utils::JSON::CscJsonObject::~CscJsonObject
~CscJsonObject()
Definition: json.cpp:452
conscience_utils::JSON::CscJsonObjectReader::getObjectSharedPtrValue
optional< ptr< CscJsonObjectReader > > getObjectSharedPtrValue(const string &key)
Definition: json.cpp:274
conscience_utils::JSON::CscJsonObject::getRequiredObjectMutablePtrValue
CscJsonObject * getRequiredObjectMutablePtrValue(const string &key)
Definition: json.cpp:895
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:286
conscience_utils::JSON::CscJsonObjectReader::getArrayValue
optional< vector< T > > getArrayValue(const string &key)
Definition: json.h:349
conscience_utils::JSON::CscJsonObject::getYyjsonMutableDoc
yyjson_mut_doc * getYyjsonMutableDoc()
Definition: json.cpp:488
conscience_utils::JSON::CscJsonObject::getYyjsonMutableValue
yyjson_mut_val * getYyjsonMutableValue()
Definition: json.cpp:501
conscience_utils::JSON::CscJsonObjectReader::toMutableList
static vector< CscJsonObject * > toMutableList(const vector< ptr< CscJsonObjectReader >> values)
Definition: json.cpp:867
conscience_utils::JSON::IsNotString
concept IsNotString
Definition: json.h:685
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:426
conscience_utils::JSON::JsonObjectBox
Definition: json.h:35
conscience_utils::JSON::CscJsonObjectReader::parseFromString
static yyjson_doc * parseFromString(const string &jsonString)
Definition: json.cpp:53
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:643
conscience_utils::JSON::MissingRequiredPropertyException::MissingRequiredPropertyException
MissingRequiredPropertyException(const string message)
Definition: json.h:168
conscience_utils::JSON::CscJsonObjectReader::toString
string toString()
Definition: json.cpp:30
conscience_utils::JSON::jsonSerializableDataToString
string jsonSerializableDataToString(JsonSerializableData data)
Definition: json.cpp:753
conscience_utils::JSON::CscJsonObject::mergeKeyWith
void mergeKeyWith(CscJsonObject &other, const string &subPropertyPath, bool deep=true)
Definition: json.cpp:946
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:92
conscience_utils::JSON::CscJsonObjectReader::getArrayItemValue
optional< T > getArrayItemValue(const string &arrayKey, size_t index)
Definition: json.h:363
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:290
conscience_utils::JSON::CscJsonObjectReader::yyjsonValue
yyjson_val * yyjsonValue
Definition: json.h:428
conscience_utils::JSON::CscJsonObject::getMutableValueByPropertyPath
yyjson_mut_val * getMutableValueByPropertyPath(const string &propertyPath)
Definition: json.cpp:357
conscience_utils::JSON::CscJsonObject::yyjsonMutableValue
yyjson_mut_val * yyjsonMutableValue
Definition: json.h:648
conscience_utils::JSON::CscJsonObjectReader::toJsonString
string toJsonString(bool prettyPrint=false)
Definition: json.cpp:34
LOG_WARN
#define LOG_WARN(message)
Definition: conscience_log.h:193
conscience_utils::JSON::CscJsonObject::getOrCreateObjectByPropertyPath
yyjson_mut_val * getOrCreateObjectByPropertyPath(const string &propertyPath)
Definition: json.cpp:620
conscience_utils::JSON::CscJsonObjectReader::hasKey
bool hasKey(const string &key)
Definition: json.cpp:134
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:679
conscience_utils::JSON::CscJsonParser::CscJsonParser
CscJsonParser(const fs::path &filePath)
Definition: json.cpp:422
conscience_utils::JSON::CscJsonObject::getObjectMutableSharedPtrValue
optional< ptr< CscJsonObject > > getObjectMutableSharedPtrValue(const string &key)
Definition: json.cpp:925
conscience_utils::JSON::vectorToJson
string vectorToJson(const vector< JsonSerializableData > &list, bool noNewLines)
Definition: json.cpp:815
conscience_utils::JSON
Definition: CscCommandMetadataBuilder.h:20
conscience_utils::JSON::CscJsonObject::saveToFile
void saveToFile(const fs::path &outPath, bool lineBreakAndTabulation=false)
Definition: json.cpp:662
conscience_utils::JSON::CscJsonObject::CscJsonObject
CscJsonObject()
Definition: json.cpp:449
conscience_utils::JSON::JsonSerializableData::JsonSerializableData
JsonSerializableData(const JsonArray &v)
Definition: json.h:103
jwt::alphabet::index
uint32_t index(const std::array< char, 64 > &alphabet, char symbol)
Definition: base.h:91
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:830
conscience_utils::JSON::mapToJson
string mapToJson(const map< string, JsonSerializableData > &map, bool noNewLines)
Definition: json.cpp:734
conscience_utils::JSON::CscJsonObjectReader::getRequiredBooleanValue
bool getRequiredBooleanValue(const string &key)
Definition: json.cpp:164
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:484
conscience_utils::JSON::IsString
concept IsString
Definition: json.h:677
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:937
conscience_utils::JSON::CscJsonObject::getYyjsonDoc
virtual ptr< yyjson_doc > getYyjsonDoc() override
Definition: json.cpp:464
conscience_utils::JSON::CscJsonObject::getYyjsonValue
virtual yyjson_val * getYyjsonValue() override
Definition: json.cpp:478
conscience_utils::JSON::CscJsonObjectReader::isNull
bool isNull(const string &key)
Definition: json.cpp:64
conscience_utils::JSON::CscJsonObjectReader::isArray
bool isArray(const string &key)
Definition: json.cpp:139
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:101
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectValueAsMap
map< string, optional< any > > getRequiredObjectValueAsMap(const string &key)
Definition: json.cpp:296
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:244
conscience_utils::JSON::CscJsonObjectReader::getRequiredArrayItemValue
T getRequiredArrayItemValue(const string &arrayKey, size_t index)
Definition: json.h:382
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:856
conscience_utils::JSON::CscJsonObjectReader::getYyjsonValue
virtual yyjson_val * getYyjsonValue()
Definition: json.cpp:17
conscience_utils::JSON::CscJsonObjectReader::getRequiredObjectValueAsMapOfStrings
map< string, string > getRequiredObjectValueAsMapOfStrings(const string &key)
Definition: json.cpp:325
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:556
conscience_utils::JSON::CscJsonObjectReader::getRequiredArrayValue
vector< T > getRequiredArrayValue(const string &key)
Definition: json.h:336
conscience_utils::JSON::CscJsonObjectReader::getRequiredFloatValue
float getRequiredFloatValue(const string &key)
Definition: json.cpp:130
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:352
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:494
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:194
conscience_utils::JSON::CscJsonObject::getRequiredObjectMutableValue
CscJsonObject getRequiredObjectMutableValue(const string &key)
Definition: json.cpp:875
conscience_utils::JSON::CscJsonObjectReader::getFloatValue
optional< float > getFloatValue(const string &key)
Definition: json.cpp:122
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:253
conscience_utils::JSON::CscJsonObjectReader::getRequiredIntValue
int getRequiredIntValue(const string &key)
Definition: json.cpp:83
conscience_utils::JSON::CscJsonObjectReader::getObjectValueAsMap
optional< map< string, optional< any > > > getObjectValueAsMap(const string &key)
Definition: json.cpp:305
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:173
conscience_utils::JSON::CscJsonObject::mergeObjects
void mergeObjects(yyjson_mut_val *thisObj, yyjson_mut_val *otherObj, bool deep)
Definition: json.cpp:958
conscience_utils::JSON::MissingRequiredPropertyException
Definition: json.h:166
conscience_utils::JSON::CscJsonObjectReader::getStringValue
optional< string > getStringValue(const string &key)
Definition: json.cpp:206
conscience_utils::JSON::CscJsonObjectReader::getKeys
vector< string > getKeys()
Definition: json.cpp:69
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:144