@brief Decode a string, either json or yaml into a @see specs::RPCRequest . @c ec will report the error if occurs @see RPCErrorCode @param request The string request, this should be either json or yaml. @param ec Output value, The error reporting. @return specs::RPCRequest A valid rpc response object if no errors.
| 130 | /// @return specs::RPCRequest A valid rpc response object if no errors. |
| 131 | /// |
| 132 | static specs::RPCRequest |
| 133 | decode(std::string const &request, std::error_code &ec) noexcept |
| 134 | { |
| 135 | specs::RPCRequest msg; |
| 136 | try { |
| 137 | YAML::Node node = YAML::Load(request); |
| 138 | switch (node.Type()) { |
| 139 | case YAML::NodeType::Map: { // 4 |
| 140 | // single element |
| 141 | msg.add_message(decode_and_validate(node)); |
| 142 | } break; |
| 143 | case YAML::NodeType::Sequence: { // 3 |
| 144 | // In case we get [] which is valid sequence but invalid jsonrpc message. |
| 145 | if (node.size() > 0) { |
| 146 | // it's a batch |
| 147 | msg.is_batch(true); |
| 148 | msg.reserve(node.size()); |
| 149 | |
| 150 | for (auto &&n : node) { |
| 151 | msg.add_message(decode_and_validate(n)); |
| 152 | } |
| 153 | } else { |
| 154 | // Valid json but invalid base on jsonrpc specs, ie: []. |
| 155 | ec = error::RPCErrorCode::INVALID_REQUEST; |
| 156 | } |
| 157 | } break; |
| 158 | default: |
| 159 | // Only Sequences or Objects are valid. |
| 160 | ec = error::RPCErrorCode::INVALID_REQUEST; |
| 161 | break; |
| 162 | } |
| 163 | } catch (YAML::Exception const &e) { |
| 164 | ec = error::RPCErrorCode::PARSE_ERROR; |
| 165 | } |
| 166 | return msg; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | /// |