| 222 | } |
| 223 | |
| 224 | bool Message::decodeFromJSON(std::string_view json, ExceptionTracker& exceptionTracker) { |
| 225 | if (_descriptor == nullptr) { |
| 226 | exceptionTracker.onError("Cannot parse from JSON without a descriptor"); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | JSONHelper jsonHelper(_descriptor, reinterpret_cast<const Byte*>(json.data()), json.size()); |
| 231 | |
| 232 | auto result = google::protobuf::util::JsonToBinaryStream(jsonHelper.typeResolver, |
| 233 | jsonHelper.typeUrl, |
| 234 | &jsonHelper.inputStream, |
| 235 | &jsonHelper.outputStream, |
| 236 | google::protobuf::util::JsonParseOptions()); |
| 237 | if (!result.ok()) { |
| 238 | exceptionTracker.onError(result.message()); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | auto buffer = makeShared<ByteBuffer>(); |
| 243 | buffer->set(reinterpret_cast<const Byte*>(jsonHelper.output.data()), |
| 244 | reinterpret_cast<const Byte*>(jsonHelper.output.data() + jsonHelper.output.size())); |
| 245 | _dataSource = buffer; |
| 246 | |
| 247 | return decode(buffer->data(), buffer->size(), exceptionTracker); |
| 248 | } |
| 249 | |
| 250 | Ref<Message> Message::clone() const { |
| 251 | auto out = makeShared<Message>(_descriptor, _dataSource); |