• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "bundle_pack_info.h"
17 
18 #include "bundle_constants.h"
19 #include "json_util.h"
20 #include "parcel_macro.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 // version
27 const char* PACK_SUMMARY_APP_VERSION_CODE = "code";
28 const char* PACK_SUMMARY_APP_VERSION_NAME = "name";
29 const char* PACK_SUMMARY_APP_VERSION_MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode";
30 
31 // app
32 const char* PACK_SUMMARY_APP_BUNDLE_VERSION = "version";
33 
34 // module ablities
35 const char* PACK_SUMMARY_MODULE_ABILITY_NAME = "name";
36 const char* PACK_SUMMARY_MODULE_ABILITY_LABEL = "label";
37 const char* PACK_SUMMARY_MODULE_ABILITY_VISIBLE = "visible";
38 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS = "forms";
39 
40 // module extensionAbilities
41 const char* PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_NAME = "name";
42 const char* PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_FORMS = "forms";
43 
44 // module ablities forms
45 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_NAME = "name";
46 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_TYPE = "type";
47 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_ENABLED = "updateEnabled";
48 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_SCHEDULE_DUPDATETIME = "scheduledUpdateTime";
49 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_MULTI_SCHEDULE_DUPDATETIME = "multiScheduledUpdateTime";
50 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_DURATION = "updateDuration";
51 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_SUPPORT_DIMENSIONS = "supportDimensions";
52 const char* PACK_SUMMARY_MODULE_ABILITY_FORMS_DEFAULT_DIMENSION = "defaultDimension";
53 
54 // module distro
55 const char* PACK_SUMMARY_MODULE_DISTRO_MODULE_TYPE = "moduleType";
56 const char* PACK_SUMMARY_MODULE_DISTRO_INSTALLATION_FREE = "installationFree";
57 const char* PACK_SUMMARY_MODULE_DISTRO_DELIVERY_WITH_INSTALL = "deliveryWithInstall";
58 
59 // module apiversion
60 const char* PACK_SUMMARY_MODULE_API_VERSION_COMPATIBLE = "compatible";
61 const char* PACK_SUMMARY_MODULE_API_VERSION_RELEASE_TYPE = "releaseType";
62 const char* PACK_SUMMARY_MODULE_API_VERSION_TARGET = "target";
63 
64 // package module
65 const char* PACK_SUMMARY_MODULE_MAIN_ABILITY = "mainAbility";
66 const char* PACK_SUMMARY_MODULE_DEVICE_TYPE = "deviceType";
67 const char* PACK_SUMMARY_MODULE_ABILITIES = "abilities";
68 const char* PACK_SUMMARY_MODULE_DISTRO = "distro";
69 const char* PACK_SUMMARY_MODULE_API_VERSION = "apiVersion";
70 const char* PACK_SUMMARY_MODULE_EXTENSION_ABILITIES = "extensionAbilities";
71 
72 // summary
73 const char* PACK_SUMMARY_APP = "app";
74 const char* PACK_SUMMARY_MODULE = "modules";
75 
76 // packages
77 const char* PACK_PACKAGES_DEVICE_TYPE = "deviceType";
78 const char* PACK_PACKAGES_MODULE_TYPE = "moduleType";
79 const char* PACK_PACKAGES_DELIVERY_WITH_INSTALL = "deliveryWithInstall";
80 const char* PACK_PACKAGES_NAME = "name";
81 
82 // bundle pack info
83 const char* BUNDLE_PACK_INFO_SUMMARY = "summary";
84 const char* BUNDLE_PACK_INFO_PACKAGES = "packages";
85 
86 
87 } // namespace
88 
to_json(nlohmann::json & jsonObject,const Version & version)89 void to_json(nlohmann::json &jsonObject, const Version &version)
90 {
91     jsonObject = nlohmann::json {
92         {PACK_SUMMARY_APP_VERSION_CODE, version.code},
93         {PACK_SUMMARY_APP_VERSION_NAME, version.name},
94         {PACK_SUMMARY_APP_VERSION_MIN_COMPATIBLE_VERSION_CODE, version.minCompatibleVersionCode}
95     };
96 }
97 
from_json(const nlohmann::json & jsonObject,Version & version)98 void from_json(const nlohmann::json &jsonObject, Version &version)
99 {
100     const auto &jsonObjectEnd = jsonObject.end();
101     int32_t parseResult = ERR_OK;
102     GetValueIfFindKey<uint32_t>(jsonObject,
103         jsonObjectEnd,
104         PACK_SUMMARY_APP_VERSION_CODE,
105         version.code,
106         JsonType::NUMBER,
107         false,
108         parseResult,
109         ArrayType::NOT_ARRAY);
110     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
111         jsonObjectEnd,
112         PACK_SUMMARY_APP_VERSION_NAME,
113         version.name,
114         false,
115         parseResult);
116     GetValueIfFindKey<uint32_t>(jsonObject,
117         jsonObjectEnd,
118         PACK_SUMMARY_APP_VERSION_MIN_COMPATIBLE_VERSION_CODE,
119         version.minCompatibleVersionCode,
120         JsonType::NUMBER,
121         false,
122         parseResult,
123         ArrayType::NOT_ARRAY);
124     if (parseResult != ERR_OK) {
125         APP_LOGE("read version error %{public}d", parseResult);
126     }
127 }
128 
to_json(nlohmann::json & jsonObject,const PackageApp & app)129 void to_json(nlohmann::json &jsonObject, const PackageApp &app)
130 {
131     jsonObject = nlohmann::json {
132         {Constants::BUNDLE_NAME, app.bundleName},
133         {PACK_SUMMARY_APP_BUNDLE_VERSION, app.version}
134     };
135 }
136 
from_json(const nlohmann::json & jsonObject,PackageApp & app)137 void from_json(const nlohmann::json &jsonObject, PackageApp &app)
138 {
139     const auto &jsonObjectEnd = jsonObject.end();
140     int32_t parseResult = ERR_OK;
141     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
142         jsonObjectEnd,
143         Constants::BUNDLE_NAME,
144         app.bundleName,
145         false,
146         parseResult);
147     GetValueIfFindKey<Version>(jsonObject,
148         jsonObjectEnd,
149         PACK_SUMMARY_APP_BUNDLE_VERSION,
150         app.version,
151         JsonType::OBJECT,
152         false,
153         parseResult,
154         ArrayType::NOT_ARRAY);
155     if (parseResult != ERR_OK) {
156         APP_LOGE("read package app error %{public}d", parseResult);
157     }
158 }
159 
to_json(nlohmann::json & jsonObject,const ExtensionAbilities & extensionAbilities)160 void to_json(nlohmann::json &jsonObject, const ExtensionAbilities &extensionAbilities)
161 {
162     jsonObject = nlohmann::json {
163         {PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_NAME, extensionAbilities.name},
164         {PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_FORMS, extensionAbilities.forms}
165     };
166 }
167 
from_json(const nlohmann::json & jsonObject,ExtensionAbilities & extensionAbilities)168 void from_json(const nlohmann::json &jsonObject, ExtensionAbilities &extensionAbilities)
169 {
170     const auto &jsonObjectEnd = jsonObject.end();
171     int32_t parseResult = ERR_OK;
172     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
173         jsonObjectEnd,
174         PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_NAME,
175         extensionAbilities.name,
176         false,
177         parseResult);
178     GetValueIfFindKey<std::vector<AbilityFormInfo>>(jsonObject,
179         jsonObjectEnd,
180         PACK_SUMMARY_MODULE_EXTENSION_ABILITIES_FORMS,
181         extensionAbilities.forms,
182         JsonType::ARRAY,
183         false,
184         parseResult,
185         ArrayType::OBJECT);
186     if (parseResult != ERR_OK) {
187         APP_LOGE("read abilityinfo error %{public}d", parseResult);
188     }
189 }
190 
to_json(nlohmann::json & jsonObject,const ModuleAbilityInfo & abilityinfo)191 void to_json(nlohmann::json &jsonObject, const ModuleAbilityInfo &abilityinfo)
192 {
193     jsonObject = nlohmann::json {
194         {PACK_SUMMARY_MODULE_ABILITY_NAME, abilityinfo.name},
195         {PACK_SUMMARY_MODULE_ABILITY_LABEL, abilityinfo.label},
196         {PACK_SUMMARY_MODULE_ABILITY_VISIBLE, abilityinfo.visible},
197         {PACK_SUMMARY_MODULE_ABILITY_FORMS, abilityinfo.forms}
198     };
199 }
200 
from_json(const nlohmann::json & jsonObject,ModuleAbilityInfo & abilityinfo)201 void from_json(const nlohmann::json &jsonObject, ModuleAbilityInfo &abilityinfo)
202 {
203     const auto &jsonObjectEnd = jsonObject.end();
204     int32_t parseResult = ERR_OK;
205     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
206         jsonObjectEnd,
207         PACK_SUMMARY_MODULE_ABILITY_NAME,
208         abilityinfo.name,
209         false,
210         parseResult);
211     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
212         jsonObjectEnd,
213         PACK_SUMMARY_MODULE_ABILITY_LABEL,
214         abilityinfo.label,
215         false,
216         parseResult);
217     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
218         jsonObjectEnd,
219         PACK_SUMMARY_MODULE_ABILITY_VISIBLE,
220         abilityinfo.visible,
221         false,
222         parseResult);
223     GetValueIfFindKey<std::vector<AbilityFormInfo>>(jsonObject,
224         jsonObjectEnd,
225         PACK_SUMMARY_MODULE_ABILITY_FORMS,
226         abilityinfo.forms,
227         JsonType::ARRAY,
228         false,
229         parseResult,
230         ArrayType::OBJECT);
231     if (parseResult != ERR_OK) {
232         APP_LOGE("read abilityinfo error %{public}d", parseResult);
233     }
234 }
235 
to_json(nlohmann::json & jsonObject,const AbilityFormInfo & abilityFormInfo)236 void to_json(nlohmann::json &jsonObject, const AbilityFormInfo &abilityFormInfo)
237 {
238     jsonObject = nlohmann::json {
239         {PACK_SUMMARY_MODULE_ABILITY_FORMS_NAME, abilityFormInfo.name},
240         {PACK_SUMMARY_MODULE_ABILITY_FORMS_TYPE, abilityFormInfo.type},
241         {PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_ENABLED, abilityFormInfo.updateEnabled},
242         {PACK_SUMMARY_MODULE_ABILITY_FORMS_SCHEDULE_DUPDATETIME, abilityFormInfo.scheduledUpdateTime},
243         {PACK_SUMMARY_MODULE_ABILITY_FORMS_MULTI_SCHEDULE_DUPDATETIME, abilityFormInfo.multiScheduledUpdateTime},
244         {PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_DURATION, abilityFormInfo.updateDuration},
245         {PACK_SUMMARY_MODULE_ABILITY_FORMS_SUPPORT_DIMENSIONS, abilityFormInfo.supportDimensions},
246         {PACK_SUMMARY_MODULE_ABILITY_FORMS_DEFAULT_DIMENSION, abilityFormInfo.defaultDimension}};
247 }
248 
from_json(const nlohmann::json & jsonObject,AbilityFormInfo & abilityFormInfo)249 void from_json(const nlohmann::json &jsonObject, AbilityFormInfo &abilityFormInfo)
250 {
251     const auto &jsonObjectEnd = jsonObject.end();
252     int32_t parseResult = ERR_OK;
253     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_NAME,
254         abilityFormInfo.name, false, parseResult);
255     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_TYPE,
256         abilityFormInfo.type, false, parseResult);
257     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_ENABLED,
258         abilityFormInfo.updateEnabled, false, parseResult);
259     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_SCHEDULE_DUPDATETIME,
260         abilityFormInfo.scheduledUpdateTime, false, parseResult);
261     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
262         PACK_SUMMARY_MODULE_ABILITY_FORMS_MULTI_SCHEDULE_DUPDATETIME,
263         abilityFormInfo.multiScheduledUpdateTime, false, parseResult);
264     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_UPDATE_DURATION,
265         abilityFormInfo.updateDuration, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
266     GetValueIfFindKey<std::vector<std::string>>(jsonObject, jsonObjectEnd,
267         PACK_SUMMARY_MODULE_ABILITY_FORMS_SUPPORT_DIMENSIONS, abilityFormInfo.supportDimensions, JsonType::ARRAY, false,
268         parseResult, ArrayType::STRING);
269     BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, PACK_SUMMARY_MODULE_ABILITY_FORMS_DEFAULT_DIMENSION,
270         abilityFormInfo.defaultDimension, false, parseResult);
271     if (parseResult != ERR_OK) {
272         APP_LOGE("read BundleConfigInfo error %{public}d", parseResult);
273     }
274 }
275 
to_json(nlohmann::json & jsonObject,const ModuleDistro & distro)276 void to_json(nlohmann::json &jsonObject, const ModuleDistro &distro)
277 {
278     jsonObject = nlohmann::json {
279         {PACK_SUMMARY_MODULE_DISTRO_MODULE_TYPE, distro.moduleType},
280         {Constants::MODULE_NAME, distro.moduleName},
281         {PACK_SUMMARY_MODULE_DISTRO_INSTALLATION_FREE, distro.installationFree},
282         {PACK_SUMMARY_MODULE_DISTRO_DELIVERY_WITH_INSTALL, distro.deliveryWithInstall}
283     };
284 }
285 
from_json(const nlohmann::json & jsonObject,ModuleDistro & distro)286 void from_json(const nlohmann::json &jsonObject, ModuleDistro &distro)
287 {
288     const auto &jsonObjectEnd = jsonObject.end();
289     int32_t parseResult = ERR_OK;
290     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
291         jsonObjectEnd,
292         PACK_SUMMARY_MODULE_DISTRO_MODULE_TYPE,
293         distro.moduleType,
294         false,
295         parseResult);
296     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
297         jsonObjectEnd,
298         Constants::MODULE_NAME,
299         distro.moduleName,
300         false,
301         parseResult);
302     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
303         jsonObjectEnd,
304         PACK_SUMMARY_MODULE_DISTRO_INSTALLATION_FREE,
305         distro.installationFree,
306         false,
307         parseResult);
308     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
309         jsonObjectEnd,
310         PACK_SUMMARY_MODULE_DISTRO_DELIVERY_WITH_INSTALL,
311         distro.deliveryWithInstall,
312         false,
313         parseResult);
314     if (parseResult != ERR_OK) {
315         APP_LOGE("read abilityinfo error %{public}d", parseResult);
316     }
317 }
318 
to_json(nlohmann::json & jsonObject,const ApiVersion & apiVersion)319 void to_json(nlohmann::json &jsonObject, const ApiVersion &apiVersion)
320 {
321     jsonObject = nlohmann::json {
322         {PACK_SUMMARY_MODULE_API_VERSION_COMPATIBLE, apiVersion.compatible},
323         {PACK_SUMMARY_MODULE_API_VERSION_RELEASE_TYPE, apiVersion.releaseType},
324         {PACK_SUMMARY_MODULE_API_VERSION_TARGET, apiVersion.target}
325     };
326 }
327 
from_json(const nlohmann::json & jsonObject,ApiVersion & apiVersion)328 void from_json(const nlohmann::json &jsonObject, ApiVersion &apiVersion)
329 {
330     const auto &jsonObjectEnd = jsonObject.end();
331     int32_t parseResult = ERR_OK;
332     GetValueIfFindKey<uint32_t>(jsonObject,
333         jsonObjectEnd,
334         PACK_SUMMARY_MODULE_API_VERSION_COMPATIBLE,
335         apiVersion.compatible,
336         JsonType::NUMBER,
337         false,
338         parseResult,
339         ArrayType::NOT_ARRAY);
340     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
341         jsonObjectEnd,
342         PACK_SUMMARY_MODULE_API_VERSION_RELEASE_TYPE,
343         apiVersion.releaseType,
344         false,
345         parseResult);
346     GetValueIfFindKey<uint32_t>(jsonObject,
347         jsonObjectEnd,
348         PACK_SUMMARY_MODULE_API_VERSION_TARGET,
349         apiVersion.target,
350         JsonType::NUMBER,
351         false,
352         parseResult,
353         ArrayType::NOT_ARRAY);
354     if (parseResult != ERR_OK) {
355         APP_LOGE("read abilityinfo error %{public}d", parseResult);
356     }
357 }
358 
to_json(nlohmann::json & jsonObject,const PackageModule & packageModule)359 void to_json(nlohmann::json &jsonObject, const PackageModule &packageModule)
360 {
361     jsonObject = nlohmann::json {
362         {PACK_SUMMARY_MODULE_MAIN_ABILITY, packageModule.mainAbility},
363         {PACK_SUMMARY_MODULE_DEVICE_TYPE, packageModule.deviceType},
364         {PACK_SUMMARY_MODULE_ABILITIES, packageModule.abilities},
365         {PACK_SUMMARY_MODULE_EXTENSION_ABILITIES, packageModule.extensionAbilities},
366         {PACK_SUMMARY_MODULE_DISTRO, packageModule.distro},
367         {PACK_SUMMARY_MODULE_API_VERSION, packageModule.apiVersion}
368     };
369 }
370 
from_json(const nlohmann::json & jsonObject,PackageModule & packageModule)371 void from_json(const nlohmann::json &jsonObject, PackageModule &packageModule)
372 {
373     const auto &jsonObjectEnd = jsonObject.end();
374     int32_t parseResult = ERR_OK;
375     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
376         jsonObjectEnd,
377         PACK_SUMMARY_MODULE_MAIN_ABILITY,
378         packageModule.mainAbility,
379         false,
380         parseResult);
381     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
382         jsonObjectEnd,
383         PACK_SUMMARY_MODULE_DEVICE_TYPE,
384         packageModule.deviceType,
385         JsonType::ARRAY,
386         false,
387         parseResult,
388         ArrayType::STRING);
389     GetValueIfFindKey<std::vector<ModuleAbilityInfo>>(jsonObject,
390         jsonObjectEnd,
391         PACK_SUMMARY_MODULE_ABILITIES,
392         packageModule.abilities,
393         JsonType::ARRAY,
394         false,
395         parseResult,
396         ArrayType::OBJECT);
397     GetValueIfFindKey<std::vector<ExtensionAbilities>>(jsonObject,
398         jsonObjectEnd,
399         PACK_SUMMARY_MODULE_EXTENSION_ABILITIES,
400         packageModule.extensionAbilities,
401         JsonType::ARRAY,
402         false,
403         parseResult,
404         ArrayType::OBJECT);
405     GetValueIfFindKey<ModuleDistro>(jsonObject,
406         jsonObjectEnd,
407         PACK_SUMMARY_MODULE_DISTRO,
408         packageModule.distro,
409         JsonType::OBJECT,
410         false,
411         parseResult,
412         ArrayType::NOT_ARRAY);
413     GetValueIfFindKey<ApiVersion>(jsonObject,
414         jsonObjectEnd,
415         PACK_SUMMARY_MODULE_API_VERSION,
416         packageModule.apiVersion,
417         JsonType::OBJECT,
418         false,
419         parseResult,
420         ArrayType::NOT_ARRAY);
421     if (parseResult != ERR_OK) {
422         APP_LOGE("read abilityinfo error %{public}d", parseResult);
423     }
424 }
425 
to_json(nlohmann::json & jsonObject,const Summary & summary)426 void to_json(nlohmann::json &jsonObject, const Summary &summary)
427 {
428     jsonObject = nlohmann::json {
429         {PACK_SUMMARY_APP, summary.app},
430         {PACK_SUMMARY_MODULE, summary.modules}
431     };
432 }
433 
from_json(const nlohmann::json & jsonObject,Summary & summary)434 void from_json(const nlohmann::json &jsonObject, Summary &summary)
435 {
436     const auto &jsonObjectEnd = jsonObject.end();
437     int32_t parseResult = ERR_OK;
438     GetValueIfFindKey<PackageApp>(jsonObject,
439         jsonObjectEnd,
440         PACK_SUMMARY_APP,
441         summary.app,
442         JsonType::OBJECT,
443         false,
444         parseResult,
445         ArrayType::NOT_ARRAY);
446     GetValueIfFindKey<std::vector<PackageModule>>(jsonObject,
447         jsonObjectEnd,
448         PACK_SUMMARY_MODULE,
449         summary.modules,
450         JsonType::ARRAY,
451         false,
452         parseResult,
453         ArrayType::OBJECT);
454     if (parseResult != ERR_OK) {
455         APP_LOGE("read abilityinfo error %{public}d", parseResult);
456     }
457 }
458 
to_json(nlohmann::json & jsonObject,const Packages & packages)459 void to_json(nlohmann::json &jsonObject, const Packages &packages)
460 {
461     jsonObject = nlohmann::json {
462         {PACK_PACKAGES_DEVICE_TYPE, packages.deviceType},
463         {PACK_PACKAGES_MODULE_TYPE, packages.moduleType},
464         {PACK_PACKAGES_DELIVERY_WITH_INSTALL, packages.deliveryWithInstall},
465         {PACK_PACKAGES_NAME, packages.name}
466     };
467 }
468 
from_json(const nlohmann::json & jsonObject,Packages & packages)469 void from_json(const nlohmann::json &jsonObject, Packages &packages)
470 {
471     const auto &jsonObjectEnd = jsonObject.end();
472     int32_t parseResult = ERR_OK;
473     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
474         jsonObjectEnd,
475         PACK_PACKAGES_DEVICE_TYPE,
476         packages.deviceType,
477         JsonType::ARRAY,
478         false,
479         parseResult,
480         ArrayType::STRING);
481     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
482         jsonObjectEnd,
483         PACK_PACKAGES_MODULE_TYPE,
484         packages.moduleType,
485         false,
486         parseResult);
487     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
488         jsonObjectEnd,
489         PACK_PACKAGES_DELIVERY_WITH_INSTALL,
490         packages.deliveryWithInstall,
491         false,
492         parseResult);
493     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
494         jsonObjectEnd,
495         PACK_PACKAGES_NAME,
496         packages.name,
497         false,
498         parseResult);
499     if (parseResult != ERR_OK) {
500         APP_LOGE("read abilityinfo error %{public}d", parseResult);
501     }
502 }
503 
to_json(nlohmann::json & jsonObject,const BundlePackInfo & bundlePackInfo)504 void to_json(nlohmann::json &jsonObject, const BundlePackInfo &bundlePackInfo)
505 {
506     jsonObject = nlohmann::json {
507         {BUNDLE_PACK_INFO_SUMMARY, bundlePackInfo.summary},
508         {BUNDLE_PACK_INFO_PACKAGES, bundlePackInfo.packages}
509     };
510 }
511 
from_json(const nlohmann::json & jsonObject,BundlePackInfo & bundlePackInfo)512 void from_json(const nlohmann::json &jsonObject, BundlePackInfo &bundlePackInfo)
513 {
514     const auto &jsonObjectEnd = jsonObject.end();
515     int32_t parseResult = ERR_OK;
516     GetValueIfFindKey<Summary>(jsonObject,
517         jsonObjectEnd,
518         BUNDLE_PACK_INFO_SUMMARY,
519         bundlePackInfo.summary,
520         JsonType::OBJECT,
521         false,
522         parseResult,
523         ArrayType::NOT_ARRAY);
524     GetValueIfFindKey<std::vector<Packages>>(jsonObject,
525         jsonObjectEnd,
526         BUNDLE_PACK_INFO_PACKAGES,
527         bundlePackInfo.packages,
528         JsonType::ARRAY,
529         false,
530         parseResult,
531         ArrayType::OBJECT);
532     if (parseResult != ERR_OK) {
533         APP_LOGE("read abilityinfo error %{public}d", parseResult);
534     }
535 }
536 
ReadFromParcel(Parcel & parcel)537 bool BundlePackInfo::ReadFromParcel(Parcel &parcel)
538 {
539     MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
540     if (!messageParcel) {
541         APP_LOGE("Type conversion failed");
542         return false;
543     }
544     uint32_t length = messageParcel->ReadUint32();
545     if (length == 0) {
546         APP_LOGE("Invalid data length");
547         return false;
548     }
549     const char *data = reinterpret_cast<const char *>(messageParcel->ReadRawData(length));
550     if (!data) {
551         APP_LOGE("Fail read raw length = %{public}d", length);
552         return false;
553     }
554     nlohmann::json jsonObject = nlohmann::json::parse(data, nullptr, false);
555     if (jsonObject.is_discarded()) {
556         APP_LOGE("failed to parse BundleInfo");
557         return false;
558     }
559     *this = jsonObject.get<BundlePackInfo>();
560     return true;
561 }
562 
Marshalling(Parcel & parcel) const563 bool BundlePackInfo::Marshalling(Parcel &parcel) const
564 {
565     MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
566     if (!messageParcel) {
567         APP_LOGE("Type conversion failed");
568         return false;
569     }
570     nlohmann::json jsonObject = *this;
571     std::string str = jsonObject.dump();
572     if (!messageParcel->WriteUint32(str.size() + 1)) {
573         APP_LOGE("Failed to write;data size");
574         return false;
575     }
576     if (!messageParcel->WriteRawData(str.c_str(), str.size() + 1)) {
577         APP_LOGE("Failed to write data");
578         return false;
579     }
580     return true;
581 }
582 
Unmarshalling(Parcel & parcel)583 BundlePackInfo *BundlePackInfo::Unmarshalling(Parcel &parcel)
584 {
585     BundlePackInfo *info = new (std::nothrow) BundlePackInfo();
586     if (info && !info->ReadFromParcel(parcel)) {
587         APP_LOGW("read from parcel failed");
588         delete info;
589         info = nullptr;
590     }
591     return info;
592 }
593 } // AppExecFwk
594 } // OHOS
595