rippled
Loading...
Searching...
No Matches
Output.cpp
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#include <xrpl/json/Output.h>
21#include <xrpl/json/Writer.h>
22
23namespace Json {
24
25namespace {
26
27void
28outputJson(Json::Value const& value, Writer& writer)
29{
30 switch (value.type())
31 {
32 case Json::nullValue: {
33 writer.output(nullptr);
34 break;
35 }
36
37 case Json::intValue: {
38 writer.output(value.asInt());
39 break;
40 }
41
42 case Json::uintValue: {
43 writer.output(value.asUInt());
44 break;
45 }
46
47 case Json::realValue: {
48 writer.output(value.asDouble());
49 break;
50 }
51
52 case Json::stringValue: {
53 writer.output(value.asString());
54 break;
55 }
56
57 case Json::booleanValue: {
58 writer.output(value.asBool());
59 break;
60 }
61
62 case Json::arrayValue: {
63 writer.startRoot(Writer::array);
64 for (auto const& i : value)
65 {
66 writer.rawAppend();
67 outputJson(i, writer);
68 }
69 writer.finish();
70 break;
71 }
72
73 case Json::objectValue: {
74 writer.startRoot(Writer::object);
75 auto members = value.getMemberNames();
76 for (auto const& tag : members)
77 {
78 writer.rawSet(tag);
79 outputJson(value[tag], writer);
80 }
81 writer.finish();
82 break;
83 }
84 } // switch
85}
86
87} // namespace
88
89void
90outputJson(Json::Value const& value, Output const& out)
91{
92 Writer writer(out);
93 outputJson(value, writer);
94}
95
98{
100 Writer writer(stringOutput(s));
101 outputJson(value, writer);
102 return s;
103}
104
105} // namespace Json
Represents a JSON value.
Definition: json_value.h:147
Int asInt() const
Definition: json_value.cpp:503
UInt asUInt() const
Definition: json_value.cpp:545
ValueType type() const
Definition: json_value.cpp:350
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469
bool asBool() const
Definition: json_value.cpp:619
double asDouble() const
Definition: json_value.cpp:587
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition: json/Writer.h:127
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
Output stringOutput(std::string &s)
std::string jsonAsString(Json::Value const &)
Return the minimal string representation of a Json::Value in O(n) time.
Definition: Output.cpp:97
@ booleanValue
bool value
Definition: json_value.h:41
@ nullValue
'null' value
Definition: json_value.h:36
@ stringValue
UTF-8 string value.
Definition: json_value.h:40
@ realValue
double value
Definition: json_value.h:39
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
@ intValue
signed integer value
Definition: json_value.h:37
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
@ uintValue
unsigned integer value
Definition: json_value.h:38
void outputJson(Json::Value const &, Output const &)
Writes a minimal representation of a Json value to an Output in O(n) time.
Definition: Output.cpp:90