• 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 #ifndef OS_ACCOUNT_FRAMEWORKS_COMMON_DATABASE_INCLUDE_IACCOUNT_INFO_H
17 #define OS_ACCOUNT_FRAMEWORKS_COMMON_DATABASE_INCLUDE_IACCOUNT_INFO_H
18 
19 #include <string>
20 #include "nlohmann/json.hpp"
21 
22 namespace OHOS {
23 namespace AccountSA {
24 using Json = nlohmann::json;
25 
26 class IAccountInfo {
27 public:
28     virtual Json ToJson() const = 0;
29     virtual void FromJson(const Json &jsonObject) = 0;
30     virtual std::string ToString() const = 0;
31     virtual std::string GetPrimeKey() const = 0;
32 };
33 
34 enum class JsonType {
35     NULLABLE,
36     BOOLEAN,
37     NUMBER,
38     OBJECT,
39     ARRAY,
40     STRING,
41 };
42 
43 template<typename T, typename dataType>
GetDataByType(const Json & jsonObject,const nlohmann::detail::iter_impl<const Json> & end,const std::string & key,dataType & data,const JsonType jsonType)44 void GetDataByType(const Json &jsonObject, const nlohmann::detail::iter_impl<const Json> &end, const std::string &key,
45     dataType &data, const JsonType jsonType)
46 {
47     if (jsonObject.find(key) != end) {
48         switch (jsonType) {
49             case JsonType::BOOLEAN:
50                 if (!jsonObject.at(key).is_boolean()) {
51                     break;
52                 }
53                 data = jsonObject.at(key).get<T>();
54                 break;
55             case JsonType::NUMBER:
56                 if (!jsonObject.at(key).is_number()) {
57                     break;
58                 }
59                 data = jsonObject.at(key).get<T>();
60                 break;
61             case JsonType::OBJECT:
62                 if (!jsonObject.at(key).is_object()) {
63                     break;
64                 }
65                 data = jsonObject.at(key).get<T>();
66                 break;
67             case JsonType::ARRAY:
68                 if (!jsonObject.at(key).is_array()) {
69                     break;
70                 }
71                 data = jsonObject.at(key).get<T>();
72                 break;
73             case JsonType::STRING:
74                 if (!jsonObject.at(key).is_string()) {
75                     break;
76                 }
77                 data = jsonObject.at(key).get<T>();
78                 break;
79             case JsonType::NULLABLE:
80                 break;
81             default:
82                 break;
83         }
84     }
85 }
86 }  // namespace AccountSA
87 }  // namespace OHOS
88 
89 #endif  // OS_ACCOUNT_FRAMEWORKS_COMMON_DATABASE_INCLUDE_IACCOUNT_INFO_H
90