Flutter iOS Embedder
json_message_codec.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <iostream>
8 #include <string>
9 
10 #include "rapidjson/error/en.h"
11 #include "rapidjson/stringbuffer.h"
12 #include "rapidjson/writer.h"
13 
14 namespace flutter {
15 
16 // static
18  static JsonMessageCodec sInstance;
19  return sInstance;
20 }
21 
22 std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessageInternal(
23  const rapidjson::Document& message) const {
24  rapidjson::StringBuffer buffer;
25  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
26  // clang-tidy has trouble reasoning about some of the complicated array and
27  // pointer-arithmetic code in rapidjson.
28  // NOLINTNEXTLINE(clang-analyzer-core.*)
29  message.Accept(writer);
30  const char* buffer_start = buffer.GetString();
31  return std::make_unique<std::vector<uint8_t>>(
32  buffer_start, buffer_start + buffer.GetSize());
33 }
34 
35 std::unique_ptr<rapidjson::Document> JsonMessageCodec::DecodeMessageInternal(
36  const uint8_t* binary_message,
37  const size_t message_size) const {
38  auto raw_message = reinterpret_cast<const char*>(binary_message);
39  auto json_message = std::make_unique<rapidjson::Document>();
40  rapidjson::ParseResult result =
41  json_message->Parse(raw_message, message_size);
42  if (result.IsError()) {
43  std::cerr << "Unable to parse JSON message:" << std::endl
44  << rapidjson::GetParseError_En(result.Code()) << std::endl;
45  return nullptr;
46  }
47  return json_message;
48 }
49 
50 } // namespace flutter
flutter::JsonMessageCodec::GetInstance
static const JsonMessageCodec & GetInstance()
Definition: json_message_codec.cc:17
json_message_codec.h
flutter::JsonMessageCodec::DecodeMessageInternal
std::unique_ptr< rapidjson::Document > DecodeMessageInternal(const uint8_t *binary_message, const size_t message_size) const override
Definition: json_message_codec.cc:35
flutter
Definition: accessibility_bridge.h:28
flutter::JsonMessageCodec
Definition: json_message_codec.h:16
flutter::JsonMessageCodec::EncodeMessageInternal
std::unique_ptr< std::vector< uint8_t > > EncodeMessageInternal(const rapidjson::Document &message) const override
Definition: json_message_codec.cc:22