8#ifndef PXR_BASE_JS_JSON_H
9#define PXR_BASE_JS_JSON_H
15#include "pxr/base/js/api.h"
21PXR_NAMESPACE_OPEN_SCOPE
68 JS_API
JsWriter(std::ostream& ostr, Style style = Style::Compact);
106 bool WriteValue(
const char(&s)[N]) {
return _String(s, N-1); }
119 bool WriteKey(
const char(&s)[N]) {
return _Key(s, N-1); }
122 template<
class K,
class V>
124 _WriteObjectFields(std::forward<K>(k), std::forward<V>(v));
137 template <
class Container>
140 for (
const auto& i : c) {
148 template <
class Container,
class ItemWriteFn>
151 for (
const auto& i : c) {
159 template <
class Iterator,
class ItemWriteFn>
161 const Iterator& begin,
const Iterator& end,
const ItemWriteFn& f) {
163 for (Iterator i = begin; i != end; ++i) {
172 template<
class ...T>
174 static_assert(
sizeof...(T) %2 == 0,
175 "Arguments must come in key value pairs");
177 _WriteObjectFields(std::forward<T>(f)...);
186 JS_API
bool _String(
const char* s,
size_t len);
187 JS_API
bool _Key(
const char* s,
size_t len);
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));
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));
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)...);
210 std::unique_ptr<_Impl> _impl;
216PXR_NAMESPACE_CLOSE_SCOPE
A discriminated union type for JSON values.
This class provides an interface to writing json values directly to a stream.
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...
JS_API bool WriteValue(double d)
Write a double value.
void WriteObject(T &&... f)
Convenience function to write an object given key value pair arguments.
JS_API bool WriteValue(bool b)
Write a boolean value.
JS_API bool BeginArray()
Write the start of an array.
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 JsWriter(std::ostream &ostr, Style style=Style::Compact)
Constructor.
JS_API bool WriteValue(const std::string &s)
Write a string value.
JS_API bool WriteKey(const char *)
Write an object key.
JS_API bool EndObject()
Write the end of an object.
JS_API bool WriteValue(int64_t i)
Write a 64-bit integer value.
bool WriteKey(const char(&s)[N])
Write a string literal object key.
void WriteKeyValue(K &&k, V &&v)
Convenience function to write an object key and value.
void WriteArray(const Container &c)
Convenience function to write an array of values.
JS_API bool WriteKey(const std::string &)
Write an object key.
JS_API bool BeginObject()
Write the start of an object.
JS_API bool WriteValue(const char *s)
Write a string value.
JS_API bool WriteValue(std::nullptr_t)
Write a null value.
bool WriteValue(const char(&s)[N])
Write a string value.
JS_API ~JsWriter()
Destructor.
JS_API bool WriteValue(uint64_t u)
Write a 64-bit unsigned integer value.
JS_API bool WriteValue(unsigned u)
Write an unsigned integer value.
JsWriter(const JsWriter &)=delete
Disable copies.
JS_API bool EndArray()
Write the end of an array.
JS_API bool WriteValue(int i)
Write an integer value.
JS_API void JsWriteValue(JsWriter *writer, const JsValue &value)
Write a json value.
JS_API JsValue JsParseStream(std::istream &istr, JsParseError *error=0)
Parse the contents of input stream istr and return a JsValue.
JS_API std::string JsWriteToString(const JsValue &value)
Convert the JsValue value to JSON and return it as a string.
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 JsValue JsParseString(const std::string &data, JsParseError *error=0)
Parse the contents of the JSON string data and return it as a JsValue.
A struct containing information about a JSON parsing error.