mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
The JSON codec is awkward to use in the wrapper (since the client has to build and link one of the JSON libraries to do so). Since it would be very cumbersome to wrap in a C API, and there's essentially no reason to use it instead of the standard codec, this removes it from the wrapper entirely. Since some system channels (internal to the engine) still use it, it's moved into common/cpp instead of being eliminated entirely. Internally we always use RapidJSON though, so the jsoncpp implementation is removed. Also adds some unit test coverage, since there wasn't any. Fixes #30669
51 lines
1.7 KiB
C++
51 lines
1.7 KiB
C++
// Copyright 2013 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include "rapidjson/error/en.h"
|
|
#include "rapidjson/stringbuffer.h"
|
|
#include "rapidjson/writer.h"
|
|
|
|
namespace flutter {
|
|
|
|
// static
|
|
const JsonMessageCodec& JsonMessageCodec::GetInstance() {
|
|
static JsonMessageCodec sInstance;
|
|
return sInstance;
|
|
}
|
|
|
|
std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessageInternal(
|
|
const rapidjson::Document& message) const {
|
|
// TODO: Look into alternate writers that would avoid the buffer copy.
|
|
rapidjson::StringBuffer buffer;
|
|
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
|
|
message.Accept(writer);
|
|
const char* buffer_start = buffer.GetString();
|
|
return std::make_unique<std::vector<uint8_t>>(
|
|
buffer_start, buffer_start + buffer.GetSize());
|
|
}
|
|
|
|
std::unique_ptr<rapidjson::Document> JsonMessageCodec::DecodeMessageInternal(
|
|
const uint8_t* binary_message,
|
|
const size_t message_size) const {
|
|
auto raw_message = reinterpret_cast<const char*>(binary_message);
|
|
auto json_message = std::make_unique<rapidjson::Document>();
|
|
rapidjson::ParseResult result =
|
|
json_message->Parse(raw_message, message_size);
|
|
bool parsing_successful =
|
|
result == rapidjson::ParseErrorCode::kParseErrorNone;
|
|
if (!parsing_successful) {
|
|
std::cerr << "Unable to parse JSON message:" << std::endl
|
|
<< rapidjson::GetParseError_En(result.Code()) << std::endl;
|
|
return nullptr;
|
|
}
|
|
return json_message;
|
|
}
|
|
|
|
} // namespace flutter
|