HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
json.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_JS_JSON_H
9 #define PXR_BASE_JS_JSON_H
10 
11 /// \file js/json.h
12 /// Top-level entrypoints for reading and writing JSON.
13 
14 #include "pxr/pxr.h"
15 #include "pxr/base/js/api.h"
16 #include "pxr/base/js/value.h"
17 
18 #include <iosfwd>
19 #include <string>
20 
22 
23 /// \struct JsParseError
24 ///
25 /// A struct containing information about a JSON parsing error.
26 ///
27 struct JsParseError {
28  JsParseError() : line(0), column(0) { }
29  unsigned int line;
30  unsigned int column;
31  std::string reason;
32 };
33 
34 /// Parse the contents of input stream \p istr and return a JsValue. On
35 /// failure, this returns a null JsValue.
36 JS_API
37 JsValue JsParseStream(std::istream& istr, JsParseError* error = 0);
38 
39 /// Parse the contents of the JSON string \p data and return it as a JsValue.
40 /// On failure, this returns a null JsValue.
41 JS_API
42 JsValue JsParseString(const std::string& data, JsParseError* error = 0);
43 
44 /// Convert the JsValue \p value to JSON and write the result to output stream
45 /// \p ostr.
46 JS_API
47 void JsWriteToStream(const JsValue& value, std::ostream& ostr);
48 
49 /// Convert the JsValue \p value to JSON and return it as a string.
50 JS_API
51 std::string JsWriteToString(const JsValue& value);
52 
53 /// \class JsWriter
54 ///
55 /// This class provides an interface to writing json values directly to a
56 /// stream. This can be much more efficient than constructing a JsValue instance
57 /// and using JsWriteToStream if the data size is significant.
58 ///
59 class JsWriter {
60 public:
61  enum class Style {
62  Compact,
63  Pretty
64  };
65 
66  /// Constructor. The lifetime of the /p ostr parameter is assumed to be
67  /// longer than the JsWriter instance.
68  JS_API JsWriter(std::ostream& ostr, Style style = Style::Compact);
69 
70  /// Destructor.
71  JS_API ~JsWriter();
72 
73  /// Disable copies.
74  JsWriter(const JsWriter&) = delete;
75  JsWriter& operator=(const JsWriter&) = delete;
76 
77  /// Write a null value.
78  JS_API bool WriteValue(std::nullptr_t);
79 
80  /// Write a boolean value.
81  JS_API bool WriteValue(bool b);
82 
83  /// Write an integer value.
84  JS_API bool WriteValue(int i);
85 
86  /// Write an unsigned integer value.
87  JS_API bool WriteValue(unsigned u);
88 
89  /// Write a 64-bit integer value.
90  JS_API bool WriteValue(int64_t i);
91 
92  /// Write a 64-bit unsigned integer value.
93  JS_API bool WriteValue(uint64_t u);
94 
95  /// Write a double value.
96  JS_API bool WriteValue(double d);
97 
98  /// Write a string value.
99  JS_API bool WriteValue(const std::string& s);
100 
101  /// Write a string value.
102  JS_API bool WriteValue(const char* s);
103 
104  /// Write a string value.
105  template< size_t N>
106  bool WriteValue(const char(&s)[N]) { return _String(s, N-1); }
107 
108  /// Write the start of an object.
109  JS_API bool BeginObject();
110 
111  /// Write an object key.
112  JS_API bool WriteKey(const std::string&);
113 
114  /// Write an object key.
115  JS_API bool WriteKey(const char*);
116 
117  /// Write a string literal object key.
118  template< size_t N>
119  bool WriteKey(const char(&s)[N]) { return _Key(s, N-1); }
120 
121  /// Convenience function to write an object key and value.
122  template<class K, class V>
123  void WriteKeyValue(K&& k, V&& v) {
124  _WriteObjectFields(std::forward<K>(k), std::forward<V>(v));
125  }
126 
127  /// Write the end of an object.
128  JS_API bool EndObject();
129 
130  /// Write the start of an array.
131  JS_API bool BeginArray();
132 
133  /// Write the end of an array.
134  JS_API bool EndArray();
135 
136  /// Convenience function to write an array of values.
137  template <class Container>
138  void WriteArray(const Container& c) {
139  BeginArray();
140  for (const auto& i : c) {
141  WriteValue(i);
142  }
143  EndArray();
144  }
145 
146  /// Convenience function to write an array of values by calling the given
147  /// functor for each item in the container.
148  template <class Container, class ItemWriteFn>
149  void WriteArray(const Container& c, const ItemWriteFn& f) {
150  BeginArray();
151  for (const auto& i : c) {
152  f(*this, i);
153  }
154  EndArray();
155  }
156 
157  /// Convenience function to write an array of values given two iterators by
158  /// calling the given functor for each item in the container.
159  template <class Iterator, class ItemWriteFn>
161  const Iterator& begin, const Iterator& end, const ItemWriteFn& f) {
162  BeginArray();
163  for (Iterator i = begin; i != end; ++i) {
164  f(*this, i);
165  }
166  EndArray();
167  }
168 
169  /// Convenience function to write an object given key value pair arguments.
170  /// key arguments must be convertable to strings, value argruments must be
171  /// either a writable type, or a callablable type taking a JsWriter&.
172  template< class ...T>
173  void WriteObject(T&&... f) {
174  static_assert(sizeof...(T) %2 == 0,
175  "Arguments must come in key value pairs");
176  BeginObject();
177  _WriteObjectFields(std::forward<T>(f)...);
178  EndObject();
179  }
180 
181 private:
182  // Don't want implicit casts to write functions, its better to get an error.
183  template <class T>
184  bool WriteValue(T) = delete;
185 
186  JS_API bool _String(const char* s, size_t len);
187  JS_API bool _Key(const char* s, size_t len);
188 
189  template <class KeyType, class T>
190  auto _WriteObjectFields(KeyType&& key, T&& v)
191  -> decltype(WriteValue(std::forward<T>(v)), void()) {
192  WriteKey(std::forward<KeyType>(key));
193  WriteValue(std::forward<T>(v));
194  }
195 
196  template <class KeyType, class T>
197  auto _WriteObjectFields(KeyType&& key, T&& v)
198  -> decltype(v(std::declval<JsWriter&>()), void()) {
199  WriteKey(std::forward<KeyType>(key));
200  v(*this);
201  }
202 
203  template< class Key0, class T0, class ...T>
204  void _WriteObjectFields(Key0&& key0, T0&& f0, T&&...f){
205  _WriteObjectFields(std::forward<Key0>(key0), std::forward<T0>(f0));
206  _WriteObjectFields(std::forward<T>(f)...);
207  }
208 
209  class _Impl;
210  std::unique_ptr<_Impl> _impl;
211 };
212 
213 /// Write a json value.
214 JS_API void JsWriteValue(JsWriter* writer, const JsValue& value);
215 
217 
218 #endif // PXR_BASE_JS_JSON_H
JS_API void JsWriteToStream(const JsValue &value, std::ostream &ostr)
const GLdouble * v
Definition: glcorearb.h:837
JsParseError()
Definition: json.h:28
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLdouble s
Definition: glad.h:3009
JS_API bool WriteValue(std::nullptr_t)
Write a null value.
void WriteObject(T &&...f)
Definition: json.h:173
< returns > If no error
Definition: snippets.dox:2
JS_API bool BeginArray()
Write the start of an array.
GLfloat f
Definition: glcorearb.h:1926
JsWriter & operator=(const JsWriter &)=delete
Definition: value.h:44
void WriteKeyValue(K &&k, V &&v)
Convenience function to write an object key and value.
Definition: json.h:123
#define JS_API
Definition: api.h:23
void WriteArray(const Container &c)
Convenience function to write an array of values.
Definition: json.h:138
GLuint GLuint end
Definition: glcorearb.h:475
JS_API bool EndObject()
Write the end of an object.
JS_API JsWriter(std::ostream &ostr, Style style=Style::Compact)
JS_API bool BeginObject()
Write the start of an object.
JS_API ~JsWriter()
Destructor.
bool WriteKey(const char(&s)[N])
Write a string literal object key.
Definition: json.h:119
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
bool WriteValue(const char(&s)[N])
Write a string value.
Definition: json.h:106
JS_API bool WriteKey(const std::string &)
Write an object key.
JS_API bool EndArray()
Write the end of an array.
void WriteArray(const Container &c, const ItemWriteFn &f)
Definition: json.h:149
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
void WriteArray(const Iterator &begin, const Iterator &end, const ItemWriteFn &f)
Definition: json.h:160
unsigned int line
Definition: json.h:29
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
JS_API void JsWriteValue(JsWriter *writer, const JsValue &value)
Write a json value.
GA_API const UT_StringHolder N
JS_API JsValue JsParseString(const std::string &data, JsParseError *error=0)
JS_API JsValue JsParseStream(std::istream &istr, JsParseError *error=0)
std::string reason
Definition: json.h:31
unsigned int column
Definition: json.h:30
GLenum GLenum GLsizei void GLsizei void * column
Definition: glad.h:5135
Style
Definition: json.h:61
Definition: json.h:59
Definition: format.h:1821
JS_API std::string JsWriteToString(const JsValue &value)
Convert the JsValue value to JSON and return it as a string.
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:566