1 /*
2 * Copyright (c) 2022 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 "dispatch_info.h"
17
18 #include "app_log_wrapper.h"
19 #include "json_util.h"
20 #include "nlohmann/json.hpp"
21 #include "parcel_macro.h"
22 #include "string_ex.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string JSON_KEY_VERSION = "version";
28 } // namespace
29
to_json(nlohmann::json & jsonObject,const DispatcherInfo & dispatcherInfo)30 void to_json(nlohmann::json &jsonObject, const DispatcherInfo &dispatcherInfo)
31 {
32 jsonObject = nlohmann::json {
33 {JSON_KEY_VERSION, dispatcherInfo.version}
34 };
35 }
36
from_json(const nlohmann::json & jsonObject,DispatcherInfo & dispatcherInfo)37 void from_json(const nlohmann::json &jsonObject, DispatcherInfo &dispatcherInfo)
38 {
39 const auto &jsonObjectEnd = jsonObject.end();
40 int32_t parseResult = ERR_OK;
41 GetValueIfFindKey<std::string>(jsonObject,
42 jsonObjectEnd,
43 JSON_KEY_VERSION,
44 dispatcherInfo.version,
45 JsonType::STRING,
46 false,
47 parseResult,
48 ArrayType::NOT_ARRAY);
49 if (parseResult != ERR_OK) {
50 APP_LOGE("read module dispatcherInfo from jsonObject error, error code : %{public}d", parseResult);
51 }
52 }
53
ReadFromParcel(Parcel & parcel)54 bool DispatcherInfo::ReadFromParcel(Parcel &parcel)
55 {
56 version = Str16ToStr8(parcel.ReadString16());
57 return true;
58 }
59
Marshalling(Parcel & parcel) const60 bool DispatcherInfo::Marshalling(Parcel &parcel) const
61 {
62 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(version));
63 return true;
64 }
65
Unmarshalling(Parcel & parcel)66 DispatcherInfo *DispatcherInfo::Unmarshalling(Parcel &parcel)
67 {
68 DispatcherInfo *dispatcherInfo = new (std::nothrow) DispatcherInfo();
69 if (dispatcherInfo && !dispatcherInfo->ReadFromParcel(parcel)) {
70 APP_LOGE("read from parcel failed");
71 delete dispatcherInfo;
72 dispatcherInfo = nullptr;
73 }
74 return dispatcherInfo;
75 }
76 } // namespace AppExecFwk
77 } // namespace OHOS