rippled
json/Writer.h
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 #ifndef RIPPLE_JSON_WRITER_H_INCLUDED
21 #define RIPPLE_JSON_WRITER_H_INCLUDED
22 
23 #include <ripple/basics/contract.h>
24 #include <ripple/basics/ToString.h>
25 #include <ripple/json/Output.h>
26 #include <ripple/json/json_value.h>
27 #include <memory>
28 
29 namespace Json {
30 
126 class Writer
127 {
128 public:
129  enum CollectionType {array, object};
130 
131  explicit Writer (Output const& output);
132  Writer(Writer&&) noexcept;
133  Writer& operator=(Writer&&) noexcept;
134 
135  ~Writer();
136 
138  void startRoot (CollectionType);
139 
142 
144  void startSet (CollectionType, std::string const& key);
145 
147  void finish ();
148 
151  void finishAll ();
152 
158  template <typename Scalar>
159  void append (Scalar t)
160  {
161  rawAppend();
162  output (t);
163  }
164 
167  void rawAppend();
168 
180  template <typename Type>
181  void set (std::string const& tag, Type t)
182  {
183  rawSet (tag);
184  output (t);
185  }
186 
189  void rawSet (std::string const& key);
190 
191  // You won't need to call anything below here until you are writing single
192  // items (numbers, strings, bools, null) to a JSON stream.
193 
194  /*** Output a string. */
195  void output (std::string const&);
196 
197  /*** Output a literal constant or C string. */
198  void output (char const*);
199 
200  /*** Output a Json::Value. */
201  void output (Json::Value const&);
202 
204  void output (std::nullptr_t);
205 
207  void output (float);
208 
210  void output (double);
211 
213  void output (bool);
214 
216  template <typename Type>
217  void output (Type t)
218  {
220  }
221 
222  void output (Json::StaticString const& t)
223  {
224  output (t.c_str());
225  }
226 
227 private:
228  class Impl;
230 
231  void implOutput (std::string const&);
232 };
233 
234 inline void check (bool condition, std::string const& message)
235 {
236  if (! condition)
237  ripple::Throw<std::logic_error> (message);
238 }
239 
240 } // Json
241 
242 #endif
Json::Writer::rawAppend
void rawAppend()
Add a comma before this next item if not the first item in an array.
Definition: Writer.cpp:285
Json::Writer::implOutput
void implOutput(std::string const &)
Definition: Writer.cpp:274
Json::Writer::append
void append(Scalar t)
Append a value to an array.
Definition: json/Writer.h:159
std::string
STL class.
Json::Writer::finish
void finish()
Finish the collection most recently started.
Definition: Writer.cpp:316
Json::Writer::set
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
Definition: json/Writer.h:181
Json::check
void check(bool condition, std::string const &message)
Definition: json/Writer.h:234
Json::Writer::startAppend
void startAppend(CollectionType)
Start a new collection inside an array.
Definition: Writer.cpp:303
std::function
Json::StaticString::c_str
constexpr const char * c_str() const
Definition: json_value.h:75
Json::Writer::startRoot
void startRoot(CollectionType)
Start a new collection at the root level.
Definition: Writer.cpp:298
std::nullptr_t
Json::Writer::finishAll
void finishAll()
Finish all objects and arrays.
Definition: Writer.cpp:279
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:26
Json::Writer::output
void output(Json::StaticString const &t)
Definition: json/Writer.h:222
std::to_string
T to_string(T... args)
Json::Writer::impl_
std::unique_ptr< Impl > impl_
Definition: json/Writer.h:228
Json::Writer::array
@ array
Definition: json/Writer.h:129
memory
Json::Writer::startSet
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition: Writer.cpp:309
Json::Writer::Impl
Definition: Writer.cpp:75
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:62
std
STL namespace.
Json::Writer::rawSet
void rawSet(std::string const &key)
Emit just "tag": as part of an object.
Definition: Writer.cpp:290
Json::Writer::output
void output(Type t)
Output numbers or booleans.
Definition: json/Writer.h:217
Json::Writer::Writer
Writer(Output const &output)
Definition: Writer.cpp:214
Json::Writer::output
void output(std::string const &)
Definition: Writer.cpp:241
std::unique_ptr
STL class.
Json::Writer::CollectionType
CollectionType
Definition: json/Writer.h:129
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