InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IJsonUtils.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: swagarg
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __IJsonUtils__
25 #define __IJsonUtils__
26 
27 #include <vector>
28 #include <map>
29 #include <string>
30 #include <regex>
31 
32 #include "boost/property_tree/json_parser.hpp"
33 
34 class JSON;
35 
36 typedef boost::property_tree::ptree Tree;
37 typedef std::vector<JSON> JSONArray;
38 
39 class JSON
40 {
41  Tree value;
42 public:
43  /*
44  Enumeration for different types part of json tree
45  */
46  enum Type
47  {
48  type_List,
49  type_Map,
50  type_String,
51  type_Number,
52  type_Bool,
53  type_Null
54  };
55 
56  JSON(){}
57  JSON(Tree inputValue) : value(inputValue){}
58  Tree GetTree() { return value; }
59 
60  /*
61  Checks if the json is empty
62  @return true if json is empty.
63  */
64  bool IsEmpty();
65 
66  /*
67  Gets string value for the key
68  @param [IN] key in json
69  @return string value for the key
70  */
71  std::string GetString(std::string key);
72  std::string GetString();
73 
74  /*
75  Gets numeric value for the key
76  @param [IN] key in json
77  @return numeric value for the key
78  */
79  uint64 GetNumber(std::string key);
80 
81  /*
82  Gets double value for the key
83  @param [IN] key in json
84  @return double value for the key
85  */
86  double GetDouble(const std::string& key);
87 
88  /*
89  Gets Float value for the key
90  @param [IN] key in json
91  @return float value for the key
92  */
93  float GetFloat(const std::string& key);
94 
95  /*
96  Gets Boolean Value for key
97  @param [IN] key in json
98  @return boolean value for the key
99  */
100  bool GetBoolean(const std::string& key);
101 
102  /*
103  Gets JSON array list value for the key
104  @param [IN] key in json
105  @param [OUT] output json array list value for the key
106  */
107  void GetListAt(std::string key, JSONArray &outputArray);
108 
109  /*
110  Gets json value for the key
111  @param [IN] key in json
112  @param [OUT] output json value for the key
113  */
114  void GetJSONAt(std::string key, JSON &output);
115 
116  /*
117  Gets type of json key
118  @param [IN] key in json
119  @return key type
120  */
121  Type GetType(std::string key);
122 
123  /*
124  Checks if valid value for key exists in json tree
125  @param [IN] key in json
126  @return true if valid value for the key exists in json tree else false
127  */
128  bool checkKey(std::string key);
129 
130  /*
131  Checks if value for key is of type map in json tree
132  @param [IN] key in json
133  @return true value for key is of type map in json tree else false
134  */
135  bool isMap(std::string key);
136 
137  /*
138  Checks if key is present json tree
139  @param [IN] key in json
140  @return true value for key in json tree else false
141  */
142  bool IsKeyPresent(std::string key);
143 
144  /*
145  Checks if value for key is of type list in jsontree
146  @param [IN] key in json
147  @return true value for key is of type list in jsontree else false
148  */
149  bool isList(std::string key);
150 
151  /*
152  Checks if value for key is of type number
153  @param [IN] key in json
154  @return true value for key is of type number else false
155  */
156  bool isNumber(std::string key);
157 
158  /*
159  Checks if value for key is of type string
160  @param [IN] key in json
161  @return true value for key is of type string else false
162  */
163  bool isString(std::string key);
164 
165  /*
166  Checks if value for key is of type bool in jsontree
167  @param [IN] key in json
168  @return true value for key is of type bool in jsontree else false
169  */
170  bool isBool(std::string key);
171 
172  /*
173  Adds key and value to json tree
174  @param [IN] key in json
175  @param [IN] value in json
176  */
177  void addValue(std::string key, std::string val);
178 
179  /*
180  Reads the json tree from string stream
181  @param sstream [IN] input stringstream which fills json tree
182  */
183  void read_json(std::stringstream &sstream);
184 
185  /*
186  Writes the json tree into string stream
187  @param sstream [OUT] output stringstream filled with JSON tree
188  @param isPretty [IN] if output is to be dumped in PrettyFormat.
189  */
190  void write_json(std::stringstream &stream, bool ispretty = false);
191 
192  /*
193  Writes the json tree into string
194  @param sstream [OUT] output string filled with JSON tree
195  @param isPretty [IN] if output is to be dumped in PrettyFormat.
196  */
197  void write_json(std::string &str, bool ispretty = false);
198 
199  /*
200  Adds key and value to json tree
201  @param [IN] key in json
202  @param [IN] value in json
203  */
204  void AddValue(std::string key, bool val);
205 
206  /*
207  Adds key and value to json tree
208  @param [IN] key in json
209  @param [IN] value in json
210  */
211  void AddValue(std::string key, float val);
212 
213  /*
214  Adds key and value to json tree
215  @param [IN] key in json
216  @param [IN] value in json
217  */
218  void AddValue(std::string key, double val);
219 
220  /*
221  Adds key and value to json tree
222  @param [IN] key in json
223  @param [IN] value in json
224  */
225  void AddValue(std::string key, int val);
226 
227  /*
228  Adds key and value to json tree
229  @param [IN] key in json
230  @param [IN] value in json
231  */
232  void AddValue(std::string key, Tree val);
233 
234  /*
235  Adds key and value to json tree
236  @param [IN] key in json
237  @param [IN] value in json
238  */
239  void PutValue(std::string key, Tree val);
240 
241  /*
242  Adds key and value to json tree
243  @param [IN] key in json
244  @param [IN] value in json
245  */
246  void AddValue(std::string key, JSONArray val);
247 
248  /*
249  Adds key and value to json tree
250  @param [IN] key in json
251  @param [IN] value in json
252  */
253  void PushValue(std::string key, Tree val);
254 
255 };
256 
257 #endif // __IJsonUtils__