13 #ifndef __STOUT_JSONIFY__ 14 #define __STOUT_JSONIFY__ 16 #define RAPIDJSON_HAS_STDSTRING 1 17 #define RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNanAndInfFlag 37 #include <rapidjson/stringbuffer.h> 38 #include <rapidjson/writer.h> 44 #include <type_traits> 70 namespace JSON {
class Proxy; }
89 operator std::string() &&
91 rapidjson::StringBuffer buffer;
92 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
96 return {buffer.GetString(), buffer.GetSize()};
100 Proxy(std::function<
void(rapidjson::Writer<rapidjson::StringBuffer>*)>
write)
116 template <
typename T>
117 friend Proxy (::jsonify)(
const T&);
124 std::function<void(rapidjson::Writer<rapidjson::StringBuffer>*)>
write;
130 return stream << std::string(std::move(that));
141 : writer_(writer), value_(false) {}
151 void set(
bool value) { value_ = value; }
154 rapidjson::Writer<rapidjson::StringBuffer>* writer_;
165 : writer_(writer), type_(INT), int_(0) {}
173 case INT: CHECK(writer_->Int64(int_));
break;
174 case UINT: CHECK(writer_->Uint64(uint_));
break;
175 case DOUBLE: CHECK(writer_->Double(double_));
break;
197 void set(
short int value) {
set(
static_cast<long long int>(value)); }
199 void set(
int value) {
set(
static_cast<long long int>(value)); }
201 void set(
long int value) {
set(
static_cast<long long int>(value)); }
203 void set(
long long int value)
209 void set(
unsigned short int value)
211 set(
static_cast<unsigned long long int>(value));
214 void set(
unsigned int value)
216 set(
static_cast<unsigned long long int>(value));
219 void set(
unsigned long int value)
221 set(
static_cast<unsigned long long int>(value));
224 void set(
unsigned long long int value)
230 void set(
float value) {
set(
static_cast<double>(value)); }
232 void set(
double value)
239 rapidjson::Writer<rapidjson::StringBuffer>* writer_;
241 enum { INT, UINT, DOUBLE } type_;
259 : writer_(writer), empty_(true) {}
269 template <std::
size_t N>
270 void set(
const char (&value)[N])
276 CHECK(writer_->String(value, N-1));
279 void set(
const std::string& value)
285 CHECK(writer_->String(value));
289 rapidjson::Writer<rapidjson::StringBuffer>* writer_;
302 CHECK(writer_->StartArray());
310 CHECK(writer_->EndArray());
316 template <
typename T>
320 rapidjson::Writer<rapidjson::StringBuffer>* writer_;
332 CHECK(writer_->StartObject());
340 CHECK(writer_->EndObject());
346 template <
typename T>
347 void field(
const std::string& key,
const T& value)
355 CHECK(writer_->Key(key.c_str(), key.size()));
360 rapidjson::Writer<rapidjson::StringBuffer>* writer_;
367 NullWriter(rapidjson::Writer<rapidjson::StringBuffer>* writer)
379 rapidjson::Writer<rapidjson::StringBuffer>* writer_;
429 template <std::
size_t N>
449 template <
typename T>
463 template <
typename U>
466 void(std::begin(std::declval<U&>()) != std::end(std::declval<U&>())),
467 ++std::declval<decltype(std::begin(std::declval<U&>()))&>(),
468 *std::begin(std::declval<U&>()),
472 template <
typename U>
476 static constexpr
bool value = decltype(test<T>(
Prefer()))::value;
483 template <
typename T>
487 template <
typename U,
typename =
typename U::mapped_type>
490 template <
typename U>
494 static constexpr
bool value = decltype(test<T>(
Prefer()))::value;
506 typename std::enable_if<
508 !(std::is_array<Iterable>::value &&
509 std::rank<Iterable>::value == 1 &&
515 foreach (
const auto& value, iterable) {
527 typename std::enable_if<
532 foreachpair (
const auto& key,
const auto& value, dictionary) {
534 writer->
field(key, value);
561 : writer_(writer), type_(NULL_WRITER) {}
566 case BOOLEAN_WRITER: {
567 proxy_.boolean_writer.~BooleanWriter();
570 case NUMBER_WRITER: {
571 proxy_.number_writer.~NumberWriter();
574 case STRING_WRITER: {
575 proxy_.string_writer.~StringWriter();
579 proxy_.array_writer.~ArrayWriter();
582 case OBJECT_WRITER: {
583 proxy_.object_writer.~ObjectWriter();
587 proxy_.null_writer.~NullWriter();
596 type_ = BOOLEAN_WRITER;
597 return &proxy_.boolean_writer;
603 type_ = NUMBER_WRITER;
604 return &proxy_.number_writer;
610 type_ = STRING_WRITER;
611 return &proxy_.string_writer;
617 type_ = ARRAY_WRITER;
618 return &proxy_.array_writer;
624 type_ = OBJECT_WRITER;
625 return &proxy_.object_writer;
630 new (&proxy_.null_writer)
NullWriter(writer_);
632 return &proxy_.null_writer;
658 rapidjson::Writer<rapidjson::StringBuffer>* writer_;
674 std::function<void(rapidjson::Writer<rapidjson::StringBuffer>*)>
jsonify(
678 return [&
write](rapidjson::Writer<rapidjson::StringBuffer>* writer) {
688 template <
typename T>
689 std::function<void(rapidjson::Writer<rapidjson::StringBuffer>*)>
jsonify(
693 return [&value](rapidjson::Writer<rapidjson::StringBuffer>* writer) {
701 template <
typename T>
707 #endif // __STOUT_JSONIFY__ StringWriter(rapidjson::Writer< rapidjson::StringBuffer > *writer)
Definition: jsonify.hpp:258
void set(short int value)
Definition: jsonify.hpp:197
~BooleanWriter()
Definition: jsonify.hpp:146
double double_
Definition: jsonify.hpp:247
WriterProxy(rapidjson::Writer< rapidjson::StringBuffer > *writer)
Definition: jsonify.hpp:560
Definition: jsonify.hpp:364
Definition: jsonify.hpp:137
Definition: jsonify.hpp:484
BooleanWriter(rapidjson::Writer< rapidjson::StringBuffer > *writer)
Definition: jsonify.hpp:140
friend std::ostream & operator<<(std::ostream &stream, Proxy &&that)
Definition: jsonify.hpp:128
Definition: jsonify.hpp:255
void element(const T &value)
Definition: jsonify.hpp:317
friend Proxy() jsonify(const T &)
Definition: jsonify.hpp:702
void json(BooleanWriter *writer, const Boolean &boolean)
Definition: json.hpp:735
~ArrayWriter()
Definition: jsonify.hpp:308
NullWriter(rapidjson::Writer< rapidjson::StringBuffer > *writer)
Definition: jsonify.hpp:367
void field(const std::string &key, const T &value)
Definition: jsonify.hpp:347
std::function< void(rapidjson::Writer< rapidjson::StringBuffer > *)> write
Definition: jsonify.hpp:124
Definition: jsonify.hpp:86
Definition: jsonify.hpp:446
Definition: jsonify.hpp:450
Definition: jsonify.hpp:326
#define foreachpair(KEY, VALUE, ELEMS)
Definition: foreach.hpp:51
ArrayWriter(rapidjson::Writer< rapidjson::StringBuffer > *writer)
Definition: jsonify.hpp:299
~WriterProxy()
Definition: jsonify.hpp:563
Definition: jsonify.hpp:161
JSON::Proxy jsonify(const T &)
Definition: jsonify.hpp:702
~NumberWriter()
Definition: jsonify.hpp:170
std::function< void(rapidjson::Writer< rapidjson::StringBuffer > *)> jsonify(const F &write, Prefer)
Definition: jsonify.hpp:674
long long int int_
Definition: jsonify.hpp:245
Definition: attributes.hpp:24
~StringWriter()
Definition: jsonify.hpp:264
~ObjectWriter()
Definition: jsonify.hpp:338
Try< uint32_t > type(const std::string &path)
ObjectWriter(rapidjson::Writer< rapidjson::StringBuffer > *writer)
Definition: jsonify.hpp:329
Definition: jsonify.hpp:445
unsigned long long int uint_
Definition: jsonify.hpp:246
NumberWriter(rapidjson::Writer< rapidjson::StringBuffer > *writer)
Definition: jsonify.hpp:164
Result< bool > test(const std::string &_link, unsigned int flags)
Definition: internal.hpp:111
void set(bool value)
Definition: jsonify.hpp:151
~NullWriter()
Definition: jsonify.hpp:373
Definition: jsonify.hpp:557
Definition: jsonify.hpp:296
void set(const char(&value)[N])
Definition: jsonify.hpp:270