loom

Форк
0
176 строк · 4.0 Кб
1
#include "simodo/variable/json/Rpc.h"
2
#include "simodo/variable/json/Serialization.h"
3
#include "simodo/inout/convert/functions.h"
4

5
namespace simodo::variable
6
{
7

8
JsonRpc::JsonRpc(const std::string & json)
9
    : _origin_json(json)
10
    , _value(fromJson(json))
11
{
12
    if (_value.type() != ValueType::Object)
13
        return;
14

15
    setupMembers();
16
}
17

18
JsonRpc::JsonRpc(const std::u16string & json)
19
    : _origin_json(inout::toU8(json))
20
    , _value(fromJson(json))
21
{
22
    if (_value.type() != ValueType::Object)
23
        return;
24

25
    setupMembers();
26
}
27

28
JsonRpc::JsonRpc(variable::Value value)
29
    : _origin_json(toJson(value))
30
    , _value(value)
31
{
32
    if (_value.type() != ValueType::Object)
33
        return;
34

35
    setupMembers();
36
}
37

38
JsonRpc::JsonRpc(std::u16string method, variable::Value params, int64_t id)
39
    : _value(Object {{
40
        {u"jsonrpc_version", u"2.0"},
41
        {u"method", method},
42
        {u"params", params},
43
        {u"id", id},
44
    }})
45
{
46
    setupMembers();
47
}
48

49
JsonRpc::JsonRpc(std::u16string method, int64_t id)
50
    : _value(Object {{
51
        {u"jsonrpc_version", u"2.0"},
52
        {u"method", method},
53
        {u"id", id},
54
    }})
55
{
56
    setupMembers();
57
}
58

59
JsonRpc::JsonRpc(std::u16string method, variable::Value params)
60
    : _value(Object {{
61
        {u"jsonrpc_version", u"2.0"},
62
        {u"method", method},
63
        {u"params", params},
64
    }})
65
{
66
    setupMembers();
67
}
68

69
// Rpc::Rpc(std::u16string method)
70
//     : _value(Record {{
71
//         {u"jsonrpc_version", u"2.0"},
72
//         {u"method", method},
73
//     }})
74
// {
75
//     setupMembers();
76
// }
77

78
JsonRpc::JsonRpc(variable::Value result, int64_t id)
79
    : _value(Object {{
80
        {u"jsonrpc_version", u"2.0"},
81
        {u"result", result},
82
        {u"id", id},
83
    }})
84
{
85
    setupMembers();
86
}
87

88
JsonRpc::JsonRpc(int64_t code, std::u16string message, variable::Value data, int64_t id)
89
    : _value(Object {{
90
        {u"jsonrpc_version", u"2.0"},
91
        {u"error", Object {{
92
            {u"code",    code},
93
            {u"message", message},
94
            {u"data",    data},
95
        }}},
96
        {u"id", id},
97
    }})
98
{
99
    setupMembers();
100
}
101

102
JsonRpc::JsonRpc(int64_t code, std::u16string message, int64_t id)
103
    : _value(Object {{
104
        {u"jsonrpc_version", u"2.0"},
105
        {u"error", Object {{
106
            {u"code",    code},
107
            {u"message", message},
108
        }}},
109
        {u"id", id},
110
    }})
111
{
112
    setupMembers();
113
}
114

115
const variable::Value & JsonRpc::params() const
116
{
117
    return _params ? *_params : _fail;
118
}
119

120
const variable::Value & JsonRpc::result() const
121
{
122
    return _result ? *_result : _fail;
123
}
124

125
const variable::Value & JsonRpc::error() const
126
{
127
    return _error ? *_error : _fail;
128
}
129

130
void JsonRpc::setupMembers()
131
{
132
    if (_is_valid)
133
        return;
134

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");
142

143
    if (jsonrpc_value.type() == ValueType::String)
144
        _jsonrpc_version = jsonrpc_value.getString();
145
    else
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 = &params_value;
153
    if (!result_value.isNull())
154
        _result = &result_value;
155
    if (error_value.type() == ValueType::Object)
156
        _error = &error_value;
157

158
    if (_origin_json.empty())
159
        _origin_json = toJson(_value);
160

161
    if (!_method.empty() && (_result || _error))
162
        return;
163

164
    if (_method.empty() && !_result && !_error && _id < 0)
165
        return;
166

167
    if (_result && _error)
168
        return;
169

170
    if ((_result || _error) && _params)
171
        return;
172

173
    _is_valid = true;
174
}
175

176
}
177

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.