From c90886cebb5c6785da54d5ed03107444afe22807 Mon Sep 17 00:00:00 2001 From: zhouaoteng Date: Mon, 4 Sep 2023 18:58:24 +0800 Subject: [PATCH] fix:cjson Rectification Signed-off-by: zhouaoteng --- bundle.json | 1 + .../base/src/dcamera_channel_info_cmd.cpp | 127 +++++++++++------- .../base/src/dcamera_event_cmd.cpp | 100 ++++++++------ .../base/src/dcamera_info_cmd.cpp | 81 ++++++----- .../base/src/dcamera_metadata_setting_cmd.cpp | 100 ++++++++------ .../base/src/dcamera_open_info_cmd.cpp | 81 ++++++----- services/cameraservice/sinkservice/BUILD.gn | 1 + services/cameraservice/sourceservice/BUILD.gn | 1 + 8 files changed, 290 insertions(+), 202 deletions(-) diff --git a/bundle.json b/bundle.json index e53251ae..2f23df33 100644 --- a/bundle.json +++ b/bundle.json @@ -49,6 +49,7 @@ "access_token" ], "third_party": [ + "cJSON", "ffmpeg", "jsoncpp", "libyuv" diff --git a/services/cameraservice/base/src/dcamera_channel_info_cmd.cpp b/services/cameraservice/base/src/dcamera_channel_info_cmd.cpp index d44d7c50..b7d737cc 100644 --- a/services/cameraservice/base/src/dcamera_channel_info_cmd.cpp +++ b/services/cameraservice/base/src/dcamera_channel_info_cmd.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,9 +14,7 @@ */ #include "dcamera_channel_info_cmd.h" - -#include "json/json.h" - +#include "cJSON.h" #include "distributed_camera_constants.h" #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" @@ -25,83 +23,110 @@ namespace OHOS { namespace DistributedHardware { int32_t DCameraChannelInfoCmd::Marshal(std::string& jsonStr) { - Json::Value rootValue; - rootValue["Type"] = Json::Value(type_); - rootValue["dhId"] = Json::Value(dhId_); - rootValue["Command"] = Json::Value(command_); + cJSON *rootValue = cJSON_CreateObject(); + if (rootValue == nullptr) { + return DCAMERA_BAD_VALUE; + } + cJSON_AddStringToObject(rootValue, "Type", type_.c_str()); + cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str()); + cJSON_AddStringToObject(rootValue, "Command", command_.c_str()); - Json::Value channelInfo; - channelInfo["SourceDevId"] = Json::Value(value_->sourceDevId_); - Json::Value details; - for (auto iter = value_->detail_.begin(); iter != value_->detail_.end(); iter++) { - Json::Value detail; - detail["DataSessionFlag"] = Json::Value(iter->dataSessionFlag_); - detail["StreamType"] = Json::Value(iter->streamType_); - details.append(detail); + cJSON *channelInfo = cJSON_CreateObject(); + if (channelInfo == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; } - channelInfo["Detail"] = details; - rootValue["Value"] = channelInfo; + cJSON_AddStringToObject(channelInfo, "SourceDevId", value_->sourceDevId_.c_str()); + cJSON_AddItemToObject(rootValue, "Value", channelInfo); - jsonStr = rootValue.toStyledString(); + cJSON *details = cJSON_CreateArray(); + if (details == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + cJSON_AddItemToObject(channelInfo, "Detail", details); + for (auto iter = value_->detail_.begin(); iter != value_->detail_.end(); iter++) { + cJSON *detail = cJSON_CreateObject(); + if (detail == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + cJSON_AddStringToObject(detail, "DataSessionFlag", iter->dataSessionFlag_.c_str()); + cJSON_AddNumberToObject(detail, "StreamType", iter->streamType_); + cJSON_AddItemToArray(details, detail); + } + + char *jsonstr = cJSON_Print(rootValue); + if (jsonstr == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + jsonStr = jsonstr; + cJSON_Delete(rootValue); + cJSON_free(jsonstr); return DCAMERA_OK; } int32_t DCameraChannelInfoCmd::Unmarshal(const std::string& jsonStr) { - JSONCPP_STRING errs; - Json::CharReaderBuilder readerBuilder; - Json::Value rootValue; - - std::unique_ptr const jsonReader(readerBuilder.newCharReader()); - if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) || - !rootValue.isObject()) { + cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); + if (rootValue == nullptr) { return DCAMERA_BAD_VALUE; } - - if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) { + cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type"); + if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - type_ = rootValue["Type"].asString(); - - if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) { + type_ = type->valuestring; + cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId"); + if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - dhId_ = rootValue["dhId"].asString(); - - if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) { + dhId_ = dhId->valuestring; + cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command"); + if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - command_ = rootValue["Command"].asString(); - - if (!rootValue.isMember("Value") || !rootValue["Value"].isObject()) { + command_ = command->valuestring; + cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value"); + if (valueJson == nullptr || !cJSON_IsObject(valueJson)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - Json::Value valueJson = rootValue["Value"]; - - if (!valueJson.isMember("SourceDevId") || !valueJson["SourceDevId"].isString()) { + cJSON *sourceDevId = cJSON_GetObjectItemCaseSensitive(valueJson, "SourceDevId"); + if (sourceDevId == nullptr || !cJSON_IsString(sourceDevId) || (sourceDevId->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } std::shared_ptr channelInfo = std::make_shared(); - channelInfo->sourceDevId_ = valueJson["SourceDevId"].asString(); - - if (!valueJson.isMember("Detail") || !valueJson["Detail"].isArray()) { + channelInfo->sourceDevId_ = sourceDevId->valuestring; + cJSON *details = cJSON_GetObjectItemCaseSensitive(valueJson, "Detail"); + if (details == nullptr || !cJSON_IsArray(details) || cJSON_GetArraySize(details) == 0) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - - for (Json::ArrayIndex i = 0; i < valueJson["Detail"].size(); i++) { - Json::Value detailJson = valueJson["Detail"][i]; - DCameraChannelDetail channelDetail; - if (!detailJson.isMember("DataSessionFlag") || !detailJson["DataSessionFlag"].isString()) { + cJSON *detail = nullptr; + cJSON_ArrayForEach(detail, details) { + cJSON *dataSessionFlag = cJSON_GetObjectItemCaseSensitive(detail, "DataSessionFlag"); + cJSON *streamType = cJSON_GetObjectItemCaseSensitive(detail, "StreamType"); + if (dataSessionFlag == nullptr || !cJSON_IsString(dataSessionFlag) || (dataSessionFlag->valuestring == nullptr)){ + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - if (!detailJson.isMember("StreamType") || !detailJson["StreamType"].isInt()) { + if (streamType == nullptr || !cJSON_IsNumber(streamType)){ + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - channelDetail.dataSessionFlag_ = detailJson["DataSessionFlag"].asString(); - channelDetail.streamType_ = static_cast(detailJson["StreamType"].asInt()); + DCameraChannelDetail channelDetail; + channelDetail.dataSessionFlag_ = dataSessionFlag->valuestring; + channelDetail.streamType_ = static_cast(streamType->valueint); channelInfo->detail_.push_back(channelDetail); } value_ = channelInfo; + cJSON_Delete(rootValue); return DCAMERA_OK; } } // namespace DistributedHardware diff --git a/services/cameraservice/base/src/dcamera_event_cmd.cpp b/services/cameraservice/base/src/dcamera_event_cmd.cpp index 74bc41ad..24c9a562 100644 --- a/services/cameraservice/base/src/dcamera_event_cmd.cpp +++ b/services/cameraservice/base/src/dcamera_event_cmd.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,9 +14,7 @@ */ #include "dcamera_event_cmd.h" - -#include "json/json.h" - +#include "cJSON.h" #include "distributed_camera_constants.h" #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" @@ -25,70 +23,86 @@ namespace OHOS { namespace DistributedHardware { int32_t DCameraEventCmd::Marshal(std::string& jsonStr) { - Json::Value rootValue; - rootValue["Type"] = Json::Value(type_); - rootValue["dhId"] = Json::Value(dhId_); - rootValue["Command"] = Json::Value(command_); - - Json::Value event; - event["EventType"] = Json::Value(value_->eventType_); - event["EventResult"] = Json::Value(value_->eventResult_); - event["EventContent"] = Json::Value(value_->eventContent_); - rootValue["Value"] = event; + cJSON *rootValue = cJSON_CreateObject(); + if (rootValue == nullptr) { + return DCAMERA_BAD_VALUE; + } + cJSON_AddStringToObject(rootValue, "Type", type_.c_str()); + cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str()); + cJSON_AddStringToObject(rootValue, "Command", command_.c_str()); - jsonStr = rootValue.toStyledString(); + cJSON *event = cJSON_CreateObject(); + if (event == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + cJSON_AddNumberToObject(event, "EventType", value_->eventType_); + cJSON_AddNumberToObject(event, "EventResult", value_->eventResult_); + cJSON_AddStringToObject(event, "EventContent", value_->eventContent_.c_str()); + cJSON_AddItemToObject(rootValue, "Value", event); + + char *jsonstr = cJSON_Print(rootValue); + if (jsonstr == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + jsonStr = jsonstr; + cJSON_Delete(rootValue); + cJSON_free(jsonstr); return DCAMERA_OK; } int32_t DCameraEventCmd::Unmarshal(const std::string& jsonStr) { - JSONCPP_STRING errs; - Json::CharReaderBuilder readerBuilder; - Json::Value rootValue; - - std::unique_ptr const jsonReader(readerBuilder.newCharReader()); - if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) || - !rootValue.isObject()) { + cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); + if (rootValue == nullptr) { return DCAMERA_BAD_VALUE; } - - if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) { + cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type"); + if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - type_ = rootValue["Type"].asString(); - - if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) { + type_ = type->valuestring; + cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId"); + if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - dhId_ = rootValue["dhId"].asString(); - - if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) { + dhId_ = dhId->valuestring; + cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command"); + if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - command_ = rootValue["Command"].asString(); - - if (!rootValue.isMember("Value") || !rootValue["Value"].isObject()) { + command_ = command->valuestring; + cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value"); + if (valueJson == nullptr || !cJSON_IsObject(valueJson)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - Json::Value valueJson = rootValue["Value"]; - - if (!valueJson.isMember("EventType") || !valueJson["EventType"].isInt()) { + cJSON *eventType = cJSON_GetObjectItemCaseSensitive(valueJson, "EventType"); + if (eventType == nullptr || !cJSON_IsNumber(eventType)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } std::shared_ptr event = std::make_shared(); - event->eventType_ = valueJson["EventType"].asInt(); - - if (!valueJson.isMember("EventResult") || !valueJson["EventResult"].isInt()) { + event->eventType_ = eventType->valueint; + cJSON *eventResult = cJSON_GetObjectItemCaseSensitive(valueJson, "EventResult"); + if (eventResult == nullptr || !cJSON_IsNumber(eventResult)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - event->eventResult_ = valueJson["EventResult"].asInt(); - - if (!valueJson.isMember("EventContent") || !valueJson["EventContent"].isString()) { + event->eventResult_ = eventResult->valueint; + cJSON *eventContent = cJSON_GetObjectItemCaseSensitive(valueJson, "EventContent"); + if (eventContent == nullptr || !cJSON_IsString(eventContent) || (eventContent->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - event->eventContent_ = valueJson["EventContent"].asString(); + event->eventContent_ = eventContent->valuestring; value_ = event; + cJSON_Delete(rootValue); return DCAMERA_OK; } } // namespace DistributedHardware diff --git a/services/cameraservice/base/src/dcamera_info_cmd.cpp b/services/cameraservice/base/src/dcamera_info_cmd.cpp index c785b323..6b2288d5 100644 --- a/services/cameraservice/base/src/dcamera_info_cmd.cpp +++ b/services/cameraservice/base/src/dcamera_info_cmd.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,8 +14,7 @@ */ #include "dcamera_info_cmd.h" - -#include "json/json.h" +#include "cJSON.h" #include "distributed_camera_constants.h" #include "distributed_camera_errno.h" @@ -25,58 +24,72 @@ namespace OHOS { namespace DistributedHardware { int32_t DCameraInfoCmd::Marshal(std::string& jsonStr) { - Json::Value rootValue; - rootValue["Type"] = Json::Value(type_); - rootValue["dhId"] = Json::Value(dhId_); - rootValue["Command"] = Json::Value(command_); - - Json::Value info; - info["State"] = Json::Value(value_->state_); - rootValue["Value"] = info; + cJSON *rootValue = cJSON_CreateObject(); + if (rootValue == nullptr) { + return DCAMERA_BAD_VALUE; + } + cJSON_AddStringToObject(rootValue, "Type", type_.c_str()); + cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str()); + cJSON_AddStringToObject(rootValue, "Command", command_.c_str()); - jsonStr = rootValue.toStyledString(); + cJSON *info = cJSON_CreateObject(); + if (info == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + cJSON_AddNumberToObject(info, "State", value_->state_); + cJSON_AddItemToObject(rootValue, "Value", info); + + char *jsonstr = cJSON_Print(rootValue); + if (jsonstr == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + jsonStr = jsonstr; + cJSON_Delete(rootValue); + cJSON_free(jsonstr); return DCAMERA_OK; } int32_t DCameraInfoCmd::Unmarshal(const std::string& jsonStr) { - JSONCPP_STRING errs; - Json::CharReaderBuilder readerBuilder; - Json::Value rootValue; - - std::unique_ptr const jsonReader(readerBuilder.newCharReader()); - if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) || - !rootValue.isObject()) { + cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); + if (rootValue == nullptr) { return DCAMERA_BAD_VALUE; } - - if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) { + cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type"); + if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - type_ = rootValue["Type"].asString(); - - if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) { + type_ = type->valuestring; + cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId"); + if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - dhId_ = rootValue["dhId"].asString(); - - if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) { + dhId_ = dhId->valuestring; + cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command"); + if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - command_ = rootValue["Command"].asString(); - - if (!rootValue.isMember("Value") || !rootValue["Value"].isObject()) { + command_ = command->valuestring; + cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value"); + if (valueJson == nullptr || !cJSON_IsObject(valueJson)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - Json::Value valueJson = rootValue["Value"]; - - if (!valueJson.isMember("State") || !valueJson["State"].isInt()) { + cJSON *state = cJSON_GetObjectItemCaseSensitive(valueJson, "State"); + if (state == nullptr || !cJSON_IsNumber(state)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } std::shared_ptr info = std::make_shared(); - info->state_ = valueJson["State"].asInt(); + info->state_ = state->valueint; value_ = info; + cJSON_Delete(rootValue); return DCAMERA_OK; } } // namespace DistributedHardware diff --git a/services/cameraservice/base/src/dcamera_metadata_setting_cmd.cpp b/services/cameraservice/base/src/dcamera_metadata_setting_cmd.cpp index 2599cd8d..b3706557 100644 --- a/services/cameraservice/base/src/dcamera_metadata_setting_cmd.cpp +++ b/services/cameraservice/base/src/dcamera_metadata_setting_cmd.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,8 +14,7 @@ */ #include "dcamera_metadata_setting_cmd.h" - -#include "json/json.h" +#include "cJSON.h" #include "distributed_camera_constants.h" #include "distributed_camera_errno.h" @@ -25,68 +24,89 @@ namespace OHOS { namespace DistributedHardware { int32_t DCameraMetadataSettingCmd::Marshal(std::string& jsonStr) { - Json::Value rootValue; - rootValue["Type"] = Json::Value(type_); - rootValue["dhId"] = Json::Value(dhId_); - rootValue["Command"] = Json::Value(command_); + cJSON *rootValue = cJSON_CreateObject(); + if (rootValue == nullptr) { + return DCAMERA_BAD_VALUE; + } + cJSON_AddStringToObject(rootValue, "Type", type_.c_str()); + cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str()); + cJSON_AddStringToObject(rootValue, "Command", command_.c_str()); - Json::Value settings; + cJSON *settings = cJSON_CreateArray(); + if (settings == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + cJSON_AddItemToObject(rootValue, "Value", settings); for (auto iter = value_.begin(); iter != value_.end(); iter++) { - Json::Value setting; - setting["SettingType"] = (*iter)->type_; - setting["SettingValue"] = (*iter)->value_; - settings.append(setting); + cJSON *setting = cJSON_CreateObject(); + if (setting == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + cJSON_AddNumberToObject(setting, "SettingType", (*iter)->type_); + cJSON_AddStringToObject(setting, "SettingValue", (*iter)->value_.c_str()); + cJSON_AddItemToArray(settings, setting); } - - rootValue["Value"] = settings; - jsonStr = rootValue.toStyledString(); + + char *jsonstr = cJSON_Print(rootValue); + if (jsonstr == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + jsonStr = jsonstr; + cJSON_Delete(rootValue); + cJSON_free(jsonstr); return DCAMERA_OK; } int32_t DCameraMetadataSettingCmd::Unmarshal(const std::string& jsonStr) { - JSONCPP_STRING errs; - Json::CharReaderBuilder readerBuilder; - Json::Value rootValue; - - std::unique_ptr const jsonReader(readerBuilder.newCharReader()); - if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) || - !rootValue.isObject()) { + cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); + if (rootValue == nullptr) { return DCAMERA_BAD_VALUE; } - - if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) { + cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type"); + if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - type_ = rootValue["Type"].asString(); - - if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) { + type_ = type->valuestring; + cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId"); + if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - dhId_ = rootValue["dhId"].asString(); - - if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) { + dhId_ = dhId->valuestring; + cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command"); + if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - command_ = rootValue["Command"].asString(); - - if (!rootValue.isMember("Value") || !rootValue["Value"].isArray()) { + command_ = command->valuestring; + cJSON *settings = cJSON_GetObjectItemCaseSensitive(rootValue, "Value"); + if (settings == nullptr || !cJSON_IsArray(settings) || cJSON_GetArraySize(settings) == 0) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - - for (Json::ArrayIndex i = 0; i < rootValue["Value"].size(); i++) { - Json::Value valueJsonEle = rootValue["Value"][i]; - if (!valueJsonEle.isMember("SettingType") || !valueJsonEle["SettingType"].isInt()) { + cJSON *subSetting = nullptr; + cJSON_ArrayForEach(subSetting, settings) { + cJSON *settingType = cJSON_GetObjectItemCaseSensitive(subSetting, "SettingType"); + cJSON *settingValue = cJSON_GetObjectItemCaseSensitive(subSetting, "SettingValue"); + if (settingType == nullptr || !cJSON_IsNumber(settingType)){ + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - if (!valueJsonEle.isMember("SettingValue") || !valueJsonEle["SettingValue"].isString()) { + if (settingValue == nullptr || !cJSON_IsString(settingValue) || (settingValue->valuestring == nullptr)){ + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } std::shared_ptr setting = std::make_shared(); - setting->type_ = (DCSettingsType)valueJsonEle["SettingType"].asInt(); - setting->value_ = valueJsonEle["SettingValue"].asString(); + setting->type_ = (DCSettingsType)settingValue->valueint; + setting->value_ = settingValue->valuestring; value_.push_back(setting); } + cJSON_Delete(rootValue); return DCAMERA_OK; } } // namespace DistributedHardware diff --git a/services/cameraservice/base/src/dcamera_open_info_cmd.cpp b/services/cameraservice/base/src/dcamera_open_info_cmd.cpp index ce88c25f..f098b033 100644 --- a/services/cameraservice/base/src/dcamera_open_info_cmd.cpp +++ b/services/cameraservice/base/src/dcamera_open_info_cmd.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,8 +14,7 @@ */ #include "dcamera_open_info_cmd.h" - -#include "json/json.h" +#include "cJSON.h" #include "distributed_camera_constants.h" #include "distributed_camera_errno.h" @@ -25,57 +24,71 @@ namespace OHOS { namespace DistributedHardware { int32_t DCameraOpenInfoCmd::Marshal(std::string& jsonStr) { - Json::Value rootValue; - rootValue["Type"] = Json::Value(type_); - rootValue["dhId"] = Json::Value(dhId_); - rootValue["Command"] = Json::Value(command_); - - Json::Value openInfo; - openInfo["SourceDevId"] = Json::Value(value_->sourceDevId_); - rootValue["Value"] = openInfo; + cJSON *rootValue = cJSON_CreateObject(); + if (rootValue == nullptr) { + return DCAMERA_BAD_VALUE; + } + cJSON_AddStringToObject(rootValue, "Type", type_.c_str()); + cJSON_AddStringToObject(rootValue, "dhId", dhId_.c_str()); + cJSON_AddStringToObject(rootValue, "Command", command_.c_str()); - jsonStr = rootValue.toStyledString(); + cJSON *openInfo = cJSON_CreateObject(); + if (openInfo == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + cJSON_AddStringToObject(openInfo, "SourceDevId", value_->sourceDevId_.c_str()); + cJSON_AddItemToObject(rootValue, "Value", openInfo); + + char *jsonstr = cJSON_Print(rootValue); + if (jsonstr == nullptr) { + cJSON_Delete(rootValue); + return DCAMERA_BAD_VALUE; + } + jsonStr = jsonstr; + cJSON_Delete(rootValue); + cJSON_free(jsonstr); return DCAMERA_OK; } int32_t DCameraOpenInfoCmd::Unmarshal(const std::string& jsonStr) { - JSONCPP_STRING errs; - Json::CharReaderBuilder readerBuilder; - Json::Value rootValue; - - std::unique_ptr const jsonReader(readerBuilder.newCharReader()); - if (!jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &rootValue, &errs) || - !rootValue.isObject()) { + cJSON *rootValue = cJSON_Parse(jsonStr.c_str()); + if (rootValue == nullptr) { return DCAMERA_BAD_VALUE; } - - if (!rootValue.isMember("Type") || !rootValue["Type"].isString()) { + cJSON *type = cJSON_GetObjectItemCaseSensitive(rootValue, "Type"); + if (type == nullptr || !cJSON_IsString(type) || (type->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - type_ = rootValue["Type"].asString(); - - if (!rootValue.isMember("dhId") || !rootValue["dhId"].isString()) { + type_ = type->valuestring; + cJSON *dhId = cJSON_GetObjectItemCaseSensitive(rootValue, "dhId"); + if (dhId == nullptr || !cJSON_IsString(dhId) || (dhId->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - dhId_ = rootValue["dhId"].asString(); - - if (!rootValue.isMember("Command") || !rootValue["Command"].isString()) { + dhId_ = dhId->valuestring; + cJSON *command = cJSON_GetObjectItemCaseSensitive(rootValue, "Command"); + if (command == nullptr || !cJSON_IsString(command) || (command->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - command_ = rootValue["Command"].asString(); - - if (!rootValue.isMember("Value") || !rootValue["Value"].isObject()) { + command_ = command->valuestring; + cJSON *valueJson = cJSON_GetObjectItemCaseSensitive(rootValue, "Value"); + if (valueJson == nullptr || !cJSON_IsObject(valueJson)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } - Json::Value valueJson = rootValue["Value"]; - - if (!valueJson.isMember("SourceDevId") || !valueJson["SourceDevId"].isString()) { + cJSON *sourceDevId = cJSON_GetObjectItemCaseSensitive(valueJson, "SourceDevId"); + if (sourceDevId == nullptr || !cJSON_IsString(sourceDevId) || (sourceDevId->valuestring == nullptr)) { + cJSON_Delete(rootValue); return DCAMERA_BAD_VALUE; } std::shared_ptr openInfo = std::make_shared(); - openInfo->sourceDevId_ = valueJson["SourceDevId"].asString(); + openInfo->sourceDevId_ = sourceDevId->valuestring; value_ = openInfo; + cJSON_Delete(rootValue); return DCAMERA_OK; } } // namespace DistributedHardware diff --git a/services/cameraservice/sinkservice/BUILD.gn b/services/cameraservice/sinkservice/BUILD.gn index 9b4feabe..758d5a0c 100644 --- a/services/cameraservice/sinkservice/BUILD.gn +++ b/services/cameraservice/sinkservice/BUILD.gn @@ -96,6 +96,7 @@ ohos_shared_library("distributed_camera_sink") { "${services_path}/cameraservice/cameraoperator/handler:distributed_camera_handler", "${services_path}/channel:distributed_camera_channel", "${services_path}/data_process:distributed_camera_data_process", + "//third_party/cJSON:cjson", "//third_party/jsoncpp:jsoncpp", ] diff --git a/services/cameraservice/sourceservice/BUILD.gn b/services/cameraservice/sourceservice/BUILD.gn index 5ecc69ef..a7784e7c 100644 --- a/services/cameraservice/sourceservice/BUILD.gn +++ b/services/cameraservice/sourceservice/BUILD.gn @@ -96,6 +96,7 @@ ohos_shared_library("distributed_camera_source") { "${common_path}:distributed_camera_utils", "${services_path}/channel:distributed_camera_channel", "${services_path}/data_process:distributed_camera_data_process", + "//third_party/cJSON:cjson", "//third_party/jsoncpp:jsoncpp", ] -- Gitee