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