rippled
Loading...
Searching...
No Matches
libxrpl/json/Output.cpp
1#include <xrpl/json/Output.h>
2#include <xrpl/json/Writer.h>
3#include <xrpl/json/json_value.h>
4
5#include <string>
6
7namespace Json {
8
9namespace {
10
11void
12outputJson(Json::Value const& value, Writer& writer)
13{
14 switch (value.type())
15 {
16 case Json::nullValue: {
17 writer.output(nullptr);
18 break;
19 }
20
21 case Json::intValue: {
22 writer.output(value.asInt());
23 break;
24 }
25
26 case Json::uintValue: {
27 writer.output(value.asUInt());
28 break;
29 }
30
31 case Json::realValue: {
32 writer.output(value.asDouble());
33 break;
34 }
35
36 case Json::stringValue: {
37 writer.output(value.asString());
38 break;
39 }
40
41 case Json::booleanValue: {
42 writer.output(value.asBool());
43 break;
44 }
45
46 case Json::arrayValue: {
47 writer.startRoot(Writer::array);
48 for (auto const& i : value)
49 {
50 writer.rawAppend();
51 outputJson(i, writer);
52 }
53 writer.finish();
54 break;
55 }
56
57 case Json::objectValue: {
58 writer.startRoot(Writer::object);
59 auto members = value.getMemberNames();
60 for (auto const& tag : members)
61 {
62 writer.rawSet(tag);
63 outputJson(value[tag], writer);
64 }
65 writer.finish();
66 break;
67 }
68 } // switch
69}
70
71} // namespace
72
73void
74outputJson(Json::Value const& value, Output const& out)
75{
76 Writer writer(out);
77 outputJson(value, writer);
78}
79
82{
84 Writer writer(stringOutput(s));
85 outputJson(value, writer);
86 return s;
87}
88
89} // namespace Json
Represents a JSON value.
Definition json_value.h:130
Int asInt() const
UInt asUInt() const
Members getMemberNames() const
Return a list of the member names.
ValueType type() const
std::string asString() const
Returns the unquoted string value.
bool asBool() const
double asDouble() const
Writer implements an O(1)-space, O(1)-granular output JSON writer.
JSON (JavaScript Object Notation).
Definition json_errors.h:6
Output stringOutput(std::string &s)
std::string jsonAsString(Json::Value const &)
Return the minimal string representation of a Json::Value in O(n) time.
@ booleanValue
bool value
Definition json_value.h:24
@ nullValue
'null' value
Definition json_value.h:19
@ stringValue
UTF-8 string value.
Definition json_value.h:23
@ realValue
double value
Definition json_value.h:22
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ intValue
signed integer value
Definition json_value.h:20
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
@ uintValue
unsigned integer value
Definition json_value.h:21
void outputJson(Json::Value const &, Output const &)
Writes a minimal representation of a Json value to an Output in O(n) time.