6#include <bond/core/config.h>
8#include "detail/rapidjson_helper.h"
11#include <bond/core/transforms.h>
17template <
typename Buffer>
18class SimpleJsonReader;
22template <
typename BufferT>
24 :
protected rapidjson::Writer<detail::RapidJsonOutputStream<BufferT> >,
28 typedef BufferT Buffer;
36 SimpleJsonWriter(Buffer& output,
bool pretty =
false,
int indent = 4,
bool all_fields =
true)
37 : rapidjson::Writer<detail::RapidJsonOutputStream<BufferT> >(_stream),
42 _indent((
std::min)(indent, 8)),
44 _all_fields(all_fields)
48 typename boost::call_traits<Buffer>::reference
57 void WriteOpen(
char bracket)
59 _output.Write(bracket);
64 void WriteClose(
char bracket)
69 _output.Write(bracket);
73 typename boost::enable_if<is_string_type<T> >::type
74 WriteName(
const T& name)
77 _output.Write(
": ", _pretty ? 2 : 1);
80 void WriteName(uint16_t
id)
84 _output.Write(
"\": ", _pretty ? 3 : 2);
87 void Write(
bool value)
90 _output.Write(
"true", 4);
92 _output.Write(
"false", 5);
96 typename boost::enable_if<is_string_type<T> >::type
102 template <
typename T>
103 typename boost::enable_if<is_signed_int<T> >::type
106 this->WriteInt64(value);
109 template <
typename T>
110 typename boost::enable_if<std::is_unsigned<T> >::type
113 this->WriteUint64(value);
116 void Write(
double value)
118 this->WriteDouble(value);
121 template <
typename T>
122 typename boost::enable_if<std::is_enum<T> >::type
123 Write(
const T& value)
125 this->WriteInt(
static_cast<int>(value));
128 template <
typename T>
129 typename boost::enable_if<is_type_alias<T> >::type
130 Write(
const T& value)
132 Write(get_aliased_value(value));
137 _output.Write(
"null", 4);
140 void WriteSeparator(
const int per_line = 1)
143 _output.Write(
", ", _pretty ? 2 : 1);
145 if (_count++ % per_line == 0)
150 using rapidjson::Writer<detail::RapidJsonOutputStream<BufferT> >::WriteString;
152 template <
typename T>
153 typename boost::enable_if<is_string<T> >::type
154 WriteString(
const T& value)
156 WriteString(string_data(value), string_length(value));
159 template <
typename T>
160 typename boost::enable_if<is_wstring<T> >::type
161 WriteString(
const T& value)
164 for (
const wchar_t *p = string_data(value), *end = p + string_length(value); p < end; ++p)
168 if (c < L
'\x20' || c ==
'"' || c ==
'\\' || c ==
'/')
172 case L
'\b': c = L
'b';
break;
173 case L
'\f': c = L
'f';
break;
174 case L
'\n': c = L
'n';
break;
175 case L
'\r': c = L
'r';
break;
176 case L
'\t': c = L
't';
break;
183 if (c >= L
'\x20' && c < L
'\x80')
185 _output.Write(
static_cast<char>(c));
195 void WriteUnicode(
wchar_t c)
198 u[2] = detail::HexDigit(
static_cast<int>(c >> 12));
199 u[3] = detail::HexDigit(
static_cast<int>(c >> 8));
200 u[4] = detail::HexDigit(
static_cast<int>(c >> 4));
201 u[5] = detail::HexDigit(
static_cast<int>(c >> 0));
202 _output.Write(u,
sizeof(u));
211 for (
int i = _level; i--;)
212 _output.Write(
" ", _indent);
215 template <
typename Writer,
typename Protocols>
216 friend class Serializer;
218 detail::RapidJsonOutputStream<BufferT> _stream;
224 const bool _all_fields;
228template <
typename Buffer>
struct
229is_writer<SimpleJsonWriter<Buffer>, void>
233template <
typename Buffer,
typename Protocols>
234class Serializer<SimpleJsonWriter<Buffer>, Protocols>
235 :
public SerializingTransform
238 typedef SimpleJsonWriter<Buffer> writer_type;
240 Serializer(writer_type& writer)
245 void Begin(
const Metadata& )
const
248 _output.WriteOpen(
'{');
253 WriteEnd(--_level != 0);
256 void UnknownEnd()
const
261 template <
typename T>
262 bool Base(
const T& value)
const
264 Apply<Protocols>(*
this, value);
268 template <
typename T>
269 bool Field(uint16_t ,
const Metadata& metadata,
const maybe<T>& value)
const
271 if (!value.is_nothing())
273 WriteName(detail::FieldName(metadata));
274 Write(value.value());
279 template <
typename T>
280 bool Field(uint16_t ,
const Metadata& metadata,
const T& value)
const
282 if (_output._all_fields
283 || !detail::omit_field<writer_type>(metadata, value))
285 WriteName(detail::FieldName(metadata));
291 template <
typename T>
292 bool UnknownField(uint16_t
id,
const T& value)
const
299 bool OmittedField(uint16_t
id,
const Metadata& metadata, BondDataType type)
const
301 if (_output._all_fields && !metadata.default_value.nothing)
306 Field(
id, metadata, !!metadata.default_value.uint_value);
312 Field(
id, metadata, metadata.default_value.uint_value);
316 Field(
id, metadata, metadata.default_value.double_value);
319 Field(
id, metadata, metadata.default_value.string_value);
326 WriteName(detail::FieldName(metadata));
327 _output.WriteOpen(
'[');
328 _output.WriteClose(
']');
331 WriteName(detail::FieldName(metadata));
332 _output.WriteOpen(
'{');
333 _output.WriteClose(
'}');
339 Field(
id, metadata, metadata.default_value.int_value);
342 Field(
id, metadata, metadata.default_value.wstring_value);
353 template <
typename T,
typename Reader>
354 void Container(
const value<T, Reader>& element, uint32_t size)
const
356 _output.WriteOpen(
'[');
360 _output.WriteSeparator();
364 _output.WriteClose(
']');
367 template <
typename Key,
typename T,
typename Reader>
368 void Container(
const value<Key, Reader>& key,
const T& value, uint32_t size)
const
370 _output.WriteOpen(
'[');
374 _output.WriteSeparator();
376 _output.WriteSeparator();
380 _output.WriteClose(
']');
384 void WriteEnd(
bool base)
const
387 _output.WriteClose(
'}');
390 template <
typename T>
391 void WriteName(
const T& name)
const
393 _output.WriteSeparator();
394 _output.WriteName(name);
398 template <
typename T>
399 typename boost::enable_if<is_basic_type<T> >::type
400 Write(
const T& value)
const
402 _output.Write(value);
406 template <
typename T>
407 void Write(
const nullable<T>& value)
const
415 _output.WriteOpen(
'[');
416 Write(value.value());
417 _output.WriteClose(
']');
422 template <
typename T>
423 typename boost::enable_if<is_bond_type<T> >::type
424 Write(
const T& value)
const
426 Apply<Protocols>(SerializeTo<Protocols>(_output), value);
430 template <
typename T1,
typename T2>
431 void Write(
const std::pair<T1, T2>& value)
const
434 _output.WriteSeparator(is_basic_type<T2>::value ? 2 : 1);
439 template <
typename T>
440 typename boost::enable_if<is_container<T> >::type
441 Write(
const T& value)
const
443 _output.WriteOpen(
'[');
445 for (const_enumerator<T> elements(value); elements.more();)
447 _output.WriteSeparator();
448 Write(elements.next());
451 _output.WriteClose(
']');
455 void Write(
const blob& value)
const
457 _output.WriteOpen(
'[');
459 for (
const char& ch : value)
461 _output.WriteSeparator();
462 _output.Write(
static_cast<int8_t
>(ch));
465 _output.WriteClose(
']');
469 template <
typename Reader,
typename T>
470 typename boost::enable_if<is_basic_type<T> >::type
471 Write(
const value<T, Reader>& value)
const
475 value.template Deserialize<Protocols>(data);
479 template <
typename Reader,
typename T>
480 typename boost::disable_if<is_basic_type<T> >::type
481 Write(
const value<T, Reader>& value)
const
483 Apply<Protocols>(SerializeTo<Protocols>(_output), value);
487 writer_type& _output;
488 mutable uint32_t _level;
Reader for Simple JSON.
Definition: simple_json_reader.h:28
Writer for Simple JSON.
Definition: simple_json_writer.h:26
SimpleJsonWriter(Buffer &output, bool pretty=false, int indent=4, bool all_fields=true)
Construct from output buffer/stream.
Definition: simple_json_writer.h:36
boost::call_traits< Buffer >::reference GetBuffer()
Access to underlying buffer.
Definition: simple_json_writer.h:49
namespace bond
Definition: apply.h:17