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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef JS_JSON_H
26 #define JS_JSON_H
27 
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/js/api.h"
33 #include "pxr/base/js/value.h"
34 
35 #include <iosfwd>
36 #include <string>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
44 struct JsParseError {
45  JsParseError() : line(0), column(0) { }
46  unsigned int line;
47  unsigned int column;
48  std::string reason;
49 };
50 
53 JS_API
54 JsValue JsParseStream(std::istream& istr, JsParseError* error = 0);
55 
58 JS_API
59 JsValue JsParseString(const std::string& data, JsParseError* error = 0);
60 
63 JS_API
64 void JsWriteToStream(const JsValue& value, std::ostream& ostr);
65 
67 JS_API
68 std::string JsWriteToString(const JsValue& value);
69 
76 class JsWriter {
77 public:
78  enum class Style {
79  Compact,
80  Pretty
81  };
82 
85  JS_API JsWriter(std::ostream& ostr, Style style = Style::Compact);
86 
88  JS_API ~JsWriter();
89 
91  JsWriter(const JsWriter&) = delete;
92  JsWriter& operator=(const JsWriter&) = delete;
93 
95  JS_API bool WriteValue(std::nullptr_t);
96 
98  JS_API bool WriteValue(bool b);
99 
101  JS_API bool WriteValue(int i);
102 
104  JS_API bool WriteValue(unsigned u);
105 
107  JS_API bool WriteValue(int64_t i);
108 
110  JS_API bool WriteValue(uint64_t u);
111 
113  JS_API bool WriteValue(double d);
114 
116  JS_API bool WriteValue(const std::string& s);
117 
119  JS_API bool WriteValue(const char* s);
120 
122  template< size_t N>
123  bool WriteValue(const char(&s)[N]) { return _String(s, N-1); }
124 
126  JS_API bool BeginObject();
127 
129  JS_API bool WriteKey(const std::string&);
130 
132  JS_API bool WriteKey(const char*);
133 
135  template< size_t N>
136  bool WriteKey(const char(&s)[N]) { return _Key(s, N-1); }
137 
139  template<class K, class V>
140  void WriteKeyValue(K&& k, V&& v) {
141  _WriteObjectFields(std::forward<K>(k), std::forward<V>(v));
142  }
143 
145  JS_API bool EndObject();
146 
148  JS_API bool BeginArray();
149 
151  JS_API bool EndArray();
152 
154  template <class Container>
155  void WriteArray(const Container& c) {
156  BeginArray();
157  for (const auto& i : c) {
158  WriteValue(i);
159  }
160  EndArray();
161  }
162 
165  template <class Container, class ItemWriteFn>
166  void WriteArray(const Container& c, const ItemWriteFn& f) {
167  BeginArray();
168  for (const auto& i : c) {
169  f(*this, i);
170  }
171  EndArray();
172  }
173 
176  template <class Iterator, class ItemWriteFn>
178  const Iterator& begin, const Iterator& end, const ItemWriteFn& f) {
179  BeginArray();
180  for (Iterator i = begin; i != end; ++i) {
181  f(*this, i);
182  }
183  EndArray();
184  }
185 
189  template< class ...T>
190  void WriteObject(T&&... f) {
191  static_assert(sizeof...(T) %2 == 0,
192  "Arguments must come in key value pairs");
193  BeginObject();
194  _WriteObjectFields(std::forward<T>(f)...);
195  EndObject();
196  }
197 
198 private:
199  // Don't want implicit casts to write functions, its better to get an error.
200  template <class T>
201  bool WriteValue(T) = delete;
202 
203  JS_API bool _String(const char* s, size_t len);
204  JS_API bool _Key(const char* s, size_t len);
205 
206  template <class KeyType, class T>
207  auto _WriteObjectFields(KeyType&& key, T&& v)
208  -> decltype(WriteValue(std::forward<T>(v)), void()) {
209  WriteKey(std::forward<KeyType>(key));
210  WriteValue(std::forward<T>(v));
211  }
212 
213  template <class KeyType, class T>
214  auto _WriteObjectFields(KeyType&& key, T&& v)
215  -> decltype(v(std::declval<JsWriter&>()), void()) {
216  WriteKey(std::forward<KeyType>(key));
217  v(*this);
218  }
219 
220  template< class Key0, class T0, class ...T>
221  void _WriteObjectFields(Key0&& key0, T0&& f0, T&&...f){
222  _WriteObjectFields(std::forward<Key0>(key0), std::forward<T0>(f0));
223  _WriteObjectFields(std::forward<T>(f)...);
224  }
225 
226  class _Impl;
227  std::unique_ptr<_Impl> _impl;
228 };
229 
231 JS_API void JsWriteValue(JsWriter* writer, const JsValue& value);
232 
233 PXR_NAMESPACE_CLOSE_SCOPE
234 
235 #endif // JS_JSON_H
bool WriteKey(const char(&s)[N])
Write a string literal object key.
Definition: json.h:136
JS_API void JsWriteToStream(const JsValue &value, std::ostream &ostr)
Convert the JsValue value to JSON and write the result to output stream ostr.
JS_API JsWriter(std::ostream &ostr, Style style=Style::Compact)
Constructor.
A discriminated union type for JSON values.
Definition: value.h:61
JS_API bool BeginObject()
Write the start of an object.
void WriteArray(const Container &c, const ItemWriteFn &f)
Convenience function to write an array of values by calling the given functor for each item in the co...
Definition: json.h:166
void WriteObject(T &&...f)
Convenience function to write an object given key value pair arguments.
Definition: json.h:190
A struct containing information about a JSON parsing error.
Definition: json.h:44
bool WriteValue(const char(&s)[N])
Write a string value.
Definition: json.h:123
void WriteKeyValue(K &&k, V &&v)
Convenience function to write an object key and value.
Definition: json.h:140
JS_API void JsWriteValue(JsWriter *writer, const JsValue &value)
Write a json value.
JS_API ~JsWriter()
Destructor.
JS_API JsValue JsParseString(const std::string &data, JsParseError *error=0)
Parse the contents of the JSON string data and return it as a JsValue.
void WriteArray(const Container &c)
Convenience function to write an array of values.
Definition: json.h:155
JS_API bool WriteValue(std::nullptr_t)
Write a null value.
JS_API bool BeginArray()
Write the start of an array.
JS_API bool WriteKey(const std::string &)
Write an object key.
JS_API JsValue JsParseStream(std::istream &istr, JsParseError *error=0)
Parse the contents of input stream istr and return a JsValue.
JS_API bool EndArray()
Write the end of an array.
This class provides an interface to writing json values directly to a stream.
Definition: json.h:76
JS_API bool EndObject()
Write the end of an object.
void WriteArray(const Iterator &begin, const Iterator &end, const ItemWriteFn &f)
Convenience function to write an array of values given two iterators by calling the given functor for...
Definition: json.h:177
JS_API std::string JsWriteToString(const JsValue &value)
Convert the JsValue value to JSON and return it as a string.