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