74 lines
3.1 KiB
C++
74 lines
3.1 KiB
C++
#include "ZegoDataUtils.h"
|
||
|
||
bool zego_value_is_null(flutter::EncodableValue value) { return value.IsNull(); }
|
||
|
||
int32_t zego_value_get_int(flutter::EncodableValue value) {
|
||
// dart 没有 int32_t int64_t 区分,这里处理了 int32 最高位为 1(负数)的 case
|
||
return (int32_t)zego_value_get_long(value);
|
||
}
|
||
|
||
int64_t zego_value_get_long(flutter::EncodableValue value) { return value.LongValue(); }
|
||
|
||
bool zego_value_get_bool(flutter::EncodableValue value) { return std::get<bool>(value); }
|
||
|
||
double zego_value_get_double(flutter::EncodableValue value) { return std::get<double>(value); }
|
||
|
||
std::string zego_value_get_string(flutter::EncodableValue value) {
|
||
return std::get<std::string>(value);
|
||
}
|
||
|
||
std::vector<float> zego_value_get_vector_float(flutter::EncodableValue value) {
|
||
return std::get<std::vector<float>>(value);
|
||
}
|
||
|
||
std::vector<uint8_t> zego_value_get_vector_uint8(flutter::EncodableValue value) {
|
||
return std::get<std::vector<uint8_t>>(value);
|
||
}
|
||
|
||
ZFMap zego_value_get_map(flutter::EncodableValue value) { return std::get<ZFMap>(value); }
|
||
|
||
ZFArray zego_value_get_list(flutter::EncodableValue value) { return std::get<ZFArray>(value); }
|
||
|
||
|
||
// 将 EncodableValue 转换为 nlohmann::json
|
||
nlohmann::json EncodableValueToJson(const flutter::EncodableValue& value) {
|
||
if (std::holds_alternative<bool>(value)) {
|
||
return std::get<bool>(value);
|
||
} else if (std::holds_alternative<int32_t>(value)) {
|
||
return std::get<int32_t>(value);
|
||
} else if (std::holds_alternative<int64_t>(value)) {
|
||
return std::get<int64_t>(value);
|
||
} else if (std::holds_alternative<double>(value)) {
|
||
return std::get<double>(value);
|
||
} else if (std::holds_alternative<std::string>(value)) {
|
||
return std::get<std::string>(value);
|
||
} else if (std::holds_alternative<flutter::EncodableList>(value)) {
|
||
nlohmann::json json_array = nlohmann::json::array();
|
||
for (const auto& item : std::get<flutter::EncodableList>(value)) {
|
||
json_array.push_back(EncodableValueToJson(item));
|
||
}
|
||
return json_array;
|
||
} else if (std::holds_alternative<flutter::EncodableMap>(value)) {
|
||
nlohmann::json json_object = nlohmann::json::object();
|
||
for (const auto& pair : std::get<flutter::EncodableMap>(value)) {
|
||
std::string key = std::get<std::string>(pair.first); // 假设键是字符串
|
||
json_object[key] = EncodableValueToJson(pair.second);
|
||
}
|
||
return json_object;
|
||
}
|
||
return nullptr; // 处理空值或不支持的类型
|
||
}
|
||
|
||
// 将 EncodableMap 转换为 JSON 字符串
|
||
std::string map_2_json(const flutter::EncodableMap& map) {
|
||
nlohmann::json json_object = nlohmann::json::object();
|
||
for (const auto& pair : map) {
|
||
std::string key = std::get<std::string>(pair.first); // 假设键是字符串
|
||
json_object[key] = EncodableValueToJson(pair.second);
|
||
}
|
||
auto json_string = json_object.dump(); // 序列化为 JSON 字符串
|
||
return json_string; // 序列化为 JSON 字符串
|
||
}
|
||
std::string value_2_json(flutter::EncodableValue value){
|
||
return EncodableValueToJson(value).dump();
|
||
} |