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/ToString.h>
24 #include <ripple/basics/contract.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&
134  operator=(Writer&&) noexcept;
135 
136  ~Writer();
137 
140 
143 
145  void
146  startSet(CollectionType, std::string const& key);
147 
149  void
150  finish();
151 
154  void
155  finishAll();
156 
162  template <typename Scalar>
163  void
164  append(Scalar t)
165  {
166  rawAppend();
167  output(t);
168  }
169 
172  void
173  rawAppend();
174 
186  template <typename Type>
187  void
188  set(std::string const& tag, Type t)
189  {
190  rawSet(tag);
191  output(t);
192  }
193 
196  void
197  rawSet(std::string const& key);
198 
199  // You won't need to call anything below here until you are writing single
200  // items (numbers, strings, bools, null) to a JSON stream.
201 
202  /*** Output a string. */
203  void
204  output(std::string const&);
205 
206  /*** Output a literal constant or C string. */
207  void
208  output(char const*);
209 
210  /*** Output a Json::Value. */
211  void
212  output(Json::Value const&);
213 
215  void output(std::nullptr_t);
216 
218  void
219  output(float);
220 
222  void
223  output(double);
224 
226  void
227  output(bool);
228 
230  template <typename Type>
231  void
232  output(Type t)
233  {
235  }
236 
237  void
239  {
240  output(t.c_str());
241  }
242 
243 private:
244  class Impl;
246 
247  void
248  implOutput(std::string const&);
249 };
250 
251 inline void
252 check(bool condition, std::string const& message)
253 {
254  if (!condition)
255  ripple::Throw<std::logic_error>(message);
256 }
257 
258 } // namespace Json
259 
260 #endif
Json::Writer::rawAppend
void rawAppend()
Add a comma before this next item if not the first item in an array.
Definition: Writer.cpp:315
Json::Writer::implOutput
void implOutput(std::string const &)
Definition: Writer.cpp:302
Json::Writer::append
void append(Scalar t)
Append a value to an array.
Definition: json/Writer.h:164
std::string
STL class.
Json::Writer::finish
void finish()
Finish the collection most recently started.
Definition: Writer.cpp:351
Json::Writer::set
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
Definition: json/Writer.h:188
Json::check
void check(bool condition, std::string const &message)
Definition: json/Writer.h:252
Json::Writer::startAppend
void startAppend(CollectionType)
Start a new collection inside an array.
Definition: Writer.cpp:336
std::function
Json::StaticString::c_str
constexpr const char * c_str() const
Definition: json_value.h:73
Json::Writer::startRoot
void startRoot(CollectionType)
Start a new collection at the root level.
Definition: Writer.cpp:330
std::nullptr_t
Json::Writer::finishAll
void finishAll()
Finish all objects and arrays.
Definition: Writer.cpp:308
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:27
Json::Writer::output
void output(Json::StaticString const &t)
Definition: json/Writer.h:238
std::to_string
T to_string(T... args)
Json::Writer::impl_
std::unique_ptr< Impl > impl_
Definition: json/Writer.h:244
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:343
Json::Writer::Impl
Definition: Writer.cpp:75
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
std
STL namespace.
Json::Writer::rawSet
void rawSet(std::string const &key)
Emit just "tag": as part of an object.
Definition: Writer.cpp:321
Json::Writer::output
void output(Type t)
Output numbers or booleans.
Definition: json/Writer.h:232
Json::Writer::Writer
Writer(Output const &output)
Definition: Writer.cpp:235
Json::Writer::output
void output(std::string const &)
Definition: Writer.cpp:264
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:145