1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15#include "dm_kv_info.h" 16 17#include "cJSON.h" 18#include "dm_anonymous.h" 19#include "dm_constants.h" 20 21namespace OHOS { 22namespace DistributedHardware { 23void ConvertDmKVValueToJson(const DmKVValue &kvValue, std::string &result) 24{ 25 nlohmann::json jsonObj; 26 jsonObj[UDID_HASH_KEY] = kvValue.udidHash; 27 jsonObj[APP_ID_KEY] = kvValue.appID; 28 jsonObj[ANOY_DEVICE_ID_KEY] = kvValue.anoyDeviceId; 29 jsonObj[SALT_KEY] = kvValue.salt; 30 jsonObj[LAST_MODIFY_TIME_KEY] = kvValue.lastModifyTime; 31 result = jsonObj.dump(); 32} 33 34void ConvertJsonToDmKVValue(const std::string &result, DmKVValue &kvValue) 35{ 36 if (result.empty()) { 37 return; 38 } 39 nlohmann::json resultJson = nlohmann::json::parse(result, nullptr, false); 40 if (resultJson.is_discarded()) { 41 return; 42 } 43 if (IsString(resultJson, UDID_HASH_KEY)) { 44 kvValue.udidHash = resultJson[UDID_HASH_KEY].get<std::string>(); 45 } 46 if (IsString(resultJson, APP_ID_KEY)) { 47 kvValue.appID = resultJson[APP_ID_KEY].get<std::string>(); 48 } 49 if (IsString(resultJson, ANOY_DEVICE_ID_KEY)) { 50 kvValue.anoyDeviceId = resultJson[ANOY_DEVICE_ID_KEY].get<std::string>(); 51 } 52 if (IsString(resultJson, SALT_KEY)) { 53 kvValue.salt = resultJson[SALT_KEY].get<std::string>(); 54 } 55 if (IsInt64(resultJson, LAST_MODIFY_TIME_KEY)) { 56 kvValue.lastModifyTime = resultJson[LAST_MODIFY_TIME_KEY].get<int64_t>(); 57 } 58} 59} // namespace DistributedHardware 60} // namespace OHOS 61