| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#pragma once |
| 37 |
|
| 38 |
#include "serialization.h" |
| 39 |
#include <cassert> |
| 40 |
#include <iostream> |
| 41 |
#include <iomanip> |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
template <class Stream, bool IsSaving> |
| 50 |
struct json_archive_base |
| 51 |
{ |
| 52 |
typedef Stream stream_type; |
| 53 |
typedef json_archive_base<Stream, IsSaving> base_type; |
| 54 |
typedef boost::mpl::bool_<IsSaving> is_saving; |
| 55 |
|
| 56 |
typedef const char *variant_tag_type; |
| 57 |
|
| 58 |
json_archive_base(stream_type &s, bool indent = false) |
| 59 |
: stream_(s), indent_(indent), object_begin(false), depth_(0) { } |
| 60 |
|
| 61 |
bool good() const { return stream_.good(); } |
| 62 |
void set_fail() { stream_.setstate(std::ios::failbit); } |
| 63 |
void clear_fail() { stream_.clear(); } |
| 64 |
|
| 65 |
void tag(const char *tag) { |
| 66 |
if (!object_begin) |
| 67 |
stream_ << ", "; |
| 68 |
make_indent(); |
| 69 |
stream_ << '"' << tag << "\": "; |
| 70 |
object_begin = false; |
| 71 |
} |
| 72 |
|
| 73 |
void begin_object() |
| 74 |
{ |
| 75 |
stream_ << "{"; |
| 76 |
++depth_; |
| 77 |
object_begin = true; |
| 78 |
} |
| 79 |
|
| 80 |
void end_object() |
| 81 |
{ |
| 82 |
--depth_; |
| 83 |
make_indent(); |
| 84 |
stream_ << "}"; |
| 85 |
} |
| 86 |
|
| 87 |
void begin_variant() { begin_object(); } |
| 88 |
void end_variant() { end_object(); } |
| 89 |
|
| 90 |
bool varint_bug_backward_compatibility_enabled() const { return false; } |
| 91 |
|
| 92 |
protected: |
| 93 |
void make_indent() |
| 94 |
{ |
| 95 |
if (indent_) |
| 96 |
{ |
| 97 |
stream_ << '\n' << std::string(2 * depth_, ' '); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
protected: |
| 102 |
stream_type &stream_; |
| 103 |
bool indent_; |
| 104 |
bool object_begin; |
| 105 |
size_t depth_; |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
template <bool W> |
| 116 |
struct json_archive; |
| 117 |
|
| 118 |
template <> |
| 119 |
struct json_archive<true> : public json_archive_base<std::ostream, true> |
| 120 |
{ |
| 121 |
json_archive(stream_type &s, bool indent = false) : base_type(s, indent), inner_array_size_(0) { } |
| 122 |
|
| 123 |
std::streampos getpos() const { return stream_.tellp(); } |
| 124 |
|
| 125 |
template<typename T> |
| 126 |
static auto promote_to_printable_integer_type(T v) -> decltype(+v) |
| 127 |
{ |
| 128 |
|
| 129 |
|
| 130 |
return +v; |
| 131 |
} |
| 132 |
|
| 133 |
template <class T> |
| 134 |
void serialize_int(T v) |
| 135 |
{ |
| 136 |
stream_ << std::dec << promote_to_printable_integer_type(v); |
| 137 |
} |
| 138 |
|
| 139 |
void serialize_blob(void *buf, size_t len, const char *delimiter="\"") { |
| 140 |
begin_string(delimiter); |
| 141 |
for (size_t i = 0; i < len; i++) { |
| 142 |
unsigned char c = ((unsigned char *)buf)[i]; |
| 143 |
stream_ << std::hex << std::setw(2) << std::setfill('0') << (int)c; |
| 144 |
} |
| 145 |
end_string(delimiter); |
| 146 |
} |
| 147 |
|
| 148 |
template <class T> |
| 149 |
void serialize_varint(T &v) |
| 150 |
{ |
| 151 |
stream_ << std::dec << promote_to_printable_integer_type(v); |
| 152 |
} |
| 153 |
|
| 154 |
void begin_string(const char *delimiter="\"") |
| 155 |
{ |
| 156 |
stream_ << delimiter; |
| 157 |
} |
| 158 |
|
| 159 |
void end_string(const char *delimiter="\"") |
| 160 |
{ |
| 161 |
stream_ << delimiter; |
| 162 |
} |
| 163 |
|
| 164 |
void begin_array(size_t s=0) |
| 165 |
{ |
| 166 |
inner_array_size_ = s; |
| 167 |
++depth_; |
| 168 |
stream_ << "[ "; |
| 169 |
} |
| 170 |
|
| 171 |
void delimit_array() |
| 172 |
{ |
| 173 |
stream_ << ", "; |
| 174 |
} |
| 175 |
|
| 176 |
void end_array() |
| 177 |
{ |
| 178 |
--depth_; |
| 179 |
if (0 < inner_array_size_) |
| 180 |
{ |
| 181 |
make_indent(); |
| 182 |
} |
| 183 |
stream_ << "]"; |
| 184 |
} |
| 185 |
|
| 186 |
void write_variant_tag(const char *t) |
| 187 |
{ |
| 188 |
tag(t); |
| 189 |
} |
| 190 |
|
| 191 |
private: |
| 192 |
size_t inner_array_size_; |
| 193 |
}; |