rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/ToString.h>
24#include <xrpl/basics/contract.h>
25#include <xrpl/json/Output.h>
26#include <xrpl/json/json_value.h>
27
28#include <memory>
29
30namespace Json {
31
128{
129public:
130 enum CollectionType { array, object };
131
132 explicit Writer(Output const& output);
133 Writer(Writer&&) noexcept;
134 Writer&
135 operator=(Writer&&) noexcept;
136
137 ~Writer();
138
141
144
146 void
147 startSet(CollectionType, std::string const& key);
148
150 void
151 finish();
152
155 void
156 finishAll();
157
163 template <typename Scalar>
164 void
165 append(Scalar t)
166 {
167 rawAppend();
168 output(t);
169 }
170
173 void
174 rawAppend();
175
187 template <typename Type>
188 void
189 set(std::string const& tag, Type t)
190 {
191 rawSet(tag);
192 output(t);
193 }
194
197 void
198 rawSet(std::string const& key);
199
200 // You won't need to call anything below here until you are writing single
201 // items (numbers, strings, bools, null) to a JSON stream.
202
203 /*** Output a string. */
204 void
205 output(std::string const&);
206
207 /*** Output a literal constant or C string. */
208 void
209 output(char const*);
210
211 /*** Output a Json::Value. */
212 void
213 output(Json::Value const&);
214
217
219 void
220 output(float);
221
223 void
224 output(double);
225
227 void
228 output(bool);
229
231 template <typename Type>
232 void
233 output(Type t)
234 {
236 }
237
238 void
240 {
241 output(t.c_str());
242 }
243
244private:
245 class Impl;
247
248 void
249 implOutput(std::string const&);
250};
251
252inline void
253check(bool condition, std::string const& message)
254{
255 if (!condition)
256 ripple::Throw<std::logic_error>(message);
257}
258
259} // namespace Json
260
261#endif
Lightweight wrapper to tag static string.
Definition json_value.h:63
constexpr char const * c_str() const
Definition json_value.h:76
Represents a JSON value.
Definition json_value.h:149
Writer implements an O(1)-space, O(1)-granular output JSON writer.
void finish()
Finish the collection most recently started.
Definition Writer.cpp:358
void output(std::string const &)
Definition Writer.cpp:270
void implOutput(std::string const &)
Definition Writer.cpp:309
void startRoot(CollectionType)
Start a new collection at the root level.
Definition Writer.cpp:337
void output(Type t)
Output numbers or booleans.
void append(Scalar t)
Append a value to an array.
void rawAppend()
Add a comma before this next item if not the first item in an array.
Definition Writer.cpp:322
void rawSet(std::string const &key)
Emit just "tag": as part of an object.
Definition Writer.cpp:328
void finishAll()
Finish all objects and arrays.
Definition Writer.cpp:315
void startAppend(CollectionType)
Start a new collection inside an array.
Definition Writer.cpp:343
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition Writer.cpp:350
void output(Json::StaticString const &t)
std::unique_ptr< Impl > impl_
JSON (JavaScript Object Notation).
Definition json_errors.h:25
void check(bool condition, std::string const &message)
STL namespace.
T to_string(T... args)