• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
16 #include "inner_patch_info.h"
17 #include "app_log_tag_wrapper.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
21 
22 using json = nlohmann::json;
23 namespace {
24 constexpr const char* VERSION_CODE = "versionCode";
25 constexpr const char* APP_PATCH_TYPE = "appPatchType";
26 }  // namespace
27 
FromJson(const std::string & jsonObject)28 bool InnerPatchInfo::FromJson(const std::string &jsonObject)
29 {
30     if (jsonObject.empty() || !json::accept(jsonObject)) {
31         APP_LOGE("invalid param");
32         return false;
33     }
34     json jsonObj = json::parse(jsonObject, nullptr, false);
35     if (jsonObj.is_discarded()) {
36         APP_LOGE("jsonObj discarded");
37         return false;
38     }
39     if (!jsonObj.contains(VERSION_CODE) || !jsonObj.contains(APP_PATCH_TYPE)) {
40         APP_LOGE("json object missing required failed");
41         return false;
42     }
43     if (!jsonObj[VERSION_CODE].is_number() || !jsonObj[APP_PATCH_TYPE].is_number()) {
44         APP_LOGE("json object value data type failed");
45         return false;
46     }
47     patchInfo_.versionCode = static_cast<uint32_t>(jsonObj[VERSION_CODE].get<int>());
48     patchInfo_.appPatchType = static_cast<AppPatchType>(jsonObj[APP_PATCH_TYPE].get<int>());
49     return true;
50 }
51 
ToString() const52 std::string InnerPatchInfo::ToString() const
53 {
54     nlohmann::json jsonObj;
55     ToJson(jsonObj);
56     return jsonObj.dump();
57 }
58 
ToJson(nlohmann::json & jsonObject) const59 void InnerPatchInfo::ToJson(nlohmann::json &jsonObject) const
60 {
61     jsonObject[VERSION_CODE] = patchInfo_.versionCode;
62     jsonObject[APP_PATCH_TYPE] = patchInfo_.appPatchType;
63 }
64 }  // namespace AppExecFwk
65 }  // namespace OHOS
66