1e0e9324cSopenharmony_ci/* 2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License. 5e0e9324cSopenharmony_ci * You may obtain a copy of the License at 6e0e9324cSopenharmony_ci * 7e0e9324cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e0e9324cSopenharmony_ci * 9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and 13e0e9324cSopenharmony_ci * limitations under the License. 14e0e9324cSopenharmony_ci */ 15e0e9324cSopenharmony_ci 16e0e9324cSopenharmony_ci#include "json_parser.h" 17e0e9324cSopenharmony_ci#include <errors.h> 18e0e9324cSopenharmony_ci#include <fstream> 19e0e9324cSopenharmony_ci#include <memory> 20e0e9324cSopenharmony_ci#include <unistd.h> 21e0e9324cSopenharmony_ci#include "common/common_macro.h" 22e0e9324cSopenharmony_ci#include "media_log.h" 23e0e9324cSopenharmony_ci 24e0e9324cSopenharmony_cinamespace OHOS { 25e0e9324cSopenharmony_cinamespace Sharing { 26e0e9324cSopenharmony_ciint32_t JsonParser::GetConfig(std::string &fileName, SharingData::Ptr &value) 27e0e9324cSopenharmony_ci{ 28e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 29e0e9324cSopenharmony_ci Json::Value root; 30e0e9324cSopenharmony_ci std::ifstream ifs; 31e0e9324cSopenharmony_ci ifs.open(fileName.c_str()); 32e0e9324cSopenharmony_ci 33e0e9324cSopenharmony_ci if (!ifs.is_open()) { 34e0e9324cSopenharmony_ci SHARING_LOGE("cannot open %{public}s.", fileName.c_str()); 35e0e9324cSopenharmony_ci return -1; 36e0e9324cSopenharmony_ci } 37e0e9324cSopenharmony_ci 38e0e9324cSopenharmony_ci Json::CharReaderBuilder builder; 39e0e9324cSopenharmony_ci builder["collectComments"] = false; 40e0e9324cSopenharmony_ci JSONCPP_STRING errs; 41e0e9324cSopenharmony_ci 42e0e9324cSopenharmony_ci if (!parseFromStream(builder, ifs, &root, &errs)) { 43e0e9324cSopenharmony_ci SHARING_LOGE("parse file error: %{public}s.", errs.c_str()); 44e0e9324cSopenharmony_ci return -1; 45e0e9324cSopenharmony_ci } 46e0e9324cSopenharmony_ci 47e0e9324cSopenharmony_ci SHARING_LOGD("parse json:\n%{public}s.", root.toStyledString().c_str()); 48e0e9324cSopenharmony_ci for (auto &modules : root) { 49e0e9324cSopenharmony_ci ReadModuleConfig(modules, value); 50e0e9324cSopenharmony_ci } 51e0e9324cSopenharmony_ci 52e0e9324cSopenharmony_ci ifs.close(); 53e0e9324cSopenharmony_ci return 0; 54e0e9324cSopenharmony_ci} 55e0e9324cSopenharmony_ci 56e0e9324cSopenharmony_ciint32_t JsonParser::SaveConfig(std::string &fileName, SharingData::Ptr &value) 57e0e9324cSopenharmony_ci{ 58e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 59e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(value); 60e0e9324cSopenharmony_ci std::ofstream ofs; 61e0e9324cSopenharmony_ci ofs.open(fileName.c_str(), std::ios::out | std::ios::trunc | std::ios::binary); 62e0e9324cSopenharmony_ci 63e0e9324cSopenharmony_ci if (!ofs.is_open()) { 64e0e9324cSopenharmony_ci SHARING_LOGE("open file %{public}s err.", fileName.c_str()); 65e0e9324cSopenharmony_ci return -1; 66e0e9324cSopenharmony_ci } 67e0e9324cSopenharmony_ci 68e0e9324cSopenharmony_ci Json::Value root(Json::ValueType::objectValue); 69e0e9324cSopenharmony_ci Json::StreamWriterBuilder builder; 70e0e9324cSopenharmony_ci builder["commentStyle"] = "All"; 71e0e9324cSopenharmony_ci Json::StreamWriter *writer(builder.newStreamWriter()); 72e0e9324cSopenharmony_ci 73e0e9324cSopenharmony_ci value->ForEach([&](const std::string &moduleName, const SharingDataGroupByModule::Ptr &moduleValue) { 74e0e9324cSopenharmony_ci Json::Value moduleArray(Json::ValueType::arrayValue); 75e0e9324cSopenharmony_ci SaveModuleConfig(moduleArray, moduleValue); 76e0e9324cSopenharmony_ci root[moduleName] = moduleArray; 77e0e9324cSopenharmony_ci }); 78e0e9324cSopenharmony_ci 79e0e9324cSopenharmony_ci MEDIA_LOGD("save json:\n%{public}s.", root.toStyledString().c_str()); 80e0e9324cSopenharmony_ci writer->write(root, &ofs); 81e0e9324cSopenharmony_ci ofs.close(); 82e0e9324cSopenharmony_ci return 0; 83e0e9324cSopenharmony_ci} 84e0e9324cSopenharmony_ci 85e0e9324cSopenharmony_ciint32_t JsonParser::ReadModuleConfig(Json::Value &modules, SharingData::Ptr &value) 86e0e9324cSopenharmony_ci{ 87e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 88e0e9324cSopenharmony_ci if (modules.empty()) { 89e0e9324cSopenharmony_ci SHARING_LOGE("this module is empty."); 90e0e9324cSopenharmony_ci return -1; 91e0e9324cSopenharmony_ci } 92e0e9324cSopenharmony_ci 93e0e9324cSopenharmony_ci auto moduleNames = modules.getMemberNames(); 94e0e9324cSopenharmony_ci for (auto &moduleName : moduleNames) { 95e0e9324cSopenharmony_ci MEDIA_LOGD("take down %{public}s: %{public}s.", moduleName.c_str(), 96e0e9324cSopenharmony_ci modules[moduleName].toStyledString().c_str()); 97e0e9324cSopenharmony_ci auto moduleValue = std::make_shared<SharingDataGroupByModule>(moduleName); 98e0e9324cSopenharmony_ci auto module = modules[moduleName]; 99e0e9324cSopenharmony_ci for (auto &item : module) { 100e0e9324cSopenharmony_ci auto tag = item["tag"].asString(); 101e0e9324cSopenharmony_ci auto tagValue = std::make_shared<SharingDataGroupByTag>(tag); 102e0e9324cSopenharmony_ci moduleValue->PutSharingValues(tag, tagValue); 103e0e9324cSopenharmony_ci auto keyNames = item.getMemberNames(); 104e0e9324cSopenharmony_ci for (auto &key : keyNames) { 105e0e9324cSopenharmony_ci if (key == "tag") { 106e0e9324cSopenharmony_ci continue; 107e0e9324cSopenharmony_ci } 108e0e9324cSopenharmony_ci if (key == "isEnable" && tag == "mediaLog") { 109e0e9324cSopenharmony_ci g_logOn = item[key].asBool(); 110e0e9324cSopenharmony_ci } 111e0e9324cSopenharmony_ci SharingValue::Ptr sharingData = nullptr; 112e0e9324cSopenharmony_ci if (item[key].isString()) { 113e0e9324cSopenharmony_ci std::string value = item[key].asString(); 114e0e9324cSopenharmony_ci sharingData = std::make_shared<SharingValue>(value); 115e0e9324cSopenharmony_ci } else if (item[key].isInt()) { 116e0e9324cSopenharmony_ci int32_t value = item[key].asInt(); 117e0e9324cSopenharmony_ci sharingData = std::make_shared<SharingValue>(value); 118e0e9324cSopenharmony_ci } else if (item[key].isBool()) { 119e0e9324cSopenharmony_ci bool value = item[key].asBool(); 120e0e9324cSopenharmony_ci sharingData = std::make_shared<SharingValue>(value); 121e0e9324cSopenharmony_ci } else if (item[key].isArray()) { 122e0e9324cSopenharmony_ci std::vector<int32_t> value; 123e0e9324cSopenharmony_ci for (auto &it : item[key]) { 124e0e9324cSopenharmony_ci value.emplace_back(it.asInt()); 125e0e9324cSopenharmony_ci } 126e0e9324cSopenharmony_ci sharingData = std::make_shared<SharingValue>(value); 127e0e9324cSopenharmony_ci } 128e0e9324cSopenharmony_ci tagValue->PutSharingValue(key, sharingData); 129e0e9324cSopenharmony_ci } 130e0e9324cSopenharmony_ci } 131e0e9324cSopenharmony_ci value->PutSharingValues(moduleValue, moduleName); 132e0e9324cSopenharmony_ci } 133e0e9324cSopenharmony_ci 134e0e9324cSopenharmony_ci return 0; 135e0e9324cSopenharmony_ci} 136e0e9324cSopenharmony_ci 137e0e9324cSopenharmony_ciint32_t JsonParser::SaveModuleConfig(Json::Value &module, const SharingDataGroupByModule::Ptr &moduleValue) 138e0e9324cSopenharmony_ci{ 139e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 140e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(moduleValue); 141e0e9324cSopenharmony_ci moduleValue->ForEach([&](const std::string &tagName, const SharingDataGroupByTag::Ptr &tagValue) { 142e0e9324cSopenharmony_ci Json::Value tagObject(Json::ValueType::objectValue); 143e0e9324cSopenharmony_ci tagObject["tag"] = tagName; 144e0e9324cSopenharmony_ci tagValue->ForEach([&](const std::string &key, const SharingValue::Ptr &value) { 145e0e9324cSopenharmony_ci if (value->IsBool()) { 146e0e9324cSopenharmony_ci bool data; 147e0e9324cSopenharmony_ci if (value->GetValue(data)) { 148e0e9324cSopenharmony_ci tagObject[key] = data; 149e0e9324cSopenharmony_ci } 150e0e9324cSopenharmony_ci } else if (value->IsInt32()) { 151e0e9324cSopenharmony_ci int32_t data; 152e0e9324cSopenharmony_ci if (value->GetValue(data)) { 153e0e9324cSopenharmony_ci tagObject[key] = data; 154e0e9324cSopenharmony_ci } 155e0e9324cSopenharmony_ci } else if (value->IsString()) { 156e0e9324cSopenharmony_ci std::string data; 157e0e9324cSopenharmony_ci if (value->GetValue(data)) { 158e0e9324cSopenharmony_ci tagObject[key] = data; 159e0e9324cSopenharmony_ci } 160e0e9324cSopenharmony_ci } else if (value->IsVector()) { 161e0e9324cSopenharmony_ci std::vector<int32_t> data; 162e0e9324cSopenharmony_ci if (value->GetValue(data)) { 163e0e9324cSopenharmony_ci Json::Value vec(Json::ValueType::arrayValue); 164e0e9324cSopenharmony_ci for (auto &item : data) { 165e0e9324cSopenharmony_ci vec.append(item); 166e0e9324cSopenharmony_ci } 167e0e9324cSopenharmony_ci tagObject[key] = vec; 168e0e9324cSopenharmony_ci } 169e0e9324cSopenharmony_ci } 170e0e9324cSopenharmony_ci }); 171e0e9324cSopenharmony_ci module.append(tagObject); 172e0e9324cSopenharmony_ci }); 173e0e9324cSopenharmony_ci 174e0e9324cSopenharmony_ci return 0; 175e0e9324cSopenharmony_ci} 176e0e9324cSopenharmony_ci} // namespace Sharing 177e0e9324cSopenharmony_ci} // namespace OHOS