1
#include "simodo/variable/json/Rpc.h"
2
#include "simodo/variable/json/Serialization.h"
3
#include "simodo/inout/convert/functions.h"
5
namespace simodo::variable
8
JsonRpc::JsonRpc(const std::string & json)
10
, _value(fromJson(json))
12
if (_value.type() != ValueType::Object)
18
JsonRpc::JsonRpc(const std::u16string & json)
19
: _origin_json(inout::toU8(json))
20
, _value(fromJson(json))
22
if (_value.type() != ValueType::Object)
28
JsonRpc::JsonRpc(variable::Value value)
29
: _origin_json(toJson(value))
32
if (_value.type() != ValueType::Object)
38
JsonRpc::JsonRpc(std::u16string method, variable::Value params, int64_t id)
40
{u"jsonrpc_version", u"2.0"},
49
JsonRpc::JsonRpc(std::u16string method, int64_t id)
51
{u"jsonrpc_version", u"2.0"},
59
JsonRpc::JsonRpc(std::u16string method, variable::Value params)
61
{u"jsonrpc_version", u"2.0"},
69
// Rpc::Rpc(std::u16string method)
71
// {u"jsonrpc_version", u"2.0"},
72
// {u"method", method},
78
JsonRpc::JsonRpc(variable::Value result, int64_t id)
80
{u"jsonrpc_version", u"2.0"},
88
JsonRpc::JsonRpc(int64_t code, std::u16string message, variable::Value data, int64_t id)
90
{u"jsonrpc_version", u"2.0"},
93
{u"message", message},
102
JsonRpc::JsonRpc(int64_t code, std::u16string message, int64_t id)
104
{u"jsonrpc_version", u"2.0"},
107
{u"message", message},
115
const variable::Value & JsonRpc::params() const
117
return _params ? *_params : _fail;
120
const variable::Value & JsonRpc::result() const
122
return _result ? *_result : _fail;
125
const variable::Value & JsonRpc::error() const
127
return _error ? *_error : _fail;
130
void JsonRpc::setupMembers()
135
const auto object = _value.getObject();
136
const Value & jsonrpc_value = object->find(u"jsonrpc_version");
137
const Value & method_value = object->find(u"method");
138
const Value & id_value = object->find(u"id");
139
const Value & params_value = object->find(u"params");
140
const Value & result_value = object->find(u"result");
141
const Value & error_value = object->find(u"error");
143
if (jsonrpc_value.type() == ValueType::String)
144
_jsonrpc_version = jsonrpc_value.getString();
146
_jsonrpc_version = u"1.0";
147
if (method_value.type() == ValueType::String)
148
_method = method_value.getString();
149
if (id_value.type() == ValueType::Int)
150
_id = id_value.getInt();
151
if (params_value.type() == ValueType::Array || params_value.type() == ValueType::Object)
152
_params = ¶ms_value;
153
if (!result_value.isNull())
154
_result = &result_value;
155
if (error_value.type() == ValueType::Object)
156
_error = &error_value;
158
if (_origin_json.empty())
159
_origin_json = toJson(_value);
161
if (!_method.empty() && (_result || _error))
164
if (_method.empty() && !_result && !_error && _id < 0)
167
if (_result && _error)
170
if ((_result || _error) && _params)