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