25 #ifndef PXR_BASE_JS_JSON_H
26 #define PXR_BASE_JS_JSON_H
32 #include "pxr/base/js/api.h"
38 PXR_NAMESPACE_OPEN_SCOPE
85 JS_API
JsWriter(std::ostream& ostr, Style style = Style::Compact);
92 JsWriter& operator=(
const JsWriter&) =
delete;
123 bool WriteValue(
const char(&s)[N]) {
return _String(s, N-1); }
129 JS_API
bool WriteKey(
const std::string&);
136 bool WriteKey(
const char(&s)[N]) {
return _Key(s, N-1); }
139 template<
class K,
class V>
141 _WriteObjectFields(std::forward<K>(k), std::forward<V>(v));
154 template <
class Container>
157 for (
const auto& i : c) {
165 template <
class Container,
class ItemWriteFn>
168 for (
const auto& i : c) {
176 template <
class Iterator,
class ItemWriteFn>
178 const Iterator& begin,
const Iterator& end,
const ItemWriteFn& f) {
180 for (Iterator i = begin; i != end; ++i) {
189 template<
class ...T>
191 static_assert(
sizeof...(T) %2 == 0,
192 "Arguments must come in key value pairs");
194 _WriteObjectFields(std::forward<T>(f)...);
203 JS_API
bool _String(
const char* s,
size_t len);
204 JS_API
bool _Key(
const char* s,
size_t len);
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));
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));
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)...);
227 std::unique_ptr<_Impl> _impl;
233 PXR_NAMESPACE_CLOSE_SCOPE
235 #endif // PXR_BASE_JS_JSON_H
bool WriteKey(const char(&s)[N])
Write a string literal object key.
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.
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...
void WriteObject(T &&...f)
Convenience function to write an object given key value pair arguments.
A struct containing information about a JSON parsing error.
bool WriteValue(const char(&s)[N])
Write a string value.
void WriteKeyValue(K &&k, V &&v)
Convenience function to write an object key and value.
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.
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.
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...
JS_API std::string JsWriteToString(const JsValue &value)
Convert the JsValue value to JSON and return it as a string.