GoPxL SDK
GoJson.h
Go to the documentation of this file.
1 
9 #ifndef GO_PXL_SDK_JSON_H
10 #define GO_PXL_SDK_JSON_H
11 
12 #include <GoPxLSdk/GoJsonPointer.h>
14 
19 #define GoJsonTypeSpec(type) \
20  template GoPxLSdkClass type GoJson::Get<type>(); \
21  template GoPxLSdkClass const type GoJson::Get<type>() const; \
22  template GoPxLSdkClass type GoJson::Get<type>(const std::string&); \
23  template GoPxLSdkClass const type GoJson::Get<type>(const std::string&) const; \
24  template GoPxLSdkClass type GoJson::Get<type>(const GoJsonPointer&); \
25  template GoPxLSdkClass const type GoJson::Get<type>(const GoJsonPointer&) const; \
26  template GoPxLSdkClass bool operator==<type>(const GoJson&, const type&); \
27  template GoPxLSdkClass void GoJson::operator=<type>(const type&); \
28  template GoPxLSdkClass void GoJson::Set<type>(const std::string&, const type&); \
29  template GoPxLSdkClass void GoJson::Set<type>(const GoJsonPointer&, const type&); \
30  template GoPxLSdkClass void GoJson::Add<type>(const std::string&, const type&); \
31  template GoPxLSdkClass void GoJson::Replace<type>(const std::string&, const type&);
32 
33 namespace GoPxLSdk
34 {
39 {
40 public:
47  GoJson();
48 
56  explicit GoJson(std::shared_ptr<nlohmann::json> json);
57 
65  GoJson(const GoJson& json);
66 
75  explicit GoJson(const std::string& data);
76 
83  ~GoJson();
84 
92  GoJsonIterator Begin();
93 
101  const GoJsonIterator Begin() const;
102 
110  const GoJsonIterator End() const;
111 
122  template<typename T>
123  T Get(const std::string& path);
124 
133  template<typename T>
134  T Get();
135 
146  template<typename T>
147  const T Get(const std::string& path) const;
148 
159  template<typename T>
160  T Get(const GoJsonPointer& pointer);
161 
172  template<typename T>
173  const T Get(const GoJsonPointer& pointer) const;
174 
183  template<typename T>
184  const T Get() const;
185 
194  ByteArray GetBinary();
195 
204  template <typename T>
205  void Set(const std::string& path, const T& val);
206 
215  template <std::size_t N>
216  void Set(const std::string& path, const char(&val)[N])
217  {
218  Set(path, std::string(val));
219  }
220 
229  template <typename T>
230  void Set(const GoJsonPointer& ptr, const T& val);
231 
240  template <std::size_t N>
241  void Set(const GoJsonPointer& ptr, const char(&val)[N])
242  {
243  Set(ptr, std::string(val));
244  }
245 
254  void Set(const std::string& path, const GoJson& val);
255 
264  void Set(const GoJsonPointer& ptr, const GoJson& val);
265 
274  void Set(const std::string& path, const std::vector<k8u>& val);
275 
284  void Set(const GoJsonPointer& ptr, const std::vector<k8u>& val);
285 
296  template<typename T>
297  void Add(const std::string& path, const T& val);
298 
310  template <std::size_t N>
311  void Add(const std::string& path, const char(&val)[N])
312  {
313  Add(path, std::string(val));
314  }
315 
325  template<typename T>
326  void Replace(const std::string& path, const T& val);
327 
337  template <std::size_t N>
338  void Replace(const std::string& path, const char(&val)[N])
339  {
340  Replace(path, std::string(val));
341  }
342 
351  bool HasKey(const std::string& key) const;
352 
362  void Copy(const std::string& source, const std::string& destination);
363 
373  void Move(const std::string& source, const std::string& destination);
374 
383  void Remove(const std::string& path);
384 
393  void Patch(const GoJson& obj);
394 
403  void Merge(const GoJson& obj);
404 
412  const bool Empty() const;
413 
423  GoJsonIterator Find(const std::string& key);
424 
435  GoJsonIterator Find(const GoJsonPointer& pointer);
436 
446  const GoJsonIterator Find(const std::string& key) const;
447 
458  const GoJsonIterator Find(const GoJsonPointer& pointer) const;
459 
469  GoJson At(int index);
470 
480  const GoJson At(int index) const;
481 
491  GoJson At(const std::string& path);
492 
502  const GoJson At(const std::string& path) const;
503 
513  GoJson At(const GoJsonPointer& pointer);
514 
524  const GoJson At(const GoJsonPointer& pointer) const;
525 
535  const bool Contains(const GoJsonPointer& pointer) const;
536 
544  const size_t Size() const;
545 
553  const bool IsObject() const;
554 
562  const bool IsNumber() const;
563 
571  const bool IsFloat() const;
572 
580  const bool IsInteger() const;
581 
589  const bool IsUnsigned() const;
590 
598  const bool IsBoolean() const;
599 
607  const bool IsString() const;
608 
616  const bool IsArray() const;
617 
625  const bool IsPrimitive() const;
626 
634  const bool IsBinary() const;
635 
643  const bool IsDiscarded() const;
644 
652  const bool IsNull() const;
653 
661  const bool IsStructured() const;
662 
671  const bool IsObject(const std::string& path) const;
672 
681  const bool IsNumber(const std::string& path) const;
682 
691  const bool IsFloat(const std::string& path) const;
692 
701  const bool IsInteger(const std::string& path) const;
702 
711  const bool IsUnsigned(const std::string& path) const;
712 
721  const bool IsBoolean(const std::string& path) const;
722 
731  const bool IsString(const std::string& path) const;
732 
741  const bool IsArray(const std::string& path) const;
742 
751  const bool IsPrimitive(const std::string& path) const;
752 
761  const bool IsBinary(const std::string& path) const;
762 
771  const bool IsDiscarded(const std::string& path) const;
772 
781  const bool IsNull(const std::string& path) const;
782 
791  const bool IsStructured(const std::string& path) const;
792 
830  void Flatten();
831 
870  void Unflatten();
871 
879  const std::string ToString();
880 
888  const std::string ToString() const;
889 
897  const ByteArray ToBinary();
898 
907  static GoJson ParseString(const std::string& data);
908 
917  static GoJson ParseMsgPack(const ByteArray& data);
918 
919  template<typename T>
920  friend bool operator==(const GoJson& left, const T& right);
921 
922  template <std::size_t N>
923  friend bool operator==(const GoJson& left, const char(&val)[N])
924  {
925  return left == std::string(val);
926  }
927 
928  friend std::ostream& operator<<(std::ostream& os, const GoJson& json)
929  {
930  os << json.ToString();
931 
932  return os;
933  }
934 
935  template<typename T>
936  void operator=(const T& left);
937 
938  template <std::size_t N>
939  void operator=(const char(&val)[N])
940  {
941  return *this = std::string(val);
942  }
943 
944 protected:
950  const std::shared_ptr<nlohmann::json> GetHandle(const std::string& path) const;
951 
956  const std::shared_ptr<nlohmann::json>& GetHandle() const;
957 
963  std::shared_ptr<nlohmann::json> GetHandle(const std::string& path);
964 
969  std::shared_ptr<nlohmann::json>& GetHandle();
970 
971  friend class GoRequest;
972  friend class GoJsonIterator;
973 
974 private:
975  mutable std::shared_ptr<nlohmann::json> jsonHandle;
976 };
977 
978 }
979 
980 #endif
Simplify working with json data.
Definition: GoJson.h:38
void operator=(const char(&val)[N])
Definition: GoJson.h:939
void Add(const std::string &path, const char(&val)[N])
Adds value if not exists.
Definition: GoJson.h:311
#define GoPxLSdkClass
Definition: Def.h:35
friend std::ostream & operator<<(std::ostream &os, const GoJson &json)
Definition: GoJson.h:928
Represents json iterator.
Definition: GoJsonIterator.h:24
const std::string ToString()
Gets string formatted JSON.
void Set(const std::string &path, const char(&val)[N])
Sets value.
Definition: GoJson.h:216
Definition: GoRequest.h:21
void Set(const GoJsonPointer &ptr, const char(&val)[N])
Sets value.
Definition: GoJson.h:241
void Replace(const std::string &path, const char(&val)[N])
Adds value if not exists.
Definition: GoJson.h:338
Definition: GoGdpMesh.h:44
std::vector< Byte > ByteArray
Definition: Def.h:49
basic_json< std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, adl_serializer, std::vector< std::uint8_t >> json
Definition: Def.h:88
Definition: Def.h:46
friend bool operator==(const GoJson &left, const char(&val)[N])
Definition: GoJson.h:923
Represents json pointer as described in Section 3 of RFC6901.
Definition: GoJsonPointer.h:20