PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pjson.h
Go to the documentation of this file.
1 /*
2  * pjson.h
3  *
4  * JSON parser
5  *
6  * Portable Tools Library
7  *
8  * Copyright (C) 2015 by Vox Lucida Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Tools Library.
21  *
22  * The Initial Developer of the Original Code is Vox Lucida Pty. Ltd.
23  *
24  * Contributor(s): Robert Jongbloed
25  * Blackboard, inc
26  */
27 
28 #ifndef PTLIB_PJSON_H
29 #define PTLIB_PJSON_H
30 
31 #include <ptlib.h>
32 
33 class PJSON : public PObject
34 {
36  public:
37  enum Types
38  {
45  };
46 
47  class Base
48  {
49  public:
50  Base() { }
51  virtual ~Base() { }
52  virtual bool IsType(Types type) const = 0;
53  virtual void ReadFrom(istream & strm) = 0;
54  virtual void PrintOn(ostream & strm) const = 0;
55  virtual Base * DeepClone() const = 0;
56  private:
57  Base(const Base &) { }
58  void operator=(const Base &);
59 
60  friend ostream & operator<<(ostream & s, const Base & b) { b.PrintOn(s); return s; }
61  };
62 
63  class Array;
64 
65  typedef long double NumberType;
66 
67  class Object : public Base, public std::map<std::string, Base *>
68  {
69  public:
70  Object() { }
71  ~Object();
72  virtual bool IsType(Types type) const;
73  virtual void ReadFrom(istream & strm);
74  virtual void PrintOn(ostream & strm) const;
75  virtual Base * DeepClone() const;
76 
77  bool IsType(const PString & name, Types type) const;
78 
79  template <class T> const T * Get(const PString & name) const
80  {
81  const_iterator it = find(name);
82  return it != end() ? dynamic_cast<const T *>(it->second) : NULL;
83  }
84  template <class T> T * Get(const PString & name)
85  {
86  iterator it = find(name);
87  return it != end() ? dynamic_cast<T *>(it->second) : NULL;
88  }
89  const Object & GetObject(const PString & name) const { return *Get<Object>(name); }
90  Object & GetObject(const PString & name) { return *Get<Object>(name); }
91  const Array & GetArray(const PString & name) const { return *Get<Array>(name); }
92  Array & GetArray(const PString & name) { return *Get<Array>(name); }
93 
94  PString GetString(const PString & name) const;
95  int GetInteger(const PString & name) const;
96  int64_t GetInteger64(const PString & name) const;
97  unsigned GetUnsigned(const PString & name) const;
98  uint64_t GetUnsigned64(const PString & name) const;
99  NumberType GetNumber(const PString & name) const;
100  bool GetBoolean(const PString & name) const;
101 
102  bool Set(const PString & name, Types type);
103  bool Set(const PString & name, const Base & toInsert);
104  bool Set(const PString & name, const PJSON & toInsert);
105  Object & SetObject(const PString & name);
106  Array & SetArray(const PString & name);
107  bool SetString(const PString & name, const PString & value);
108  bool SetNumber(const PString & name, NumberType value);
109  bool SetBoolean(const PString & name, bool value);
110 
111  bool Remove(const PString & name);
112 
113  private:
114  Object(const Object &) { }
115  void operator=(const Object &) { }
116  };
117 
118  class Array : public Base, public std::vector<Base *>
119  {
120  public:
121  Array() { }
122  ~Array();
123  virtual bool IsType(Types type) const;
124  virtual void ReadFrom(istream & strm);
125  virtual void PrintOn(ostream & strm) const;
126  virtual Base * DeepClone() const;
127 
128  bool IsType(size_t index, Types type) const;
129 
130  template <class T> const T * Get(size_t index) const { return index < size() ? dynamic_cast<const T *>(at(index)) : NULL; }
131  template <class T> T * Get(size_t index) { return index < size() ? dynamic_cast< T *>(at(index)) : NULL; }
132  const Object & GetObject(size_t index) const { return *Get<Object>(index); }
133  Object & GetObject(size_t index) { return *Get<Object>(index); }
134  const Array & GetArray(size_t index) const { return *Get<Array>(index); }
135  Array & GetArray(size_t index) { return *Get<Array>(index); }
136 
137  PString GetString(size_t index) const;
138  int GetInteger(size_t index) const;
139  int64_t GetInteger64(size_t index) const;
140  unsigned GetUnsigned(size_t index) const;
141  uint64_t GetUnsigned64(size_t index) const;
142  NumberType GetNumber(size_t index) const;
143  bool GetBoolean(size_t index) const;
144 
145  void Append(Types type);
146  void Append(const Base & toAppend);
147  void Append(const PJSON & toAppend);
148  Object & AppendObject();
149  Array & AppendArray();
150  void AppendString(const PString & value);
151  void AppendNumber(NumberType value);
152  void AppendBoolean(bool value);
153 
154  bool Remove(size_t index);
155 
156  private:
157  Array(const Array &) { }
158  void operator=(const Array &) { }
159  };
160 
161  class String : public Base, public PString
162  {
163  public:
164  String(const char * str = NULL) : PString(str) { }
165  virtual bool IsType(Types type) const;
166  virtual void ReadFrom(istream & strm);
167  virtual void PrintOn(ostream & strm) const;
168  virtual Base * DeepClone() const;
169  String & operator=(const char * str) { PString::operator=(str); return *this; }
170  String & operator=(const PString & str) { PString::operator=(str); return *this; }
171  };
172 
173  class Number : public Base
174  {
175  protected:
177  public:
178  explicit Number(NumberType value = 0);
179  virtual bool IsType(Types type) const;
180  virtual void ReadFrom(istream & strm);
181  virtual void PrintOn(ostream & strm) const;
182  virtual Base * DeepClone() const;
183  Number & operator=(NumberType value) { m_value = value; return *this; }
184  void SetValue(NumberType value) { m_value = value; }
185  NumberType GetValue() const { return m_value; }
186  };
187 
188  class Boolean : public Base
189  {
190  protected:
191  bool m_value;
192  public:
193  explicit Boolean(bool value = false);
194  virtual bool IsType(Types type) const;
195  virtual void ReadFrom(istream & strm);
196  virtual void PrintOn(ostream & strm) const;
197  virtual Base * DeepClone() const;
198  Boolean & operator=(bool value) { m_value = value; return *this; }
199  void SetValue(bool value) { m_value = value; }
200  bool GetValue() const { return m_value; }
201  };
202 
203  class Null : public Base
204  {
205  public:
206  virtual bool IsType(Types type) const;
207  virtual void ReadFrom(istream & strm);
208  virtual void PrintOn(ostream & strm) const;
209  virtual Base * DeepClone() const;
210  };
211 
213  PJSON();
214  explicit PJSON(Types type);
215  explicit PJSON(const PString & str);
216  PJSON(const PJSON & other);
217  PJSON & operator=(const PJSON & other);
218 
219  ~PJSON() { delete m_root; }
220 
221  virtual void ReadFrom(istream & strm);
222  virtual void PrintOn(ostream & strm) const;
223 
224  bool FromString(
225  const PString & str
226  );
227 
229  std::streamsize initialIndent = 0,
230  std::streamsize subsequentIndent = 0) const;
231 
232  bool IsValid() const { return m_valid; }
233 
234  bool IsType(Types type) const { return PAssertNULL(m_root)->IsType(type); }
235 
236  void Set(Types type);
237 
238  template <class T> const T & GetAs() const { return dynamic_cast<const T &>(*PAssertNULL(m_root)); }
239  template <class T> T & GetAs() { return dynamic_cast< T &>(*PAssertNULL(m_root)); }
240  const Object & GetObject() const { return GetAs<Object>(); }
241  Object & GetObject() { return GetAs<Object>(); }
242  const Array & GetArray () const { return GetAs<Array>(); }
243  Array & GetArray () { return GetAs<Array>(); }
244  const String & GetString() const { return GetAs<String>(); }
245  String & GetString() { return GetAs<String>(); }
246  const Number & GetNumber() const { return GetAs<Number>(); }
247  Number & GetNumber() { return GetAs<Number>(); }
248  const Boolean & GetBoolean() const { return GetAs<Boolean>(); }
249  Boolean & GetBoolean() { return GetAs<Boolean>(); }
250 
251  protected:
253  bool m_valid;
254 };
255 
256 
257 #if P_SSL
258 
262 class PJWT : public PJSON
263 {
264  PCLASSINFO(PJWT, PJSON);
265  public:
267  PJWT();
268 
272  explicit PJWT(
273  const PString & str,
274  const PString & secret = PString::Empty(),
275  const PTime & verifyTime = PTime(0)
276  );
277 
279  P_DECLARE_STREAMABLE_ENUM(Algorithm,
280  none,
281  HS256, // HMAC SHA-256
282  HS384, // HMAC SHA-384
283  HS512 // HMAC SHA-512
284  );
285 
288  PString Encode(
289  const PString & secret = PString::Empty(),
290  const Algorithm algorithm = HS256
291  );
292 
295  bool Decode(
296  const PString & str,
297  const PString & secret = PString::Empty(),
298  const PTime & verifyTime = PTime(0)
299  );
300 
301  void SetIssuer(const PString & str);
302  PString GetIssuer() const;
303  void SetSubject(const PString & str);
304  PString GetSubject() const;
305  void SetAudience(const PString & str);
306  PString GetAudience() const;
307  void SetExpiration(const PTime & when);
308  PTime GetExpiration() const;
309  void SetNotBefore(const PTime & when);
310  PTime GetNotBefore() const;
311  void SetIssuedAt(const PTime & when);
312  PTime GetIssuedAt() const;
313  void SetTokenId(const PString & str);
314  PString GetTokenId() const;
315 
316  void SetPrivate(const PString & key, const PString & str);
317  PString GetPrivate(const PString & key) const;
318 };
319 
320 #endif // P_SSL
321 
322 #endif // PTLIB_PJSON_H
void Set(Types type)
Object & AppendObject()
virtual bool IsType(Types type) const
Definition: pjson.h:118
bool Set(const PString &name, Types type)
Number(NumberType value=0)
const Array & GetArray(size_t index) const
Definition: pjson.h:134
virtual bool IsType(Types type) const
unsigned GetUnsigned(size_t index) const
virtual void PrintOn(ostream &strm) const
Output the string to the specified stream.
PString & operator=(const PString &str)
Assign the string to the current object.
Object & GetObject(const PString &name)
Definition: pjson.h:90
Base * m_root
Definition: pjson.h:252
const Number & GetNumber() const
Definition: pjson.h:246
Definition: pjson.h:41
Object & GetObject(size_t index)
Definition: pjson.h:133
bool GetBoolean(size_t index) const
virtual bool IsType(Types type) const
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
String(const char *str=NULL)
Definition: pjson.h:164
const Object & GetObject(const PString &name) const
Definition: pjson.h:89
String & operator=(const PString &str)
Definition: pjson.h:170
bool SetNumber(const PString &name, NumberType value)
bool SetBoolean(const PString &name, bool value)
PJSON & operator=(const PJSON &other)
NumberType m_value
Definition: pjson.h:176
Definition: pjson.h:188
This class defines an absolute time and date.
Definition: ptime.h:49
uint64_t GetUnsigned64(size_t index) const
bool Remove(size_t index)
virtual void PrintOn(ostream &strm) const
virtual Base * DeepClone() const
Definition: pjson.h:173
virtual void ReadFrom(istream &strm)
virtual void ReadFrom(istream &strm)
virtual Base * DeepClone() const
Array & GetArray(size_t index)
Definition: pjson.h:135
Boolean & operator=(bool value)
Definition: pjson.h:198
long double NumberType
Definition: pjson.h:63
int GetInteger(const PString &name) const
virtual void PrintOn(ostream &strm) const
PString AsString(std::streamsize initialIndent=0, std::streamsize subsequentIndent=0) const
virtual Base * DeepClone() const
const T * Get(size_t index) const
Definition: pjson.h:130
bool GetBoolean(const PString &name) const
bool FromString(const PString &str)
virtual void PrintOn(ostream &strm) const
const Object & GetObject() const
Definition: pjson.h:240
unsigned GetUnsigned(const PString &name) const
PString GetString(size_t index) const
Definition: pjson.h:33
Base()
Definition: pjson.h:50
virtual void ReadFrom(istream &strm)
virtual void ReadFrom(istream &strm)
virtual bool IsType(Types type) const
virtual Base * DeepClone() const
Boolean & GetBoolean()
Definition: pjson.h:249
virtual void ReadFrom(istream &strm)=0
Array()
Definition: pjson.h:121
NumberType GetNumber(const PString &name) const
Array & AppendArray()
virtual void ReadFrom(istream &strm)
~PJSON()
Definition: pjson.h:219
bool Remove(const PString &name)
int GetInteger(size_t index) const
const T & GetAs() const
Definition: pjson.h:238
Definition: pjson.h:161
virtual bool IsType(Types type) const =0
Array & GetArray(const PString &name)
Definition: pjson.h:92
virtual bool IsType(Types type) const
void AppendNumber(NumberType value)
bool GetValue() const
Definition: pjson.h:200
Boolean(bool value=false)
virtual void PrintOn(ostream &strm) const =0
const Array & GetArray() const
Definition: pjson.h:242
Definition: pjson.h:43
T * Get(size_t index)
Definition: pjson.h:131
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:428
Number & GetNumber()
Definition: pjson.h:247
Constructor.
Definition: pjson.h:203
virtual void PrintOn(ostream &strm) const
virtual Base * DeepClone() const
uint64_t GetUnsigned64(const PString &name) const
virtual bool IsType(Types type) const
Definition: pjson.h:47
virtual void ReadFrom(istream &strm)
Input the string from the specified stream.
String & GetString()
Definition: pjson.h:245
virtual Base * DeepClone() const =0
#define P_DECLARE_STREAMABLE_ENUM(name, first,...)
This declares a standard enumeration using P_DECLARE_ENUM() and adds the text names so can be streame...
Definition: object.h:326
Array & GetArray()
Definition: pjson.h:243
T & GetAs()
Definition: pjson.h:239
The character string class.
Definition: pstring.h:108
int64_t GetInteger64(const PString &name) const
Definition: pjson.h:67
const Object & GetObject(size_t index) const
Definition: pjson.h:132
int64_t GetInteger64(size_t index) const
virtual void PrintOn(ostream &strm) const
virtual void ReadFrom(istream &strm)
virtual Base * DeepClone() const
virtual ~Base()
Definition: pjson.h:51
void AppendBoolean(bool value)
bool m_value
Definition: pjson.h:191
bool m_valid
Definition: pjson.h:253
void AppendString(const PString &value)
static const PString & Empty()
Return an empty string.
String & operator=(const char *str)
Definition: pjson.h:169
Number & operator=(NumberType value)
Definition: pjson.h:183
Object & SetObject(const PString &name)
void SetValue(NumberType value)
Definition: pjson.h:184
friend ostream & operator<<(ostream &s, const Base &b)
Definition: pjson.h:60
const Array & GetArray(const PString &name) const
Definition: pjson.h:91
Definition: pjson.h:44
bool SetString(const PString &name, const PString &value)
Definition: pjson.h:42
NumberType GetNumber(size_t index) const
PString GetString(const PString &name) const
Array & SetArray(const PString &name)
Object & GetObject()
Definition: pjson.h:241
const T * Get(const PString &name) const
Definition: pjson.h:79
const String & GetString() const
Definition: pjson.h:244
NumberType GetValue() const
Definition: pjson.h:185
const Boolean & GetBoolean() const
Definition: pjson.h:248
void SetValue(bool value)
Definition: pjson.h:199
Definition: pjson.h:39
virtual void PrintOn(ostream &strm) const
bool IsValid() const
Definition: pjson.h:232
Types
Definition: pjson.h:37
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
void Append(Types type)
T * Get(const PString &name)
Definition: pjson.h:84
bool IsType(Types type) const
Definition: pjson.h:234
Definition: pjson.h:40
Object()
Definition: pjson.h:70