All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
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
21PXR_NAMESPACE_OPEN_SCOPE
22
28 JsParseError() : line(0), column(0) { }
29 unsigned int line;
30 unsigned int column;
31 std::string reason;
32};
33
36JS_API
37JsValue JsParseStream(std::istream& istr, JsParseError* error = 0);
38
41JS_API
42JsValue JsParseString(const std::string& data, JsParseError* error = 0);
43
46JS_API
47void JsWriteToStream(const JsValue& value, std::ostream& ostr);
48
50JS_API
51std::string JsWriteToString(const JsValue& value);
52
59class JsWriter {
60public:
61 enum class Style {
62 Compact,
63 Pretty
64 };
65
68 JS_API JsWriter(std::ostream& ostr, Style style = Style::Compact);
69
71 JS_API ~JsWriter();
72
74 JsWriter(const JsWriter&) = delete;
75 JsWriter& operator=(const JsWriter&) = delete;
76
78 JS_API bool WriteValue(std::nullptr_t);
79
81 JS_API bool WriteValue(bool b);
82
84 JS_API bool WriteValue(int i);
85
87 JS_API bool WriteValue(unsigned u);
88
90 JS_API bool WriteValue(int64_t i);
91
93 JS_API bool WriteValue(uint64_t u);
94
96 JS_API bool WriteValue(double d);
97
99 JS_API bool WriteValue(const std::string& s);
100
102 JS_API bool WriteValue(const char* s);
103
105 template< size_t N>
106 bool WriteValue(const char(&s)[N]) { return _String(s, N-1); }
107
109 JS_API bool BeginObject();
110
112 JS_API bool WriteKey(const std::string&);
113
115 JS_API bool WriteKey(const char*);
116
118 template< size_t N>
119 bool WriteKey(const char(&s)[N]) { return _Key(s, N-1); }
120
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
128 JS_API bool EndObject();
129
131 JS_API bool BeginArray();
132
134 JS_API bool EndArray();
135
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
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
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
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
181private:
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
214JS_API void JsWriteValue(JsWriter* writer, const JsValue& value);
215
216PXR_NAMESPACE_CLOSE_SCOPE
217
218#endif // PXR_BASE_JS_JSON_H
A discriminated union type for JSON values.
Definition: value.h:45
This class provides an interface to writing json values directly to a stream.
Definition: json.h:59
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:149
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.
Definition: json.h:173
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...
Definition: json.h:160
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.
Definition: json.h:119
void WriteKeyValue(K &&k, V &&v)
Convenience function to write an object key and value.
Definition: json.h:123
void WriteArray(const Container &c)
Convenience function to write an array of values.
Definition: json.h:138
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.
Definition: json.h:106
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.
Definition: json.h:27