6#include <bond/core/config.h>
8#include "detail/rapidjson_helper.h"
11#include "rapidjson/document.h"
12#include "rapidjson/reader.h"
14#include <boost/make_shared.hpp>
15#include <boost/none.hpp>
16#include <boost/optional/optional.hpp>
21template <
typename BufferT>
22class SimpleJsonWriter;
26template <
typename BufferT>
30 typedef BufferT Buffer;
31 typedef DOMParser<SimpleJsonReader&> Parser;
33 typedef rapidjson::Value Field;
35 BOND_STATIC_CONSTEXPR uint16_t magic = SIMPLE_JSON_PROTOCOL;
36 BOND_STATIC_CONSTEXPR uint16_t version = 0x0001;
41 _document(boost::make_shared<rapidjson::Document>()),
51 _document(parent._document),
55 BOOST_ASSERT(parent._value);
66 if (!_value || _value == _document.get())
68 const unsigned parseFlags = rapidjson::kParseIterativeFlag | rapidjson::kParseStopWhenDoneFlag;
70 _document->ParseStream<parseFlags>(_streamHolder.Get());
74 BOOST_ASSERT(!_document->HasParseError());
75 _value = _document.get();
79 const Field* FindField(uint16_t
id,
const Metadata& metadata, BondDataType type)
86 return FindField(
id, metadata, type, type == BT_INT32);
89 const Field* FindField(uint16_t
id,
const Metadata& metadata, BondDataType type,
bool is_enum);
95 detail::Read(*GetValue(), var);
99 void ReadContainerBegin(uint32_t&, T&)
104 void ReadContainerEnd()
109 template <
typename T>
113 template <
typename T>
118 bool operator==(
const SimpleJsonReader& rhs)
const
120 return _value == rhs._value;
126 return _streamHolder.Get().GetBuffer();
132 return _streamHolder.Get().GetBuffer();
136 rapidjson::Value::ConstMemberIterator MemberBegin()
const
138 return GetValue()->IsObject() ? GetValue()->MemberBegin() : rapidjson::Value::ConstMemberIterator();
141 rapidjson::Value::ConstMemberIterator MemberEnd()
const
143 return GetValue()->IsObject() ? GetValue()->MemberEnd() : rapidjson::Value::ConstMemberIterator();
146 rapidjson::Value::ConstValueIterator ArrayBegin()
const
148 return GetValue()->IsArray() ? GetValue()->Begin() : rapidjson::Value::ConstValueIterator();
151 rapidjson::Value::ConstValueIterator ArrayEnd()
const
153 return GetValue()->IsArray() ? GetValue()->End() : rapidjson::Value::ConstValueIterator();
156 uint32_t ArraySize()
const
158 return GetValue()->IsArray() ? GetValue()->Size() : 0;
161 const rapidjson::Value* GetValue()
const
163 BOOST_ASSERT(_value);
167 template <
typename Input>
168 friend struct base_input;
170 template <
typename Protocols,
typename A,
typename T,
typename Buffer>
171 friend void DeserializeContainer(std::vector<bool, A>&,
const T&, SimpleJsonReader<Buffer>&);
173 template <
typename Protocols,
typename T,
typename Buffer>
174 friend void DeserializeContainer(blob&,
const T&, SimpleJsonReader<Buffer>&);
176 template <
typename Protocols,
typename X,
typename T,
typename Buffer>
177 friend typename boost::enable_if<is_list_container<X> >::type
178 DeserializeContainer(X&,
const T&, SimpleJsonReader<Buffer>&);
180 template <
typename Protocols,
typename X,
typename T,
typename Buffer>
181 friend typename boost::enable_if<is_set_container<X> >::type
182 DeserializeContainer(X&,
const T&, SimpleJsonReader<Buffer>&);
184 template <
typename Protocols,
typename X,
typename T,
typename Buffer>
185 friend typename boost::enable_if<is_map_container<X> >::type
186 DeserializeMap(X&, BondDataType,
const T&, SimpleJsonReader<Buffer>&);
188 const rapidjson::Value* _value;
189 boost::shared_ptr<rapidjson::Document> _document;
196 explicit StreamHolder(
const Buffer& input)
202 _stream.emplace(input);
203 BOOST_ASSERT(IsParent());
206 explicit StreamHolder(SimpleJsonReader& parent)
209 _parent(&parent._streamHolder.GetParent())
211 BOOST_ASSERT(_parent->IsParent());
212 BOOST_ASSERT(!IsParent());
217 StreamHolder(
const StreamHolder& other)
218 : _stream(other.Get()),
221 BOOST_ASSERT(IsParent());
224 StreamHolder& operator=(
const StreamHolder& other)
226 _stream.emplace(other.Get());
228 BOOST_ASSERT(IsParent());
233 StreamHolder(StreamHolder&&) =
default;
234 StreamHolder& operator=(StreamHolder&&) =
default;
236 const detail::RapidJsonInputStream<Buffer>& Get()
const
238 return IsParent() ? _stream.value() : _parent->Get();
241 detail::RapidJsonInputStream<Buffer>& Get()
243 return IsParent() ? _stream.value() : _parent->Get();
247 bool IsParent()
const
249 const bool isParent = _parent ==
nullptr;
250 BOOST_ASSERT(isParent ==
static_cast<bool>(_stream));
254 StreamHolder& GetParent()
256 return IsParent() ? *this : *_parent;
259 boost::optional<detail::RapidJsonInputStream<Buffer>> _stream;
260 StreamHolder* _parent;
265template <
typename Buffer>
266BOND_CONSTEXPR_OR_CONST uint16_t SimpleJsonReader<Buffer>::magic;
269template <
typename Input,
typename Output>
struct
270is_protocol_same<SimpleJsonReader<Input>, SimpleJsonWriter<Output> >
271 : std::false_type {};
Reader for Simple JSON.
Definition: simple_json_reader.h:28
SimpleJsonReader(SimpleJsonReader &parent, const Field &value)
Create a "child" SimpleJsonReader to read value, which is known to be a member of the document in par...
Definition: simple_json_reader.h:49
Buffer & GetBuffer()
Access to underlying buffer.
Definition: simple_json_reader.h:130
SimpleJsonReader(const Buffer &input)
Construct from input buffer/stream containing serialized data.
Definition: simple_json_reader.h:39
const Buffer & GetBuffer() const
Access to underlying buffer.
Definition: simple_json_reader.h:124
Writer for Simple JSON.
Definition: simple_json_writer.h:26
namespace bond
Definition: apply.h:17