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 GetValueIfFindKey<std::string>(jsonObject,
43 jsonObjectEnd,
44 JSON_KEY_VERSION,
45 dispatcherInfo.version,
46 JsonType::STRING,
47 false,
48 parseResult,
49 ArrayType::NOT_ARRAY);
50 if (parseResult != ERR_OK) {
51 LOG_E(BMS_TAG_DEFAULT, "read module dispatcherInfo from jsonObject error: %{public}d", parseResult);
52 }
53 }
54
ReadFromParcel(Parcel & parcel)55 bool DispatcherInfo::ReadFromParcel(Parcel &parcel)
56 {
57 version = Str16ToStr8(parcel.ReadString16());
58 return true;
59 }
60
Marshalling(Parcel & parcel) const61 bool DispatcherInfo::Marshalling(Parcel &parcel) const
62 {
63 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(version));
64 return true;
65 }
66
Unmarshalling(Parcel & parcel)67 DispatcherInfo *DispatcherInfo::Unmarshalling(Parcel &parcel)
68 {
69 DispatcherInfo *dispatcherInfo = new (std::nothrow) DispatcherInfo();
70 if (dispatcherInfo && !dispatcherInfo->ReadFromParcel(parcel)) {
71 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
72 delete dispatcherInfo;
73 dispatcherInfo = nullptr;
74 }
75 return dispatcherInfo;
76 }
77 } // namespace AppExecFwk
78 } // namespace OHOS