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#include <memory>
28
29namespace Json {
30
127{
128public:
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
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
243private:
244 class Impl;
246
247 void
248 implOutput(std::string const&);
249};
250
251inline void
252check(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
Lightweight wrapper to tag static string.
Definition: json_value.h:61
constexpr const char * c_str() const
Definition: json_value.h:74
Represents a JSON value.
Definition: json_value.h:147
Writer implements an O(1)-space, O(1)-granular output JSON writer.
Definition: json/Writer.h:127
void finish()
Finish the collection most recently started.
Definition: Writer.cpp:350
void output(std::string const &)
Definition: Writer.cpp:262
void implOutput(std::string const &)
Definition: Writer.cpp:301
void startRoot(CollectionType)
Start a new collection at the root level.
Definition: Writer.cpp:329
void output(Type t)
Output numbers or booleans.
Definition: json/Writer.h:232
void append(Scalar t)
Append a value to an array.
Definition: json/Writer.h:164
void rawAppend()
Add a comma before this next item if not the first item in an array.
Definition: Writer.cpp:314
void rawSet(std::string const &key)
Emit just "tag": as part of an object.
Definition: Writer.cpp:320
void finishAll()
Finish all objects and arrays.
Definition: Writer.cpp:307
void startAppend(CollectionType)
Start a new collection inside an array.
Definition: Writer.cpp:335
void set(std::string const &tag, Type t)
Add a key, value assignment to an object.
Definition: json/Writer.h:188
void startSet(CollectionType, std::string const &key)
Start a new collection inside an object.
Definition: Writer.cpp:342
void output(Json::StaticString const &t)
Definition: json/Writer.h:238
std::unique_ptr< Impl > impl_
Definition: json/Writer.h:245
JSON (JavaScript Object Notation).
Definition: json_errors.h:25
void check(bool condition, std::string const &message)
Definition: json/Writer.h:252
STL namespace.
T to_string(T... args)