6 #ifndef KAGOME_CUSTOM_JSON_WRITER_HPP 7 #define KAGOME_CUSTOM_JSON_WRITER_HPP 9 #include <jsonrpc-lean/json.h> 10 #include <jsonrpc-lean/jsonformatteddata.h> 11 #include <jsonrpc-lean/util.h> 12 #include <jsonrpc-lean/value.h> 13 #include <jsonrpc-lean/writer.h> 15 #define RAPIDJSON_NO_SIZETYPEDEFINE 20 #include <rapidjson/stringbuffer.h> 21 #include <rapidjson/writer.h> 33 std::shared_ptr<jsonrpc::FormattedData>
GetData()
override {
34 return std::static_pointer_cast<jsonrpc::FormattedData>(myRequestData);
46 const jsonrpc::Value &
id)
override {
47 myRequestData->Writer.StartObject();
49 myRequestData->Writer.Key(jsonrpc::json::JSONRPC_NAME,
50 sizeof(jsonrpc::json::JSONRPC_NAME) - 1);
51 myRequestData->Writer.String(
52 jsonrpc::json::JSONRPC_VERSION_2_0,
53 sizeof(jsonrpc::json::JSONRPC_VERSION_2_0) - 1);
55 myRequestData->Writer.Key(jsonrpc::json::METHOD_NAME,
56 sizeof(jsonrpc::json::METHOD_NAME) - 1);
57 myRequestData->Writer.String(methodName.data(), methodName.size(),
true);
61 myRequestData->Writer.Key(jsonrpc::json::PARAMS_NAME,
62 sizeof(jsonrpc::json::PARAMS_NAME) - 1);
66 myRequestData->Writer.EndObject();
78 myRequestData->Writer.StartObject();
80 myRequestData->Writer.Key(jsonrpc::json::JSONRPC_NAME,
81 sizeof(jsonrpc::json::JSONRPC_NAME) - 1);
82 myRequestData->Writer.String(
83 jsonrpc::json::JSONRPC_VERSION_2_0,
84 sizeof(jsonrpc::json::JSONRPC_VERSION_2_0) - 1);
88 myRequestData->Writer.Key(jsonrpc::json::RESULT_NAME,
89 sizeof(jsonrpc::json::RESULT_NAME) - 1);
93 myRequestData->Writer.EndObject();
97 myRequestData->Writer.StartObject();
99 myRequestData->Writer.Key(jsonrpc::json::JSONRPC_NAME,
100 sizeof(jsonrpc::json::JSONRPC_NAME) - 1);
101 myRequestData->Writer.String(
102 jsonrpc::json::JSONRPC_VERSION_2_0,
103 sizeof(jsonrpc::json::JSONRPC_VERSION_2_0) - 1);
109 myRequestData->Writer.EndObject();
112 void WriteFault(int32_t code,
const std::string &
string)
override {
113 myRequestData->Writer.Key(jsonrpc::json::ERROR_NAME,
114 sizeof(jsonrpc::json::ERROR_NAME) - 1);
115 myRequestData->Writer.StartObject();
117 myRequestData->Writer.Key(jsonrpc::json::ERROR_CODE_NAME,
118 sizeof(jsonrpc::json::ERROR_CODE_NAME) - 1);
119 myRequestData->Writer.Int(code);
121 myRequestData->Writer.Key(jsonrpc::json::ERROR_MESSAGE_NAME,
122 sizeof(jsonrpc::json::ERROR_MESSAGE_NAME) - 1);
123 myRequestData->Writer.String(
string.data(),
string.size(),
true);
125 myRequestData->Writer.EndObject();
129 myRequestData->Writer.StartArray();
133 myRequestData->Writer.EndArray();
137 myRequestData->Writer.StartObject();
141 myRequestData->Writer.EndObject();
145 myRequestData->Writer.Key(name.data(), name.size(),
true);
153 myRequestData->Writer.String(data, size,
true);
157 myRequestData->Writer.Null();
161 myRequestData->Writer.Bool(value);
165 myRequestData->Writer.Double(value);
168 void Write(int32_t value)
override {
169 myRequestData->Writer.Int(value);
172 void Write(int64_t value)
override {
173 myRequestData->Writer.Int64(value);
176 void Write(
const std::string &value)
override {
177 myRequestData->Writer.String(value.data(), value.size(),
true);
180 void Write(
const tm &value)
override {
181 Write(jsonrpc::util::FormatIso8601DateTime(value));
186 if (
id.IsString() ||
id.IsInteger32() ||
id.IsInteger64() ||
id.IsNil()) {
187 myRequestData->Writer.Key(jsonrpc::json::ID_NAME,
188 sizeof(jsonrpc::json::ID_NAME) - 1);
190 myRequestData->Writer.String(
191 id.AsString().data(),
id.AsString().size(),
true);
192 }
else if (
id.IsInteger32()) {
193 myRequestData->Writer.Int(
id.AsInteger32());
194 }
else if (
id.IsInteger64()) {
195 myRequestData->Writer.Int64(
id.AsInteger64());
197 myRequestData->Writer.Null();
207 #endif // KAGOME_CUSTOM_JSON_WRITER_HPP void WriteId(const jsonrpc::Value &id)
void Write(const std::string &value) override
void StartDocument() override
void EndDocument() override
void StartArray() override
void StartStruct() override
void WriteFault(int32_t code, const std::string &string) override
void EndResponse() override
void WriteNull() override
void StartResponse(const jsonrpc::Value &id) override
void StartStructElement(const std::string &name) override
void EndRequest() override
std::shared_ptr< jsonrpc::JsonFormattedData > myRequestData
void Write(double value) override
void Write(int32_t value) override
void StartFaultResponse(const jsonrpc::Value &id) override
void WriteBinary(const char *data, size_t size) override
void Write(bool value) override
void Write(int64_t value) override
void EndParameter() override
void Write(const tm &value) override
void EndStruct() override
void StartParameter() override
void EndStructElement() override
std::shared_ptr< jsonrpc::FormattedData > GetData() override
void StartRequest(const std::string &methodName, const jsonrpc::Value &id) override
void EndFaultResponse() override