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 {
38 public:
40  virtual ~UT_JSONHandle() {}
41 
42  UT_JSONHandle(const UT_JSONHandle &) = delete;
43  UT_JSONHandle &operator=(const UT_JSONHandle &) = delete;
44 
45  /// Convenience method to parse a single object
46  bool parseObject(UT_JSONParser &p, UT_IStream *is=0);
47 
48  /// The Null Handle acts as a pass through when parsing a JSON file. It
49  /// can be used for simple syntax checking:
50  /// @code
51  /// UT_JSONHandle *h = UT_JSONHandle::getNullHandle();
52  /// UT_JSONParser p;
53  /// success = p.parseObject(*h);
54  /// UT_JSONHandle::releaseNullHandle(h);
55  /// @endcode
56  static UT_JSONHandle *getNullHandle();
57  /// Instead of deleting the null handle, please call releaseNullHandle()
58  static void releaseNullHandle(UT_JSONHandle *h);
59 
60  /// Event method to process a null token
61  virtual bool jsonNull(UT_JSONParser &p) = 0;
62  /// Event method to process a bool (true or false tokens)
63  virtual bool jsonBool(UT_JSONParser &p, bool value) = 0;
64 
65  /// Event method to process an integer
66  virtual bool jsonInt(UT_JSONParser &p, int64 value) = 0;
67  /// Event method to process a real/float
68  virtual bool jsonReal(UT_JSONParser &p, fpreal64 value) = 0;
69 
70  /// Event method to process a string value
71  virtual bool jsonString(UT_JSONParser &p, const char *value,
72  int64 len) = 0;
73  /// The string event taking a UT_StringHolder calls the jsonString() method
74  /// by default. However, if you can process a UT_StringHolder instead, you
75  /// can get a direct reference to the UT_StringHolder.
76  virtual bool jsonStringHolder(UT_JSONParser &p,
77  const UT_StringHolder &s);
78  /// Event method to process the key of a map/object is read
79  virtual bool jsonKey(UT_JSONParser &p, const char *v, int64 len)=0;
80  virtual bool jsonKeyHolder(UT_JSONParser &p,
81  const UT_StringHolder &s);
82 
83  /// Event method invoked at the start of a map/object
84  virtual bool jsonBeginMap(UT_JSONParser &p) = 0;
85  /// Event method invoked at the end of a map/object
86  virtual bool jsonEndMap(UT_JSONParser &p) = 0;
87  /// Event method invoked at the beginning of an array object
88  virtual bool jsonBeginArray(UT_JSONParser &p) = 0;
89  /// Event method invoked at the end of an array object
90  virtual bool jsonEndArray(UT_JSONParser &p) = 0;
91 
92  /// Event method to handle for uniform array of bool (encoded as bit
93  /// fields)
94  virtual bool uaBool(UT_JSONParser &p, int64 length);
95  /// Event method to handle uniform array of int8 data
96  virtual bool uaInt8(UT_JSONParser &p, int64 length);
97  /// Event method to handle uniform array of int16 data
98  virtual bool uaInt16(UT_JSONParser &p, int64 length);
99  /// Event method to handle uniform array of int32 data
100  virtual bool uaInt32(UT_JSONParser &p, int64 length);
101  /// Event method to handle uniform array of int64 data
102  virtual bool uaInt64(UT_JSONParser &p, int64 length);
103  /// Event method to handle uniform array of real16 data
104  virtual bool uaReal16(UT_JSONParser &p, int64 length);
105  /// Event method to handle uniform array of real32 data
106  virtual bool uaReal32(UT_JSONParser &p, int64 length);
107  /// Event method to handle uniform array of real64 data
108  virtual bool uaReal64(UT_JSONParser &p, int64 length);
109  /// Event method to handle uniform array of unsigned uint8 data
110  virtual bool uaUInt8(UT_JSONParser &p, int64 length);
111  /// Event method to handle uniform array of unsigned uint16 data
112  virtual bool uaUInt16(UT_JSONParser &p, int64 length);
113  /// Event method to handle uniform array of string data (use uaReadString)
114  virtual bool uaString(UT_JSONParser &p, int64 length);
115  /// Event method to handle uniform array of string token data (use
116  /// uaReadStringToken)
117  virtual bool uaStringToken(UT_JSONParser &p, int64 length);
118 
119  /// This method is called by addError to create the prefix for error
120  /// messages. By default, the method looks something like:
121  /// @code
122  /// msg.sprintf("JSON ERROR[%ld]:", (long)p.getStreamPosition());
123  /// @endcode
124  virtual void errorPrefix(UT_JSONParser &p, UT_WorkBuffer &msg);
125 
126 protected:
127  /// Convenience method to test key values
128  bool isKey(const char *s1, const char *s2) const
129  { return !SYSstrcasecmp(s1, s2); }
130  /// Convenience method to add an error about a bad key name
131  /// addError("Unexpected key token '%s'" % key);
132  /// This also skips the next object.
133  void skipBadKey(UT_JSONParser &p, const char *key);
134  /// Convenience method to add a fatal error about a bad key name
135  /// addError("Unexpected key token '%s'" % key);
136  void fatalBadKey(UT_JSONParser &p, const char *key);
137 };
138 
139 /// @brief This class generates errors on any JSON events.
140 ///
141 /// The UT_JSONHandleError class will throw errors on any JSON events. It's
142 /// useful as a base class for an event handler which only expects certain
143 /// events. For example, if you only expect the keyword argument, you might
144 /// have a class like: @code
145 /// class MyHandle : public UT_JSONHandleError
146 /// {
147 /// virtual bool jsonKey(UT_JSONParser &p, const char *v, int64 len);
148 /// virtual const char *className() { return "MyClass" }
149 /// }
150 /// @endcode
151 /// The base class would handle trapping errors on anything other than a
152 /// map key.
153 ///
154 /// @note Uniform arrays are handled by the UT_JSONHandle base class
155 ///
156 /// @see UT_JSONHandle, UT_JSONHandleNull
158 {
159 public:
160  /// @param fatal @n
161  /// If fatal is true then errors will cause parsing to terminate.
162  /// Otherwise, parsing will continue and errors will cascade.
163  UT_JSONHandleError(bool fatal=true) : myFatal(fatal) {}
164  ~UT_JSONHandleError() override {}
165 
166  UT_JSONHandleError(const UT_JSONHandleError &) = delete;
168 
169  /// getClassName() should return a meaningful label. The errors generated
170  /// will be:
171  /// Error parsing %s: Unexpected "type" JSON object
172  /// The default is an empty string.
173  virtual const char *getClassName() const;
174 
175  bool jsonNull(UT_JSONParser &p) override;
176  bool jsonBool(UT_JSONParser &p, bool value) override;
177  bool jsonInt(UT_JSONParser &p, int64 value) override;
178  bool jsonReal(UT_JSONParser &p, fpreal64 value) override;
179  bool jsonString(UT_JSONParser &p, const char *v, int64 len) override;
180  bool jsonKey(UT_JSONParser &p, const char *v, int64 len) override;
181  bool jsonBeginMap(UT_JSONParser &p) override;
182  bool jsonEndMap(UT_JSONParser &p) override;
183  bool jsonBeginArray(UT_JSONParser &p) override;
184  bool jsonEndArray(UT_JSONParser &p) override;
185 protected:
186  /// Add a type error for the given name
187  virtual bool typeError(UT_JSONParser &p, const char *name);
188  /// Check whether errors should be fatal (or just warnings)
189  bool getFatal() const { return myFatal; }
190  /// Set whether errors should be fatal (or just warnings)
191  void setFatal(bool f) { myFatal = f; }
192 
193 private:
194  /// Whether errors terminate parsing.
195  bool myFatal;
196 };
197 
198 /// @brief This class skips over any JSON events.
199 ///
200 /// The UT_JSONHandleNull will simply skip over any JSON events.
201 /// useful as a base class for an event handler which only expects certain
202 /// events. For example, if you only expect the keyword argument, you might
203 /// have a class like: @code
204 /// class MyHandle : public UT_JSONHandleNull
205 /// {
206 /// virtual bool jsonInt(UT_JSONParser &p, int64 len);
207 /// virtual bool jsonReal(UT_JSONParser &p, fpreal64 len);
208 /// virtual const char *className() { return "MyClass" }
209 /// }
210 /// @endcode
211 /// The base class would skip over anything other than an integer or real.
212 ///
213 /// @note Uniform arrays are handled by the UT_JSONHandle base class
214 ///
215 /// @see UT_JSONHandle, UT_JSONHandleError
217 {
218 public:
220  ~UT_JSONHandleNull() override {}
221 
222  UT_JSONHandleNull(const UT_JSONHandleNull &) = delete;
223  UT_JSONHandleNull &operator=(const UT_JSONHandleNull &) = delete;
224 
225  bool jsonNull(UT_JSONParser &p) override;
226  bool jsonBool(UT_JSONParser &p, bool value) override;
227  bool jsonInt(UT_JSONParser &p, int64 value) override;
228  bool jsonReal(UT_JSONParser &p, fpreal64 value) override;
229  bool jsonString(UT_JSONParser &p, const char *v, int64 len) override;
230  bool jsonKey(UT_JSONParser &p, const char *v, int64 len) override;
231  bool jsonBeginMap(UT_JSONParser &p) override;
232  bool jsonEndMap(UT_JSONParser &p) override;
233  bool jsonBeginArray(UT_JSONParser &p) override;
234  bool jsonEndArray(UT_JSONParser &p) override;
235 };
236 
237 
238 #endif
virtual bool jsonInt(UT_JSONParser &p, int64 value)=0
Event method to process an integer.
virtual ~UT_JSONHandle()
Definition: UT_JSONHandle.h:40
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
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.
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.