rippled
Loading...
Searching...
No Matches
json_writer.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_JSON_JSON_WRITER_H_INCLUDED
21#define RIPPLE_JSON_JSON_WRITER_H_INCLUDED
22
23#include <xrpl/json/json_forwards.h>
24#include <xrpl/json/json_value.h>
25
26#include <ostream>
27#include <vector>
28
29namespace Json {
30
31class Value;
32
36{
37public:
38 virtual ~WriterBase()
39 {
40 }
41 virtual std::string
42 write(Value const& root) = 0;
43};
44
53class FastWriter : public WriterBase
54{
55public:
56 FastWriter() = default;
57 virtual ~FastWriter()
58 {
59 }
60
61public: // overridden from Writer
63 write(Value const& root) override;
64
65private:
66 void
67 writeValue(Value const& value);
68
70};
71
91{
92public:
94 virtual ~StyledWriter()
95 {
96 }
97
98public: // overridden from Writer
104 write(Value const& root) override;
105
106private:
107 void
108 writeValue(Value const& value);
109 void
110 writeArrayValue(Value const& value);
111 bool
112 isMultineArray(Value const& value);
113 void
114 pushValue(std::string const& value);
115 void
116 writeIndent();
117 void
118 writeWithIndent(std::string const& value);
119 void
120 indent();
121 void
122 unindent();
123
125
132};
133
156{
157public:
158 StyledStreamWriter(std::string indentation = "\t");
160 {
161 }
162
163public:
170 void
171 write(std::ostream& out, Value const& root);
172
173private:
174 void
175 writeValue(Value const& value);
176 void
177 writeArrayValue(Value const& value);
178 bool
179 isMultineArray(Value const& value);
180 void
181 pushValue(std::string const& value);
182 void
183 writeIndent();
184 void
185 writeWithIndent(std::string const& value);
186 void
187 indent();
188 void
189 unindent();
190
192
199};
200
202valueToString(Int value);
204valueToString(UInt value);
206valueToString(double value);
208valueToString(bool value);
210valueToQuotedString(char const* value);
211
215operator<<(std::ostream&, Value const& root);
216
217//------------------------------------------------------------------------------
218
219// Helpers for stream
220namespace detail {
221
222template <class Write>
223void
224write_string(Write const& write, std::string const& s)
225{
226 write(s.data(), s.size());
227}
228
229template <class Write>
230void
231write_value(Write const& write, Value const& value)
232{
233 switch (value.type())
234 {
235 case nullValue:
236 write("null", 4);
237 break;
238
239 case intValue:
240 write_string(write, valueToString(value.asInt()));
241 break;
242
243 case uintValue:
244 write_string(write, valueToString(value.asUInt()));
245 break;
246
247 case realValue:
248 write_string(write, valueToString(value.asDouble()));
249 break;
250
251 case stringValue:
253 break;
254
255 case booleanValue:
256 write_string(write, valueToString(value.asBool()));
257 break;
258
259 case arrayValue: {
260 write("[", 1);
261 int const size = value.size();
262 for (int index = 0; index < size; ++index)
263 {
264 if (index > 0)
265 write(",", 1);
266 write_value(write, value[index]);
267 }
268 write("]", 1);
269 break;
270 }
271
272 case objectValue: {
273 Value::Members const members = value.getMemberNames();
274 write("{", 1);
275 for (auto it = members.begin(); it != members.end(); ++it)
276 {
277 std::string const& name = *it;
278 if (it != members.begin())
279 write(",", 1);
280
281 write_string(write, valueToQuotedString(name.c_str()));
282 write(":", 1);
283 write_value(write, value[name]);
284 }
285 write("}", 1);
286 break;
287 }
288 }
289}
290
291} // namespace detail
292
299template <class Write>
300void
301stream(Json::Value const& jv, Write const& write)
302{
303 detail::write_value(write, jv);
304 write("\n", 1);
305}
306
318{
320
321public:
330 Compact(Json::Value&& jv) : jv_{std::move(jv)}
331 {
332 }
333
335 operator<<(std::ostream& o, Compact const& cJv)
336 {
338 [&o](void const* data, std::size_t n) {
339 o.write(static_cast<char const*>(data), n);
340 },
341 cJv.jv_);
342 return o;
343 }
344};
345
346} // namespace Json
347
348#endif // JSON_WRITER_H_INCLUDED
T begin(T... args)
T c_str(T... args)
Decorator for streaming out compact json.
Compact(Json::Value &&jv)
Wrap a Json::Value for compact streaming.
friend std::ostream & operator<<(std::ostream &o, Compact const &cJv)
Json::Value jv_
Outputs a Value in JSON format without formatting (not human friendly).
Definition json_writer.h:54
virtual ~FastWriter()
Definition json_writer.h:57
std::string write(Value const &root) override
void writeValue(Value const &value)
FastWriter()=default
std::string document_
Definition json_writer.h:69
Writes a Value in JSON format in a human friendly way, to a stream rather than to a string.
void write(std::ostream &out, Value const &root)
Serialize a Value in JSON format.
void writeWithIndent(std::string const &value)
bool isMultineArray(Value const &value)
void writeValue(Value const &value)
void writeArrayValue(Value const &value)
std::ostream * document_
void pushValue(std::string const &value)
Writes a Value in JSON format in a human friendly way.
Definition json_writer.h:91
ChildValues childValues_
std::string write(Value const &root) override
Serialize a Value in JSON format.
virtual ~StyledWriter()
Definition json_writer.h:94
std::string indentString_
void pushValue(std::string const &value)
void writeValue(Value const &value)
void writeArrayValue(Value const &value)
std::string document_
void writeWithIndent(std::string const &value)
bool isMultineArray(Value const &value)
Represents a JSON value.
Definition json_value.h:149
UInt size() const
Number of values in array or object.
char const * asCString() const
Int asInt() const
UInt asUInt() const
Members getMemberNames() const
Return a list of the member names.
ValueType type() const
bool asBool() const
double asDouble() const
Abstract class for writers.
Definition json_writer.h:36
virtual std::string write(Value const &root)=0
virtual ~WriterBase()
Definition json_writer.h:38
T data(T... args)
T end(T... args)
void write_string(Write const &write, std::string const &s)
void write_value(Write const &write, Value const &value)
JSON (JavaScript Object Notation).
Definition json_errors.h:25
void stream(Json::Value const &jv, Write const &write)
Stream compact JSON to the specified function.
std::string valueToString(Int value)
std::string valueToQuotedString(char const *value)
@ booleanValue
bool value
Definition json_value.h:43
@ nullValue
'null' value
Definition json_value.h:38
@ stringValue
UTF-8 string value.
Definition json_value.h:42
@ realValue
double value
Definition json_value.h:41
@ arrayValue
array value (ordered list)
Definition json_value.h:44
@ intValue
signed integer value
Definition json_value.h:39
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
@ uintValue
unsigned integer value
Definition json_value.h:40
int Int
unsigned int UInt
std::ostream & operator<<(std::ostream &, Value const &root)
Output using the StyledStreamWriter.
STL namespace.
T size(T... args)
T write(T... args)