PTLib  Version 2.14.3
 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  * Copyright (C) 2013 Post Increment
7  *
8  * The contents of this file are subject to the Mozilla Public License
9  * Version 1.0 (the "License"); you may not use this file except in
10  * compliance with the License. You may obtain a copy of the License at
11  * http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS"
14  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
15  * the License for the specific language governing rights and limitations
16  * under the License.
17  *
18  * The Original Code is PTLib
19  *
20  * The Initial Developer of the Original Code is Post Increment
21  *
22  * Contributor(s): Craig Southeren (craigs@postincrement.com)
23  *
24  * $Revision$
25  * $Author$
26  * $Date$
27  */
28 
29 #ifndef PTLIB_PJSON_H
30 #define PTLIB_PJSON_H
31 
32 #include <ptlib.h>
33 
34 #include <json/json.h>
35 
36 class PJSON : public PObject
37 {
39  public:
41  { m_json = NULL; }
42 
44  { json_object_put(m_json); }
45 
46  static PJSON Null()
47  { return PJSON(); }
48 
49  static PJSON Bool(bool v)
50  { return PJSON(v); }
51 
52  static PJSON Double(double v)
53  { return PJSON(v); }
54 
55  static PJSON Int(int v)
56  { return PJSON(v); }
57 
58  static PJSON String(const char * v)
59  { return PJSON(json_object_new_string(v)); }
60 
61  static PJSON String(const std::string & v)
62  { return PJSON(json_object_new_string(v.c_str())); }
63 
64  static PJSON String(const PString & v)
65  { return PJSON(json_object_new_string((const char *)v)); }
66 
67  static PJSON Object()
68  { return PJSON(json_object_new_object()); }
69 
70  static PJSON Array()
71  { return PJSON(json_object_new_array()); }
72 
73  static PJSON Parse(const char * str)
74  { return PJSON(json_tokener_parse(str)); }
75 
76  bool IsNull() const { return IsType(json_type_null); }
77  bool IsBool() const { return IsType(json_type_boolean); }
78  bool IsDouble() const { return IsType(json_type_double); }
79  bool IsInt() const { return IsType(json_type_int); }
80  bool IsString() const { return IsType(json_type_string); }
81 
82  bool IsObject() const { return IsType(json_type_object); }
83  bool IsArray() const { return IsType(json_type_array); }
84 
85  bool Insert(const char * key, const PJSON & obj)
86  { if (!IsObject()) return false; json_object_object_add(m_json, key, json_object_get(obj.m_json)); return true; }
87 
88  bool Remove(const char * key)
89  { if (!IsObject()) return false; json_object_object_del(m_json, key); return true; }
90 
91  bool Append(const PJSON & obj)
92  { if (!IsArray()) return false; json_object_array_add(m_json, json_object_get(obj.m_json)); return true; }
93 
94  size_t GetSize() const
95  { return m_json == NULL ? 0 : json_object_array_length(m_json); }
96 
97  ostream & operator << (ostream & strm) const
98  { PrintOn(strm); return strm; }
99 
100  virtual void PrintOn(ostream & strm) const
101  { if (m_json != NULL) strm << json_object_to_json_string(m_json); }
102 
103  void push_back(const PJSON & obj)
104  { Append(obj); }
105 
106  size_t size() const
107  { return GetSize(); }
108 
109  protected:
110  PJSON(json_object * j)
111  : m_json(j)
112  { }
113 
114  PJSON(bool v)
115  { m_json = json_object_new_boolean(v); }
116 
117  PJSON(double v)
118  { m_json = json_object_new_double(v); }
119 
120  PJSON(int v)
121  { m_json = json_object_new_int(v); }
122 
123  PJSON(const char * v)
124  { m_json = json_object_new_string(v); }
125 
126  bool IsType(enum json_type type) const { return json_object_is_type(m_json, type); }
127 
128  json_object * m_json;
129 };
130 
131 ostream & operator << (ostream & strm, const PJSON & base)
132 { base.PrintOn(strm); return strm; }
133 
134 #endif // PTLIB_PJSON_H