Flutter Engine
flutter::JsonMessageCodec Class Reference

#include <json_message_codec.h>

Inheritance diagram for flutter::JsonMessageCodec:
flutter::MessageCodec< rapidjson::Document >

Public Member Functions

 ~JsonMessageCodec ()=default
 
 JsonMessageCodec (JsonMessageCodec const &)=delete
 
JsonMessageCodecoperator= (JsonMessageCodec const &)=delete
 
- Public Member Functions inherited from flutter::MessageCodec< rapidjson::Document >
 MessageCodec ()=default
 
 MessageCodec (MessageCodec< rapidjson::Document > const &)=delete
 
virtual ~MessageCodec ()=default
 
MessageCodecoperator= (MessageCodec< rapidjson::Document > const &)=delete
 
std::unique_ptr< rapidjson::Document > DecodeMessage (const uint8_t *binary_message, const size_t message_size) const
 
std::unique_ptr< rapidjson::Document > DecodeMessage (const std::vector< uint8_t > &binary_message) const
 
std::unique_ptr< std::vector< uint8_t > > EncodeMessage (const rapidjson::Document &message) const
 

Static Public Member Functions

static const JsonMessageCodecGetInstance ()
 

Protected Member Functions

 JsonMessageCodec ()=default
 
std::unique_ptr< rapidjson::Document > DecodeMessageInternal (const uint8_t *binary_message, const size_t message_size) const override
 
std::unique_ptr< std::vector< uint8_t > > EncodeMessageInternal (const rapidjson::Document &message) const override
 

Detailed Description

Definition at line 16 of file json_message_codec.h.

Constructor & Destructor Documentation

◆ ~JsonMessageCodec()

flutter::JsonMessageCodec::~JsonMessageCodec ( )
default

◆ JsonMessageCodec() [1/2]

flutter::JsonMessageCodec::JsonMessageCodec ( JsonMessageCodec const &  )
delete

◆ JsonMessageCodec() [2/2]

flutter::JsonMessageCodec::JsonMessageCodec ( )
protecteddefault

Member Function Documentation

◆ DecodeMessageInternal()

std::unique_ptr< rapidjson::Document > flutter::JsonMessageCodec::DecodeMessageInternal ( const uint8_t *  binary_message,
const size_t  message_size 
) const
overrideprotectedvirtual

Implements flutter::MessageCodec< rapidjson::Document >.

Definition at line 33 of file json_message_codec.cc.

35  {
36  auto raw_message = reinterpret_cast<const char*>(binary_message);
37  auto json_message = std::make_unique<rapidjson::Document>();
38  rapidjson::ParseResult result =
39  json_message->Parse(raw_message, message_size);
40  bool parsing_successful =
41  result == rapidjson::ParseErrorCode::kParseErrorNone;
42  if (!parsing_successful) {
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 }

◆ EncodeMessageInternal()

std::unique_ptr< std::vector< uint8_t > > flutter::JsonMessageCodec::EncodeMessageInternal ( const rapidjson::Document &  message) const
overrideprotectedvirtual

Implements flutter::MessageCodec< rapidjson::Document >.

Definition at line 22 of file json_message_codec.cc.

23  {
24  // TODO: Look into alternate writers that would avoid the buffer copy.
25  rapidjson::StringBuffer buffer;
26  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
27  message.Accept(writer);
28  const char* buffer_start = buffer.GetString();
29  return std::make_unique<std::vector<uint8_t>>(
30  buffer_start, buffer_start + buffer.GetSize());
31 }

◆ GetInstance()

◆ operator=()

JsonMessageCodec& flutter::JsonMessageCodec::operator= ( JsonMessageCodec const &  )
delete

The documentation for this class was generated from the following files: