rippled
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 <ripple/json/Output.h>
21 #include <ripple/json/Writer.h>
22 
23 namespace Json {
24 
25 namespace {
26 
27 void
28 outputJson(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 
89 void
90 outputJson(Json::Value const& value, Output const& out)
91 {
92  Writer writer(out);
93  outputJson(value, writer);
94 }
95 
98 {
99  std::string s;
100  Writer writer(stringOutput(s));
101  outputJson(value, writer);
102  return s;
103 }
104 
105 } // namespace Json
std::string
STL class.
Json::jsonAsString
std::string jsonAsString(Json::Value const &value)
Return the minimal string representation of a Json::Value in O(n) time.
Definition: Output.cpp:97
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
Json::booleanValue
@ booleanValue
bool value
Definition: json_value.h:41
Json::realValue
@ realValue
double value
Definition: json_value.h:39
std::function
Json::Value::asBool
bool asBool() const
Definition: json_value.cpp:619
Json::uintValue
@ uintValue
unsigned integer value
Definition: json_value.h:38
Json::outputJson
void outputJson(Json::Value const &value, Output const &out)
Writes a minimal representation of a Json value to an Output in O(n) time.
Definition: Output.cpp:90
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
Json::Writer::array
@ array
Definition: json/Writer.h:129
Json::stringValue
@ stringValue
UTF-8 string value.
Definition: json_value.h:40
Json::Writer::object
@ object
Definition: json/Writer.h:129
Json::intValue
@ intValue
signed integer value
Definition: json_value.h:37
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
Json::nullValue
@ nullValue
'null' value
Definition: json_value.h:36
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:503
Json::stringOutput
Output stringOutput(std::string &s)
Definition: json/Output.h:33
Json::Value::asDouble
double asDouble() const
Definition: json_value.cpp:587
Json::Value::type
ValueType type() const
Definition: json_value.cpp:350
Json::Writer
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition: json/Writer.h:126
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469