HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_JSONHandle.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_JSONHandle.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_JSONHandle__
12 #define __UT_JSONHandle__
13 
14 #include "UT_API.h"
15 #include <SYS/SYS_Types.h>
16 #include <SYS/SYS_String.h>
17 #include <iosfwd>
18 
19 class UT_JSONParser;
20 class UT_IStream;
21 class UT_WorkBuffer;
22 class UT_StringHolder;
23 
24 /// @brief UT_JSONHandle processes events from a UT_JSONParser parser
25 ///
26 /// The UT_JSONParser is passed a UT_JSONHandle to interpret the JSON tokens.
27 /// As each entity is encountered, an event callback is invoked on the handle
28 /// to process the token.
29 ///
30 /// It's possible to recurse into the parser from within a callback with a
31 /// different handler and possibly even a different input stream by calling
32 /// UT_JSONParser::parseObject().
33 ///
34 /// @see UT_JSONParser, UT_JSONHandleError, UT_JSONHandleNull, UT_JSONIStream
35 
37 public:
39  virtual ~UT_JSONHandle() {}
40 
41  UT_JSONHandle(const UT_JSONHandle &) = delete;
42  UT_JSONHandle &operator=(const UT_JSONHandle &) = delete;
43 
44  /// Convenience method to parse a single object
45  bool parseObject(UT_JSONParser &p, UT_IStream *is=0);
46 
47  /// The Null Handle acts as a pass through when parsing a JSON file. It
48  /// can be used for simple syntax checking:
49  /// @code
50  /// UT_JSONHandle *h = UT_JSONHandle::getNullHandle();
51  /// UT_JSONParser p;
52  /// success = p.parseObject(*h);
53  /// UT_JSONHandle::releaseNullHandle(h);
54  /// @endcode
55  static UT_JSONHandle *getNullHandle();
56  /// Instead of deleting the null handle, please call releaseNullHandle()
57  static void releaseNullHandle(UT_JSONHandle *h);
58 
59  /// Event method to process a null token
60  virtual bool jsonNull(UT_JSONParser &p) = 0;
61  /// Event method to process a bool (true or false tokens)
62  virtual bool jsonBool(UT_JSONParser &p, bool value) = 0;
63 
64  /// Event method to process an integer
65  virtual bool jsonInt(UT_JSONParser &p, int64 value) = 0;
66  /// Event method to process a real/float
67  virtual bool jsonReal(UT_JSONParser &p, fpreal64 value) = 0;
68 
69  /// Event method to process a string value
70  virtual bool jsonString(UT_JSONParser &p, const char *value,
71  int64 len) = 0;
72  /// The string event taking a UT_StringHolder calls the jsonString() method
73  /// by default. However, if you can process a UT_StringHolder instead, you
74  /// can get a direct reference to the UT_StringHolder.
75  virtual bool jsonStringHolder(UT_JSONParser &p,
76  const UT_StringHolder &s);
77  /// Event method to process the key of a map/object is read
78  virtual bool jsonKey(UT_JSONParser &p, const char *v, int64 len)=0;
79  virtual bool jsonKeyHolder(UT_JSONParser &p,
80  const UT_StringHolder &s);
81 
82  /// Event method invoked at the start of a map/object
83  virtual bool jsonBeginMap(UT_JSONParser &p) = 0;
84  /// Event method invoked at the end of a map/object
85  virtual bool jsonEndMap(UT_JSONParser &p) = 0;
86  /// Event method invoked at the beginning of an array object
87  virtual bool jsonBeginArray(UT_JSONParser &p) = 0;
88  /// Event method invoked at the end of an array object
89  virtual bool jsonEndArray(UT_JSONParser &p) = 0;
90 
91  /// Event method to handle for uniform array of bool (encoded as bit
92  /// fields)
93  virtual bool uaBool(UT_JSONParser &p, int64 length);
94  /// Event method to handle uniform array of int8 data
95  virtual bool uaInt8(UT_JSONParser &p, int64 length);
96  /// Event method to handle uniform array of int16 data
97  virtual bool uaInt16(UT_JSONParser &p, int64 length);
98  /// Event method to handle uniform array of int32 data
99  virtual bool uaInt32(UT_JSONParser &p, int64 length);
100  /// Event method to handle uniform array of int64 data
101  virtual bool uaInt64(UT_JSONParser &p, int64 length);
102  /// Event method to handle uniform array of real16 data
103  virtual bool uaReal16(UT_JSONParser &p, int64 length);
104  /// Event method to handle uniform array of real32 data
105  virtual bool uaReal32(UT_JSONParser &p, int64 length);
106  /// Event method to handle uniform array of real64 data
107  virtual bool uaReal64(UT_JSONParser &p, int64 length);
108  /// Event method to handle uniform array of unsigned uint8 data
109  virtual bool uaUInt8(UT_JSONParser &p, int64 length);
110  /// Event method to handle uniform array of unsigned uint16 data
111  virtual bool uaUInt16(UT_JSONParser &p, int64 length);
112  /// Event method to handle uniform array of string data (use uaReadString)
113  virtual bool uaString(UT_JSONParser &p, int64 length);
114  /// Event method to handle uniform array of string token data (use
115  /// uaReadStringToken)
116  virtual bool uaStringToken(UT_JSONParser &p, int64 length);
117 
118  /// This method is called by addError to create the prefix for error
119  /// messages. By default, the method looks something like:
120  /// @code
121  /// msg.sprintf("JSON ERROR[%ld]:", (long)p.getStreamPosition());
122  /// @endcode
123  virtual void errorPrefix(UT_JSONParser &p, UT_WorkBuffer &msg);
124 
125 protected:
126  /// Convenience method to test key values
127  bool isKey(const char *s1, const char *s2) const
128  { return !SYSstrcasecmp(s1, s2); }
129  /// Convenience method to add an error about a bad key name
130  /// addError("Unexpected key token '%s'" % key);
131  /// This also skips the next object.
132  void skipBadKey(UT_JSONParser &p, const char *key);
133  /// Convenience method to add a fatal error about a bad key name
134  /// addError("Unexpected key token '%s'" % key);
135  void fatalBadKey(UT_JSONParser &p, const char *key);
136 };
137 
138 /// @brief This class generates errors on any JSON events.
139 ///
140 /// The UT_JSONHandleError class will throw errors on any JSON events. It's
141 /// useful as a base class for an event handler which only expects certain
142 /// events. For example, if you only expect the keyword argument, you might
143 /// have a class like: @code
144 /// class MyHandle : public UT_JSONHandleError {
145 /// virtual bool jsonKey(UT_JSONParser &p, const char *v, int64 len);
146 /// virtual const char *className() { return "MyClass" }
147 /// }
148 /// @endcode
149 /// The base class would handle trapping errors on anything other than a
150 /// map key.
151 ///
152 /// @note Uniform arrays are handled by the UT_JSONHandle base class
153 ///
154 /// @see UT_JSONHandle, UT_JSONHandleNull
156 public:
157  /// @param fatal @n
158  /// If fatal is true then errors will cause parsing to terminate.
159  /// Otherwise, parsing will continue and errors will cascade.
160  UT_JSONHandleError(bool fatal=true) : myFatal(fatal) {}
161  ~UT_JSONHandleError() override {}
162 
163  UT_JSONHandleError(const UT_JSONHandleError &) = delete;
165 
166  /// getClassName() should return a meaningful label. The errors generated
167  /// will be:
168  /// Error parsing %s: Unexpected "type" JSON object
169  /// The default is an empty string.
170  virtual const char *getClassName() const;
171 
172  bool jsonNull(UT_JSONParser &p) override;
173  bool jsonBool(UT_JSONParser &p, bool value) override;
174  bool jsonInt(UT_JSONParser &p, int64 value) override;
175  bool jsonReal(UT_JSONParser &p, fpreal64 value) override;
176  bool jsonString(UT_JSONParser &p, const char *v, int64 len) override;
177  bool jsonKey(UT_JSONParser &p, const char *v, int64 len) override;
178  bool jsonBeginMap(UT_JSONParser &p) override;
179  bool jsonEndMap(UT_JSONParser &p) override;
180  bool jsonBeginArray(UT_JSONParser &p) override;
181  bool jsonEndArray(UT_JSONParser &p) override;
182 protected:
183  /// Add a type error for the given name
184  virtual bool typeError(UT_JSONParser &p, const char *name);
185  /// Check whether errors should be fatal (or just warnings)
186  bool getFatal() const { return myFatal; }
187  /// Set whether errors should be fatal (or just warnings)
188  void setFatal(bool f) { myFatal = f; }
189 
190 private:
191  /// Whether errors terminate parsing.
192  bool myFatal;
193 };
194 
195 /// @brief This class skips over any JSON events.
196 ///
197 /// The UT_JSONHandleNull will simply skip over any JSON events.
198 /// useful as a base class for an event handler which only expects certain
199 /// events. For example, if you only expect the keyword argument, you might
200 /// have a class like: @code
201 /// class MyHandle : public UT_JSONHandleNull {
202 /// virtual bool jsonInt(UT_JSONParser &p, int64 len);
203 /// virtual bool jsonReal(UT_JSONParser &p, fpreal64 len);
204 /// virtual const char *className() { return "MyClass" }
205 /// }
206 /// @endcode
207 /// The base class would skip over anything other than an integer or real.
208 ///
209 /// @note Uniform arrays are handled by the UT_JSONHandle base class
210 ///
211 /// @see UT_JSONHandle, UT_JSONHandleError
213 public:
215  ~UT_JSONHandleNull() override {}
216 
217  UT_JSONHandleNull(const UT_JSONHandleNull &) = delete;
218  UT_JSONHandleNull &operator=(const UT_JSONHandleNull &) = delete;
219 
220  bool jsonNull(UT_JSONParser &p) override;
221  bool jsonBool(UT_JSONParser &p, bool value) override;
222  bool jsonInt(UT_JSONParser &p, int64 value) override;
223  bool jsonReal(UT_JSONParser &p, fpreal64 value) override;
224  bool jsonString(UT_JSONParser &p, const char *v, int64 len) override;
225  bool jsonKey(UT_JSONParser &p, const char *v, int64 len) override;
226  bool jsonBeginMap(UT_JSONParser &p) override;
227  bool jsonEndMap(UT_JSONParser &p) override;
228  bool jsonBeginArray(UT_JSONParser &p) override;
229  bool jsonEndArray(UT_JSONParser &p) override;
230 };
231 
232 
233 #endif
virtual bool jsonInt(UT_JSONParser &p, int64 value)=0
Event method to process an integer.
virtual ~UT_JSONHandle()
Definition: UT_JSONHandle.h:39
const GLdouble * v
Definition: glcorearb.h:837
UT_JSONHandle processes events from a UT_JSONParser parser.
Definition: UT_JSONHandle.h:36
virtual bool jsonBool(UT_JSONParser &p, bool value)=0
Event method to process a bool (true or false tokens)
GLdouble s
Definition: glad.h:3009
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define UT_API
Definition: UT_API.h:14
virtual bool jsonBeginArray(UT_JSONParser &p)=0
Event method invoked at the beginning of an array object.
UT_JSONHandleError(bool fatal=true)
double fpreal64
Definition: SYS_Types.h:201
virtual bool jsonEndArray(UT_JSONParser &p)=0
Event method invoked at the end of an array object.
virtual bool jsonKey(UT_JSONParser &p, const char *v, int64 len)=0
Event method to process the key of a map/object is read.
GLfloat f
Definition: glcorearb.h:1926
void setFatal(bool f)
Set whether errors should be fatal (or just warnings)
long long int64
Definition: SYS_Types.h:116
virtual bool jsonReal(UT_JSONParser &p, fpreal64 value)=0
Event method to process a real/float.
GLuint const GLchar * name
Definition: glcorearb.h:786
This class generates errors on any JSON events.
virtual bool jsonString(UT_JSONParser &p, const char *value, int64 len)=0
Event method to process a string value.
~UT_JSONHandleError() override
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
bool isKey(const char *s1, const char *s2) const
Convenience method to test key values.
~UT_JSONHandleNull() override
LeafData & operator=(const LeafData &)=delete
int SYSstrcasecmp(const char *a, const char *b)
Definition: SYS_String.h:220
virtual bool jsonBeginMap(UT_JSONParser &p)=0
Event method invoked at the start of a map/object.
UT_JSONHandle & operator=(const UT_JSONHandle &)=delete
This class skips over any JSON events.
Definition: core.h:1131
bool getFatal() const
Check whether errors should be fatal (or just warnings)
virtual bool jsonNull(UT_JSONParser &p)=0
Event method to process a null token.
virtual bool jsonEndMap(UT_JSONParser &p)=0
Event method invoked at the end of a map/object.