HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_JSONWriter.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_JSONWriter.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_JSONWriter__
12 #define __UT_JSONWriter__
13 
14 #include "UT_API.h"
15 #include "UT_Compression.h"
16 #include "UT_IntArray.h"
17 #include "UT_JSONDefines.h"
18 #include "UT_NonCopyable.h"
19 #include "UT_Set.h"
20 #include "UT_StringMap.h"
21 #include "UT_WorkBuffer.h"
22 #include <ostream>
23 #include <streambuf>
24 #include <string>
25 
26 class UT_Options;
27 class UT_BitArray;
28 class UT_JSONValue;
29 
30 /// @brief Class which writes ASCII or binary JSON streams
31 ///
32 /// The JSON format (http://www.json.org) is a generic way to store data.
33 /// Houdini extends JSON to a byte-stream binary encoding (see UT_JID for
34 /// details). The UT_JSONWriter class is used to write JSON (binary or
35 /// ASCII) to a stream.
36 /// @see UT_JID, UT_JSONValue, UT_AutoJSONWriter
38 {
39 public:
40  /// @param options @n
41  /// A UT_Options class which sets specific format options.
42  /// @see setOptions()
43  UT_JSONWriter(const UT_Options *options=0);
44  virtual ~UT_JSONWriter();
45 
46  UT_JSONWriter(const UT_JSONWriter &) = delete;
47  UT_JSONWriter &operator=(const UT_JSONWriter &) = delete;
48 
49  /// Allocate a JSON Writer which will fill a UT_WorkBuffer.
50  /// Please delete when finished with the writer.
51  static UT_JSONWriter *allocWriter(UT_WorkBuffer &buffer);
52 
53  /// Allocate a JSON Writer which writes to an output stream.
54  /// Please delete when finished with the writer.
55  static UT_JSONWriter *allocWriter(
56  std::ostream &os,
57  bool binary,
59 
60  /// Allocate a JSON Writer which writes to a named file.
61  /// Please delete when finished with the writer.
62  static UT_JSONWriter *allocWriter(
63  const char *filename,
64  bool binary,
66 
67  /// Allocate a JSON Writer which writes into a given UT_JSONValue. The
68  /// value will only be set properly at the conclusion of writing.
69  /// Please delete when finished with the writer.
70  static UT_JSONWriter *allocWriter(UT_JSONValue &value);
71 
72  /// Set formatting/control options base on the UT_Options. The following
73  /// options are scanned:
74  /// - @b int @b "json:precision" -- ASCII float precision [9].
75  /// Note half-precision floats will use half this precision, while double
76  /// precision will use twice the digits.
77  /// - @b int @b "json:indentstep" -- ASCII indent step for maps/arrays [8]
78  /// - @b int @b "json:textwidth" -- ASCII wrap lines after byte count [256]
79  /// - @b bool @b "json:comments" -- Allow comments in ASCII stream [false]
80  /// - @b bool @b "json:prettyprint" -- Pretty print ASCII stream [true]
81  /// - @b bool @b "json:usetokens" -- Binary string token compression [true]
82  /// - @b bool @b "json:prettycompact" -- Add a space after separators, a
83  /// more compact pretty print matching Python [false]
84  void setOptions(const UT_Options &options);
85 
86  /// Get the current options. This will set the token/values in
87  /// the UT_Options based on options from setOptions()
88  void getOptions(UT_Options &options) const;
89 
90  /// Direct access to options.
91  void setPrecision(int precision) { myPrecision = SYSmax(1, precision); }
92  int getPrecision() const { return myPrecision; }
93  void setIndentStep(int indentstep) { myIndentStep = indentstep; }
94  int getIndentStep() const { return myIndentStep; }
95  void setUseTokens(bool usetokens) { myTokenStrings = usetokens; }
96  bool getUseTokens() const { return myTokenStrings; }
97  void setTextWidth(int width) { myTextWidth = width; }
98  int getTextWidth() const { return myTextWidth; }
99  void setComments(bool comment) { myComments = comment; }
100  bool getComments() const { return myComments; }
101  void setPrettyPrint(bool prettyprint) { myPrettyPrint = prettyprint; }
102  bool getPrettyPrint() const { return myPrettyPrint; }
103  void setPrettyCompact(bool compact) { myPrettyCompact = compact; }
104  bool getPrettyCompact() const { return myPrettyCompact; }
105 
106  /// Turn the JSON writer into a binary JSON (bJSON) writer.
107  /// @warning This method must be called before any other data is written to
108  /// the JSON file. It's a @b good idea to call in the sub-class
109  /// constructor.
110  bool setBinary(bool bin);
111 
112  /// Return whether writing binary or ASCII JSON
113  bool getBinary() const { return myBinary; }
114 
115  /// Get the number of bytes written to output so far.
116  int64 getNumBytesWritten() const { return myCurrentFilePosition; }
117 
118  /// Start a new embedded file
119  void startNewFile();
120 
121  /// End a started embedded file
122  void endNewFile();
123 
124  /// Output a comment in the JSON stream. Not all JSON parsers (actually
125  /// very few) support comments. The json:comments must be enabled for this
126  /// statement to have any effect.
127  bool jsonComment(const char *format, ...)
129 
130  // Standard JSON types
131  /// Write a @b null to the stream
132  bool jsonNull();
133  /// Write true/false
134  bool jsonBool(bool value);
135  /// Write an integer value
136  bool jsonInt(int32 value);
137  /// Write a 64 bit integer value
138  bool jsonInt(int64 value);
139  /// Write a 64 bit integer as a 64 bit integer
140  bool jsonInt64(int64 value);
141  /// Write a 16 bit real
142  bool jsonReal(fpreal16 value);
143  bool jsonRealPreserveType(fpreal16 value);
144  /// Write a 32 bit real
145  bool jsonReal(fpreal32 value);
146  bool jsonRealPreserveType(fpreal32 value);
147  /// Write a 64 bit real
148  bool jsonReal(fpreal64 value);
149  bool jsonRealPreserveType(fpreal64 value);
150  /// Write a quoted string.
151  /// @warning Please use jsonKey() to write key strings.
152  /// @note Use jsonStringToken() to take advantage of token compression
153  /// (this can be turned off with the @c usetokens option in the
154  /// constructor).
155  /// @see UT_JID
156  bool jsonString(const char *value, int64 length=0);
157 
158  /// For binary streams, common strings can be output more efficiently using
159  /// the token interface. Token strings will appear as strings in ASCII
160  /// streams.
161  bool jsonStringToken(const UT_StringRef &value);
162  bool jsonStringToken(const char *value, int64 length)
163  {
165  if (length <= 0)
166  result.reference(value);
167  else
168  result.fastReferenceWithStrlen(value, length);
169  return jsonStringToken(result);
170  }
171 
172  /// @{
173  /// Write a standard JSON value to the stream. Overloaded method to allow
174  /// convenient use in templated code.
175  bool jsonValue(bool value) { return jsonBool(value); }
176  bool jsonValue(int8 value) { return jsonInt((int32)value); }
177  bool jsonValue(int16 value) { return jsonInt((int32)value); }
178  bool jsonValue(int32 value) { return jsonInt(value); }
179  bool jsonValue(unsigned value) { return jsonInt((int32)value); }
180  bool jsonValue(int64 value) { return jsonInt(value); }
181  bool jsonValue(fpreal16 value) { return jsonReal(value); }
182  bool jsonValue(fpreal32 value) { return jsonReal(value); }
183  bool jsonValue(fpreal64 value) { return jsonReal(value); }
184  bool jsonValue(const char *v) { return jsonStringToken(v); }
185  bool jsonValue(const UT_String &v)
186  { return jsonStringToken(v.c_str(), v.length()); }
187  bool jsonValue(const std::string &v)
188  { return jsonStringToken(v.c_str(), v.length()); }
189  bool jsonValue(const UT_StringRef &v) { return jsonStringToken(v); }
190  bool jsonValue(const UT_Int16Array &v)
191  { return jsonUniformArray(v); }
192  bool jsonValue(const UT_Int32Array &v)
193  { return jsonUniformArray(v); }
194  bool jsonValue(const UT_Int64Array &v)
195  { return jsonUniformArray(v); }
197  { return jsonUniformArray(v); }
199  { return jsonUniformArray(v); }
201  { return jsonStringTokenArray(v); }
202  bool jsonValue(const UT_JSONValue &v);
203  template <typename T>
204  bool jsonValue(const UT_Set<T>& v)
205  {
206  return jsonSetAsArray(v);
207  }
208  /// @}
209 
210  /// Begin a map/object dictionary
211  bool jsonBeginMap();
212  /// Write a quoted key for the object
213  /// @note Use jsonKeyToken() to take advantage of token compression
214  /// (this can be turned off with the @c usetokens option in the
215  /// constructor).
216  /// @see UT_JID
217  bool jsonKey(const char *value, int64 length=0);
218  /// For binary streams, common key strings can be output more efficiently
219  /// using the token interface. Token key strings will appear as strings in
220  /// ASCII streams.
221  bool jsonKeyToken(const UT_StringRef &value);
222  bool jsonKeyToken(const char *value, int64 length)
223  {
225  if (length <= 0)
226  result.reference(value);
227  else
228  result.fastReferenceWithStrlen(value, length);
229  return jsonKeyToken(result);
230  }
231 
232  /// @{
233  /// Convenience method to write the key/value pair to a map
234  template <typename T> SYS_FORCE_INLINE bool
235  jsonKeyValue(const UT_StringRef &key, const T &value)
236  {
237  bool ok = jsonKeyToken(key);
238  return ok && jsonValue(value);
239  }
240 
241  template <typename T> SYS_FORCE_INLINE bool
242  jsonKeyTokenValue(const UT_StringRef &key, const T &value)
243  {
244  bool ok = jsonKeyToken(key);
245  return ok && jsonValue(value);
246  }
247  /// @}
248 
249  /// End the object
250  bool jsonEndMap();
251 
252  /// Begin a generic array object
253  bool jsonBeginArray();
254  /// End a generic array object
255  /// By default, a new line will be output for pretty printing. The newline
256  /// can give a hint to omit the new line before the end of the array
257  /// marker.
258  bool jsonEndArray(bool newline=true);
259 
260  /// When writing binary JSON files, uniform arrays can be encoded without
261  /// repetition of the type information which results in a more compressed
262  /// data format.
263  ///
264  /// @c beingUniformArray() starts a uniform array.
265  /// The @c id should be one of:
266  /// - UT_JID_BOOL
267  /// - UT_JID_INT8
268  /// - UT_JID_INT16
269  /// - UT_JID_INT32
270  /// - UT_JID_INT64
271  /// - UT_JID_UINT8
272  /// - UT_JID_UINT16
273  /// - UT_JID_REAL16
274  /// - UT_JID_REAL32
275  /// - UT_JID_REAL64
276  /// - UT_JID_STRING
277  bool beginUniformArray(int64 length, UT_JID id);
278  /// Write a bool value to a uniform array. The bool values are packed into
279  /// a bit array (where each 32-bit word stores 32 bits).
280  bool uniformWrite(bool value);
281  /// Write an 8 bit integer value to the uniform array
282  bool uniformWrite(int8 value);
283  /// Write an 16 bit integer value to the uniform array
284  bool uniformWrite(int16 value);
285  /// Write an 32 bit integer value to the uniform array
286  bool uniformWrite(int32 value);
287  /// Write an 64 bit integer value to the uniform array
288  bool uniformWrite(int64 value);
289  /// Write an 16 bit float/real value to the uniform array
290  bool uniformWrite(fpreal16 value);
291  /// Write an 32 bit float/real value to the uniform array
292  bool uniformWrite(fpreal32 value);
293  /// Write an 64 bit float/real value to the uniform array
294  bool uniformWrite(fpreal64 value);
295  /// Write an 8 bit unsigned integer value to the uniform array
296  bool uniformWrite(uint8 value);
297  /// Write an 16 bit unsigned integer value to the uniform array
298  bool uniformWrite(uint16 value);
299  /// Write a string to the uniform array
300  bool uniformWrite(const char *value, int64 length=0);
301  /// Write a block of 8 bit integer values to the uniform array
302  bool uniformBlockWrite(const int8 *value, int64 count);
303  /// Write a block of 16 bit integer values to the uniform array
304  bool uniformBlockWrite(const int16 *value, int64 count);
305  /// Write a block of 32 bit integer values to the uniform array
306  bool uniformBlockWrite(const int32 *value, int64 count);
307  /// Write a block of 64 bit integer values to the uniform array
308  bool uniformBlockWrite(const int64 *value, int64 count);
309  /// Write a block of 16 bit float/real values to the uniform array
310  bool uniformBlockWrite(const fpreal16 *value, int64 count);
311  /// Write a block of 32 bit float/real values to the uniform array
312  bool uniformBlockWrite(const fpreal32 *value, int64 count);
313  /// Write a block of 64 bit float/real values to the uniform array
314  bool uniformBlockWrite(const fpreal64 *value, int64 count);
315  /// Write a block of 8 bit unsigned integer values to the uniform array
316  bool uniformBlockWrite(const uint8 *value, int64 count);
317  /// Write a block of 16 bit unsigned integer values to the uniform array
318  bool uniformBlockWrite(const uint16 *value, int64 count);
319  /// End the uniform array.
320  /// @param nwritten @n
321  /// If provided, the number of items written is returned.
322  /// @note The JSONWriter class will automatically clamp or fill the uniform
323  /// array with 0, but nwritten will return the actual number of calls to
324  /// uniformWrite().
325  bool endUniformArray(int64 *nwritten=0);
326 
327  /// Internally called to write raw data to output.
328  virtual bool writeData(const void *data, int64 bytes);
329 
330  /// Returns the JID that matches the given type.
331  static UT_JID jidFromValue(const bool *) { return UT_JID_BOOL; }
332  static UT_JID jidFromValue(const int8 *) { return UT_JID_INT8; }
333  static UT_JID jidFromValue(const int16 *) { return UT_JID_INT16; }
334  static UT_JID jidFromValue(const int32 *) { return UT_JID_INT32; }
335  static UT_JID jidFromValue(const int64 *) { return UT_JID_INT64; }
336  static UT_JID jidFromValue(const fpreal16 *) { return UT_JID_REAL16; }
337  static UT_JID jidFromValue(const fpreal32 *) { return UT_JID_REAL32; }
338  static UT_JID jidFromValue(const fpreal64 *) { return UT_JID_REAL64; }
339  static UT_JID jidFromValue(const uint8 *) { return UT_JID_UINT8; }
340  static UT_JID jidFromValue(const uint16 *) { return UT_JID_UINT16; }
341 
342  /// Efficent method of writing a uniform array of int8 values
343  bool jsonUniformArray(int64 length, const int8 *value);
344  /// Efficent method of writing a uniform array of int16 values
345  bool jsonUniformArray(int64 length, const int16 *value);
346  /// Efficent method of writing a uniform array of int32 values
347  bool jsonUniformArray(int64 length, const int32 *value);
348  /// Efficent method of writing a uniform array of int64 values
349  bool jsonUniformArray(int64 length, const int64 *value);
350  /// Efficent method of writing a uniform array of fpreal16 values
351  bool jsonUniformArray(int64 length, const fpreal16 *value);
352  /// Efficent method of writing a uniform array of fpreal32 values
353  bool jsonUniformArray(int64 length, const fpreal32 *value);
354  /// Efficent method of writing a uniform array of fpreal64 values
355  bool jsonUniformArray(int64 length, const fpreal64 *value);
356 
357  /// Efficent method of writing a uniform array of uint8 values
358  bool jsonUniformArray(int64 length, const uint8 *value);
359  /// Efficent method of writing a uniform array of uint16 values
360  bool jsonUniformArray(int64 length, const uint16 *value);
361 
362  /// Efficient method of writing a uniform array of bool values
363  bool jsonUniformArray(int64 length, const UT_BitArray &value,
364  bool verbose_ascii = false);
365 
366  /// @{
367  /// Convenience method for saving common array types
369  { return jsonUniformArray(a.size(), a.data()); }
371  { return jsonUniformArray(a.size(), a.data()); }
373  { return jsonUniformArray(a.size(), a.data()); }
375  { return jsonUniformArray(a.size(), a.data()); }
377  { return jsonUniformArray(a.size(), a.data()); }
378  /// @}
379 
380  /// Convenience method to save a set as an array of strings
381  template <typename T>
383  {
384  if (!jsonBeginArray())
385  return false;
386 
387  for (auto&& v : a)
388  jsonValue(v);
389 
390  return jsonEndArray();
391  }
392 
393  /// Convenience method to save an array of strings
394  bool jsonStringArray(const UT_StringArray &a);
395  /// Convenience method to save an array of strings (as tokens)
396  bool jsonStringTokenArray(const UT_StringArray &a);
397 
398  /// A TiledStream allows you to write a blob of binary data to a JSON
399  /// stream in an efficient manner. The data is buffered into uniform
400  /// arrays which are output into a standard JSON array. The resulting data
401  /// looks something like: @code
402  /// [ {options} tile1-data tile2-data ]
403  /// @endcode
404  /// The tile-data is written as JSON string objects. You can read these
405  /// yourself, or use the TiledStream in UT_JSONParser.
406  ///
407  /// You should never write to the JSONWriter while the TiledStream is open.
408  ///
409  /// An example of using this might be something like: @code
410  /// bool storeStream(UT_IStream &is)
411  /// {
412  /// UT_JSONWriter::TiledStream os(writer);
413  /// char buffer[128];
414  /// int nread;
415  /// while ((nread = is.bread(buffer, 128)) > 0)
416  /// if (!os.write(buffer, nread)) return false;
417  /// return true;
418  /// }
419  /// @endcode
420  class UT_API TiledStreamBuf : public std::streambuf
421  {
422  public:
424  ~TiledStreamBuf() override;
425 
426  TiledStreamBuf(const TiledStreamBuf &) = delete;
427  TiledStreamBuf &operator=(const TiledStreamBuf &) = delete;
428 
429  protected:
430  // write one character
431  int_type overflow(int_type c) override;
432 
433  // write multiple characters
434  std::streamsize xsputn(const char* s, std::streamsize num) override;
435 
436  private:
437  bool write(const void *data, exint bytes);
438  void close();
439 
440  bool writeTile(const char *data, exint bytes);
441  bool flush();
442  UT_JSONWriter &myWriter;
443  char *myBuffer;
444  exint myLength;
445  };
446 
447  class UT_API TiledStream : public std::ostream
448  {
449  public:
451  ~TiledStream() override;
452 
454 
455  private:
456  TiledStreamBuf myBuf;
457  };
458 
459 protected:
460  /// The sub-class must implement this method to write data
461  virtual bool subclassWriteData(const void* data, int64 bytes) = 0;
462 
463 protected:
464  /// Get the nesting level (i.e. how deeply nested in maps/arrays)
465  int getNesting() const { return myStack.entries(); }
466 private:
467  int precision16() const { return (myPrecision+1)/2; }
468  int precision32() const { return myPrecision; }
469  int precision64() const { return myPrecision*2; }
470  /// @private: ASCII indent
471  bool indent();
472  /// @private: ASCII prefix
473  bool prefix();
474  /// @private: Convenience method to write a UT_JID
475  bool writeId(UT_JID id);
476  /// @private: Convenience method to write an encoded length @see UT_JID
477  bool writeLength(int64 length);
478  /// @private: Write a token string
479  bool writeTokenString(const UT_StringRef &str);
480  /// @private: Write a string of data (not quoted)
481  bool writeString(const char *str, int64 bytes=0);
482  /// @private: Write a quoted string
483  bool quotedString(const char *str, int64 bytes=0);
484  /// @private: Write an ASCII "bool" value
485  bool writeAB(bool i);
486  /// @private: Write an ASCII "integer" value
487  bool writeAI(int64 i);
488  /// @private: Write an ASCII "float" value
489  bool writeAF(fpreal64 i, int precision);
490  /// @private: Write an ASCII "float" value and ensure it has a decimal point
491  bool writeAFPreserveType(fpreal64, int precision);
492  /// @private: Start an array (possibly uniform encoding)
493  bool startArray(UT_JID id, UT_JID type=UT_JID_NULL);
494  /// @private: Finish array
495  bool finishArray(UT_JID id, bool newline=true);
496  /// @private: Flush bits for a uniform array of bools
497  bool flushBits();
498  /// @private: Push nesting of map/array
499  void pushState(UT_JID id);
500  /// @private: Pop state
501  void popState(UT_JID id);
502  /// @private: Set the prefix for ASCII encoding
503  void setPrefix();
504  /// @private: Build indent string
505  void bumpIndent(int dir);
506  /// @private: New line & indent
507  bool newLine(bool force=false);
508 
509  /// Method to detail with long lines in ASCII files
510  bool countAndWrite(const void *data, int64 bytes);
511 
512  template <typename T> bool
513  uniformArrayI(UT_JID id, int64 length, const T *data);
514  template <typename T> bool
515  uniformArrayF(UT_JID id, int64 length, const T *data);
516  template <typename T> bool
517  uniformBlockWriteImplI(const T *data, int64 count);
518  template <typename T> bool
519  uniformBlockWriteImplF(const T *data, int64 count, int prec);
520 
521  enum
522  {
523  PREFIX_FIRST, // First element (no prefix)
524  PREFIX_ARRAY, // Use array prefix
525  PREFIX_MAP, // Use object prefix
526  };
527 
528  UT_IntArray myStack;
529  UT_WorkBuffer myIndentString;
530  // Note - carefully test performance if you are switching this to a
531  // different hash table like UT_ArrayStringMap (see bug 122906)
532  UT_StringMap<int64> myTokens;
533  UT_Int64Array myTokenCountStack;
534  int64 myCurrentFilePosition;
535  int64 myUWriteSize; // Uniform write size
536  int64 myUWriteCount; // Uniform entities written
537  int64 myTokenCount;
538  UT_JID myUniform; // Type of data for fixed array
539  uint32 myBitValue; // For packing bit arrays
540  int myBitCount;
541  int myPrefix;
542 
543  bool myBinary; // ASCII or binary
544 
545  // Options
546  int myPrecision; // ASCII float precision
547  int myIndentStep; // ASCII indent step
548  int myTextWidth; // ASCII max line length
549  bool myTokenStrings; // Use token strings
550  bool myComments; // Emit comments
551  bool myPrettyPrint; // Pretty print
552  bool myPrettyCompact; // Python style, spaces after sepators, no newlines.
553 
554  // State required for formatting.
555  bool myNewLine; // Pending newline, for line wrapping
556  bool myJoinNext;
557  int myIndent; // ASCII indent level
558  int myLineLength; // ASCII line length
559 };
560 
561 /// Convenience class to allocate a writer which is automatically deleted
562 /// For example: @code
563 /// bool save(UT_JSONWriter &w) { ... } // Method to save to standard writer
564 ///
565 /// bool save(UT_WorkBuffer &buffer) // Save to a UT_WorkBuffer
566 /// {
567 /// UT_AutoJSONWriter w(buffer);
568 /// return save(w);
569 /// }
570 /// bool save(UT_JSONValue &value) // Save to a UT_JSONValue
571 /// {
572 /// UT_AutoJSONWriter w(value);
573 /// return save(w);
574 /// }
575 /// @endcode
577 {
578 public:
579  /// Write directly to the UT_WorkBuffer
581  : myWriter(UT_JSONWriter::allocWriter(b))
582  {
583  }
584  /// Write to the given ostream (binary and optionally gzipped)
585  UT_AutoJSONWriter(std::ostream &os,
586  bool binary,
587  UT_CompressionType compressionType = UT_COMPRESSION_TYPE_NONE )
588  : myWriter(UT_JSONWriter::allocWriter(os, binary, compressionType))
589  {
590  }
591  /// Write to the given filename (binary and optionally gzipped)
592  UT_AutoJSONWriter(const char *filename,
593  bool binary,
595  : myWriter(UT_JSONWriter::allocWriter(filename, binary, compressionType))
596  {
597  }
598  /// Write directly to the UT_JSONValue
600  : myWriter(UT_JSONWriter::allocWriter(value))
601  {
602  }
604  {
605  delete myWriter;
606  }
607 
608  UT_AutoJSONWriter(const UT_AutoJSONWriter &) = delete;
609  UT_AutoJSONWriter &operator=(const UT_AutoJSONWriter &) = delete;
610 
611  /// @{
612  /// Access to the writer
613  UT_JSONWriter &writer() const { return *myWriter; }
614  UT_JSONWriter &operator*() const { return *myWriter; }
615  UT_JSONWriter *operator->() const { return myWriter; }
616  /// @}
617 
618  /// Allow implicit casting between an auto-writer and the underlying writer
619  operator UT_JSONWriter &() { return *myWriter; }
620 
621  /// Close the stream (causing all data to be flushed)
622  void close()
623  {
624  delete myWriter;
625  myWriter = NULL;
626  }
627 private:
628  UT_JSONWriter *myWriter;
629 };
630 
631 #endif
UT_JSONWriter & operator*() const
The following byte represents an 8 bit integer.
#define SYSmax(a, b)
Definition: SYS_Math.h:1538
bool jsonValue(bool value)
UT_AutoJSONWriter(const char *filename, bool binary, UT_CompressionType compressionType=UT_COMPRESSION_TYPE_NONE)
Write to the given filename (binary and optionally gzipped)
static UT_JID jidFromValue(const fpreal32 *)
unsigned short uint16
Definition: SYS_Types.h:38
static UT_JID jidFromValue(const int64 *)
Definition: UT_Set.h:58
No data follows the NULL token.
GT_API const UT_StringHolder filename
int64 getNumBytesWritten() const
Get the number of bytes written to output so far.
UT_AutoJSONWriter(UT_WorkBuffer &b)
Write directly to the UT_WorkBuffer.
int int32
Definition: SYS_Types.h:39
The following 4 bytes represent an 32 bit real (float)
bool jsonKeyToken(const char *value, int64 length)
UT_JID
The UT_JID enums are used in byte-stream encoding of binary JSON.
bool getUseTokens() const
Definition: UT_JSONWriter.h:96
static UT_JID jidFromValue(const bool *)
Returns the JID that matches the given type.
bool getBinary() const
Return whether writing binary or ASCII JSON.
void writeData(std::ostream &os, const T *data, Index count, uint32_t compression)
Definition: Compression.h:363
const GLdouble * v
Definition: glcorearb.h:837
bool jsonValue(int32 value)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
0x23 and 0x24 are reserved for future use (32/64 bit unsigned)
const GLuint GLenum const void * binary
Definition: glcorearb.h:1924
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
The following byte represents an unsigned 8 bit integer.
#define UT_API
Definition: UT_API.h:14
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
**But if you need a result
Definition: thread.h:613
bool jsonValue(fpreal16 value)
void close() override
UT_JSONWriter & writer() const
bool jsonValue(const UT_String &v)
float fpreal32
Definition: SYS_Types.h:200
bool jsonUniformArray(const UT_Int32Array &a)
exint size() const
Definition: UT_Array.h:646
const char * c_str() const
Definition: UT_String.h:508
bool jsonValue(const char *v)
unsigned length() const
Return length of string.
Definition: UT_String.h:546
bool getComments() const
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
void setPrettyPrint(bool prettyprint)
The following 8 bytes represent an 64 bit real (float)
The following 8 bytes represent an 64 bit integer.
bool jsonValue(const UT_Fpreal32Array &v)
bool jsonValue(const UT_StringRef &v)
Definition: core.h:760
The following 2 bytes represent an 16 bit integer.
static UT_JID jidFromValue(const fpreal64 *)
SYS_FORCE_INLINE bool jsonKeyTokenValue(const UT_StringRef &key, const T &value)
void writeString(std::ostream &os, const Name &name)
Definition: Name.h:33
UT_AutoJSONWriter(UT_JSONValue &value)
Write directly to the UT_JSONValue.
static UT_JID jidFromValue(const fpreal16 *)
#define SYS_PRINTF_CHECK_ATTRIBUTE(string_index, first_to_check)
Definition: SYS_Types.h:447
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
bool getPrettyCompact() const
bool jsonValue(int16 value)
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
bool jsonValue(unsigned value)
signed char int8
Definition: SYS_Types.h:35
The following 4 bytes represent an 32 bit integer.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
int getPrecision() const
Definition: UT_JSONWriter.h:92
void setPrettyCompact(bool compact)
void fastReferenceWithStrlen(const char *src, exint length)
old name of method:
bool jsonUniformArray(const UT_Int16Array &a)
bool jsonValue(const UT_Int16Array &v)
bool jsonUniformArray(const UT_Int64Array &a)
GLenum GLint GLint * precision
Definition: glcorearb.h:1925
void setComments(bool comment)
Definition: UT_JSONWriter.h:99
void setIndentStep(int indentstep)
Definition: UT_JSONWriter.h:93
bool jsonUniformArray(const UT_Fpreal32Array &a)
A map of string to various well defined value types.
Definition: UT_Options.h:84
static UT_JID jidFromValue(const int8 *)
SIM_API const UT_StringHolder force
static UT_JID jidFromValue(const uint16 *)
bool jsonValue(const std::string &v)
short int16
Definition: SYS_Types.h:37
UT_CompressionType
T * data()
Definition: UT_Array.h:822
bool jsonSetAsArray(const UT_Set< T > &a)
Convenience method to save a set as an array of strings.
SYS_FORCE_INLINE bool jsonKeyValue(const UT_StringRef &key, const T &value)
static UT_JID jidFromValue(const uint8 *)
bool jsonValue(int8 value)
bool jsonValue(const UT_Int32Array &v)
UT_AutoJSONWriter(std::ostream &os, bool binary, UT_CompressionType compressionType=UT_COMPRESSION_TYPE_NONE)
Write to the given ostream (binary and optionally gzipped)
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
unsigned int uint32
Definition: SYS_Types.h:40
UT_JSONWriter * operator->() const
void reference(const char *src)
GLint GLsizei width
Definition: glcorearb.h:103
bool jsonValue(int64 value)
int getNesting() const
Get the nesting level (i.e. how deeply nested in maps/arrays)
static UT_JID jidFromValue(const int32 *)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
int getIndentStep() const
Definition: UT_JSONWriter.h:94
Definition: core.h:1131
bool jsonUniformArray(const UT_Fpreal64Array &a)
#define const
Definition: zconf.h:214
bool jsonValue(const UT_Fpreal64Array &v)
void setTextWidth(int width)
Definition: UT_JSONWriter.h:97
bool jsonValue(fpreal64 value)
void write(T &out, bool v)
Definition: ImfXdr.h:287
The following 2 bytes represents an unsigned 16 bit integer.
void close()
Close the stream (causing all data to be flushed)
bool jsonValue(fpreal32 value)
type
Definition: core.h:1059
static UT_JID jidFromValue(const int16 *)
bool getPrettyPrint() const
void setUseTokens(bool usetokens)
Definition: UT_JSONWriter.h:95
bool jsonValue(const UT_Set< T > &v)
bool jsonValue(const UT_StringArray &v)
GLint GLsizei count
Definition: glcorearb.h:405
Definition: format.h:895
Definition: format.h:2459
int getTextWidth() const
Definition: UT_JSONWriter.h:98
void setPrecision(int precision)
Direct access to options.
Definition: UT_JSONWriter.h:91
bool jsonValue(const UT_Int64Array &v)