GoPxL SDK
GoJson.h
Go to the documentation of this file.
1 
10 #ifndef GO_PXL_SDK_JSON_H
11 #define GO_PXL_SDK_JSON_H
12 
13 #include <GoPxLSdk/GoJsonPointer.h>
15 
20 #define GoJsonTypeSpec(type) \
21  template GoPxLSdkClass type GoJson::Get<type>(); \
22  template GoPxLSdkClass const type GoJson::Get<type>() const; \
23  template GoPxLSdkClass type GoJson::Get<type>(const std::string&); \
24  template GoPxLSdkClass const type GoJson::Get<type>(const std::string&) const; \
25  template GoPxLSdkClass type GoJson::Get<type>(const GoJsonPointer&); \
26  template GoPxLSdkClass const type GoJson::Get<type>(const GoJsonPointer&) const; \
27  template GoPxLSdkClass bool operator==<type>(const GoJson&, const type&); \
28  template GoPxLSdkClass void GoJson::operator=<type>(const type&); \
29  template GoPxLSdkClass void GoJson::Set<type>(const std::string&, const type&); \
30  template GoPxLSdkClass void GoJson::Set<type>(const GoJsonPointer&, const type&); \
31  template GoPxLSdkClass void GoJson::Add<type>(const std::string&, const type&); \
32  template GoPxLSdkClass void GoJson::Replace<type>(const std::string&, const type&);
33 
34 namespace GoPxLSdk
35 {
40 {
41 public:
48  GoJson();
49 
57  explicit GoJson(std::shared_ptr<nlohmann::json> json);
58 
66  GoJson(const GoJson& json);
67 
76  explicit GoJson(const std::string& data);
77 
84  ~GoJson();
85 
93  GoJsonIterator Begin();
94 
102  const GoJsonIterator Begin() const;
103 
111  const GoJsonIterator End() const;
112 
123  template<typename T>
124  T Get(const std::string& path);
125 
134  template<typename T>
135  T Get();
136 
147  template<typename T>
148  const T Get(const std::string& path) const;
149 
160  template<typename T>
161  T Get(const GoJsonPointer& pointer);
162 
173  template<typename T>
174  const T Get(const GoJsonPointer& pointer) const;
175 
184  template<typename T>
185  const T Get() const;
186 
195  ByteArray GetBinary();
196 
205  template <typename T>
206  void Set(const std::string& path, const T& val);
207 
216  template <std::size_t N>
217  void Set(const std::string& path, const char(&val)[N])
218  {
219  Set(path, std::string(val));
220  }
221 
230  template <typename T>
231  void Set(const GoJsonPointer& ptr, const T& val);
232 
241  template <std::size_t N>
242  void Set(const GoJsonPointer& ptr, const char(&val)[N])
243  {
244  Set(ptr, std::string(val));
245  }
246 
255  void Set(const std::string& path, const GoJson& val);
256 
265  void Set(const GoJsonPointer& ptr, const GoJson& val);
266 
275  void Set(const std::string& path, const std::vector<k8u>& val);
276 
285  void Set(const GoJsonPointer& ptr, const std::vector<k8u>& val);
286 
297  template<typename T>
298  void Add(const std::string& path, const T& val);
299 
311  template <std::size_t N>
312  void Add(const std::string& path, const char(&val)[N])
313  {
314  Add(path, std::string(val));
315  }
316 
326  template<typename T>
327  void Replace(const std::string& path, const T& val);
328 
338  template <std::size_t N>
339  void Replace(const std::string& path, const char(&val)[N])
340  {
341  Replace(path, std::string(val));
342  }
343 
352  bool HasKey(const std::string& key) const;
353 
363  void Copy(const std::string& source, const std::string& destination);
364 
374  void Move(const std::string& source, const std::string& destination);
375 
384  void Remove(const std::string& path);
385 
394  void Patch(const GoJson& obj);
395 
404  void Merge(const GoJson& obj);
405 
413  const bool Empty() const;
414 
424  GoJsonIterator Find(const std::string& key);
425 
436  GoJsonIterator Find(const GoJsonPointer& pointer);
437 
447  const GoJsonIterator Find(const std::string& key) const;
448 
459  const GoJsonIterator Find(const GoJsonPointer& pointer) const;
460 
470  GoJson At(int index);
471 
481  const GoJson At(int index) const;
482 
492  GoJson At(const std::string& path);
493 
503  const GoJson At(const std::string& path) const;
504 
514  GoJson At(const GoJsonPointer& pointer);
515 
525  const GoJson At(const GoJsonPointer& pointer) const;
526 
536  const bool Contains(const GoJsonPointer& pointer) const;
537 
545  const size_t Size() const;
546 
554  const bool IsObject() const;
555 
563  const bool IsNumber() const;
564 
572  const bool IsFloat() const;
573 
581  const bool IsInteger() const;
582 
590  const bool IsUnsigned() const;
591 
599  const bool IsBoolean() const;
600 
608  const bool IsString() const;
609 
617  const bool IsArray() const;
618 
626  const bool IsPrimitive() const;
627 
635  const bool IsBinary() const;
636 
644  const bool IsDiscarded() const;
645 
653  const bool IsNull() const;
654 
662  const bool IsStructured() const;
663 
672  const bool IsObject(const std::string& path) const;
673 
682  const bool IsNumber(const std::string& path) const;
683 
692  const bool IsFloat(const std::string& path) const;
693 
702  const bool IsInteger(const std::string& path) const;
703 
712  const bool IsUnsigned(const std::string& path) const;
713 
722  const bool IsBoolean(const std::string& path) const;
723 
732  const bool IsString(const std::string& path) const;
733 
742  const bool IsArray(const std::string& path) const;
743 
752  const bool IsPrimitive(const std::string& path) const;
753 
762  const bool IsBinary(const std::string& path) const;
763 
772  const bool IsDiscarded(const std::string& path) const;
773 
782  const bool IsNull(const std::string& path) const;
783 
792  const bool IsStructured(const std::string& path) const;
793 
831  void Flatten();
832 
871  void Unflatten();
872 
880  const std::string ToString();
881 
889  const std::string ToString() const;
890 
898  const ByteArray ToBinary();
899 
908  static GoJson ParseString(const std::string& data);
909 
918  static GoJson ParseMsgPack(const ByteArray& data);
919 
920  template<typename T>
921  friend bool operator==(const GoJson& left, const T& right);
922 
923  template <std::size_t N>
924  friend bool operator==(const GoJson& left, const char(&val)[N])
925  {
926  return left == std::string(val);
927  }
928 
929  friend std::ostream& operator<<(std::ostream& os, const GoJson& json)
930  {
931  os << json.ToString();
932 
933  return os;
934  }
935 
936  template<typename T>
937  void operator=(const T& left);
938 
939  template <std::size_t N>
940  void operator=(const char(&val)[N])
941  {
942  return *this = std::string(val);
943  }
944 
945 protected:
951  const std::shared_ptr<nlohmann::json> GetHandle(const std::string& path) const;
952 
957  const std::shared_ptr<nlohmann::json>& GetHandle() const;
958 
964  std::shared_ptr<nlohmann::json> GetHandle(const std::string& path);
965 
970  std::shared_ptr<nlohmann::json>& GetHandle();
971 
972  friend class GoRequest;
973  friend class GoJsonIterator;
974 
975 private:
976  mutable std::shared_ptr<nlohmann::json> jsonHandle;
977 };
978 
979 }
980 
981 #endif
Simplify working with json data.
Definition: GoJson.h:39
void operator=(const char(&val)[N])
Definition: GoJson.h:940
void Add(const std::string &path, const char(&val)[N])
Adds value if not exists.
Definition: GoJson.h:312
#define GoPxLSdkClass
Definition: Def.h:35
friend std::ostream & operator<<(std::ostream &os, const GoJson &json)
Definition: GoJson.h:929
Represents json iterator.
Definition: GoJsonIterator.h:25
Declares the GoPxLSdk.GoJsonIterator class.
const std::string ToString()
Gets string formatted JSON.
void Set(const std::string &path, const char(&val)[N])
Sets value.
Definition: GoJson.h:217
Definition: GoRequest.h:22
Declares the GoPxLSdk.GoJsonPointer class.
void Set(const GoJsonPointer &ptr, const char(&val)[N])
Sets value.
Definition: GoJson.h:242
void Replace(const std::string &path, const char(&val)[N])
Adds value if not exists.
Definition: GoJson.h:339
Definition: GoGdpMesh.h:45
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:924
Represents json pointer as described in Section 3 of RFC6901.
Definition: GoJsonPointer.h:21