• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "json_helper.h"
17 #include "hilog/log.h"
18 #include "log_tags.h"
19 #include "plugin_common_type.h"
20 
21 namespace OHOS {
22 namespace MultimediaPlugin {
23 using nlohmann::json;
24 using std::string;
25 using namespace OHOS::HiviewDFX;
26 
27 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "JsonHelper" };
28 json JsonHelper::nullJson_;
29 
CheckElementExistence(const json & jsonObject,const string & key)30 uint32_t JsonHelper::CheckElementExistence(const json &jsonObject, const string &key)
31 {
32     uint32_t errorCode;
33     GetJsonElement(jsonObject, key, errorCode);
34     return errorCode;
35 }
36 
GetStringValue(const json & jsonString,string & value)37 uint32_t JsonHelper::GetStringValue(const json &jsonString, string &value)
38 {
39     if (!jsonString.is_string()) {
40         HiLog::Error(LABEL, "GetStringValue: not a string type value.");
41         return ERR_DATA_TYPE;
42     }
43 
44     value = jsonString;
45     return SUCCESS;
46 }
47 
GetStringValue(const json & jsonObject,const string & key,string & value)48 uint32_t JsonHelper::GetStringValue(const json &jsonObject, const string &key, string &value)
49 {
50     uint32_t result;
51     const json &jsonString = GetJsonElement(jsonObject, key, result);
52     if (result != SUCCESS) {
53         PrintElementMissingLog("GetStringValue", key, result);
54         return result;
55     }
56 
57     return GetStringValue(jsonString, value);
58 }
59 
GetUint32Value(const json & jsonNum,uint32_t & value)60 uint32_t JsonHelper::GetUint32Value(const json &jsonNum, uint32_t &value)
61 {
62     if (!jsonNum.is_number_integer()) {
63         HiLog::Error(LABEL, "GetUint32Value: not a integer type value.");
64         return ERR_DATA_TYPE;
65     }
66 
67     if (jsonNum < 0) {
68         HiLog::Error(LABEL, "GetUint32Value: not a unsigned integer type value, num: %{public}lld.",
69                      static_cast<long long>(jsonNum));
70         return ERR_DATA_TYPE;
71     }
72 
73     if (jsonNum > UINT32_MAX_VALUE) {
74         HiLog::Error(LABEL, "GetUint32Value: out of range value, num: %{public}llu.",
75                      static_cast<unsigned long long>(jsonNum));
76         return ERR_DATA_TYPE;
77     }
78 
79     value = jsonNum;
80     return SUCCESS;
81 }
82 
GetUint32Value(const json & jsonObject,const string & key,uint32_t & value)83 uint32_t JsonHelper::GetUint32Value(const json &jsonObject, const string &key, uint32_t &value)
84 {
85     uint32_t result;
86     const json &jsonNum = GetJsonElement(jsonObject, key, result);
87     if (result != SUCCESS) {
88         PrintElementMissingLog("GetUint32Value", key, result);
89         return result;
90     }
91 
92     return GetUint32Value(jsonNum, value);
93 }
94 
GetUint16Value(const json & jsonObject,const string & key,uint16_t & value)95 uint32_t JsonHelper::GetUint16Value(const json &jsonObject, const string &key, uint16_t &value)
96 {
97     uint32_t result;
98     const json &jsonNum = GetJsonElement(jsonObject, key, result);
99     if (result != SUCCESS) {
100         PrintElementMissingLog("GetUint16Value", key, result);
101         return result;
102     }
103 
104     if (!jsonNum.is_number_integer()) {
105         HiLog::Error(LABEL, "GetUint16Value: not a integer type value for key %{public}s.", key.c_str());
106         return ERR_DATA_TYPE;
107     }
108 
109     if (jsonNum < 0) {
110         HiLog::Error(LABEL, "GetUint16Value: not a unsigned integer type value for key %{public}s, num: %{public}lld.",
111                      key.c_str(), static_cast<long long>(jsonNum));
112         return ERR_DATA_TYPE;
113     }
114 
115     if (jsonNum > UINT16_MAX_VALUE) {
116         HiLog::Error(LABEL, "GetUint16Value: out of range value for key %{public}s, num: %{public}llu.", key.c_str(),
117                      static_cast<unsigned long long>(jsonNum));
118         return ERR_DATA_TYPE;
119     }
120 
121     value = jsonNum;
122     return SUCCESS;
123 }
124 
GetArraySize(const json & jsonObject,const string & key,size_t & size)125 uint32_t JsonHelper::GetArraySize(const json &jsonObject, const string &key, size_t &size)
126 {
127     uint32_t result;
128     const json &jsonArray = GetJsonElement(jsonObject, key, result);
129     if (result != SUCCESS) {
130         PrintElementMissingLog("GetArraySize", key, result);
131         return result;
132     }
133 
134     if (!jsonArray.is_array()) {
135         HiLog::Error(LABEL, "GetArraySize: not a array type value for key %{public}s.", key.c_str());
136         return ERR_DATA_TYPE;
137     }
138 
139     size = jsonArray.size();
140     return SUCCESS;
141 }
142 
143 // ------------------------------- private method -------------------------------
GetJsonElement(const json & jsonObject,const string & key,uint32_t & errorCode)144 const json &JsonHelper::GetJsonElement(const json &jsonObject, const string &key, uint32_t &errorCode)
145 {
146     if (!jsonObject.is_object()) {
147         HiLog::Error(LABEL, "GetJsonElement: not an object type json for key %{public}s.", key.c_str());
148         errorCode = ERR_DATA_TYPE;
149         return nullJson_;
150     }
151 
152     auto iter = jsonObject.find(key);
153     if (iter == jsonObject.end()) {
154         // some elements are optional, it is normal to miss them, so do not use error level here.
155         HiLog::Debug(LABEL, "GetJsonElement: failed to find key %{public}s.", key.c_str());
156         errorCode = ERR_NO_TARGET;
157         return nullJson_;
158     }
159 
160     errorCode = SUCCESS;
161     return *iter;
162 }
163 
PrintElementMissingLog(const std::string & identifier,const std::string & key,uint32_t errorCode)164 void JsonHelper::PrintElementMissingLog(const std::string &identifier, const std::string &key, uint32_t errorCode)
165 {
166     if (errorCode == ERR_NO_TARGET) {
167         // some elements are optional, it is normal to miss them, so do not use error level here.
168         HiLog::Debug(LABEL, "%{public}s: failed to find key %{public}s, ERRNO: %{public}u.", identifier.c_str(),
169                      key.c_str(), errorCode);
170     } else {
171         HiLog::Error(LABEL, "%{public}s: failed to find key %{public}s, ERRNO: %{public}u.", identifier.c_str(),
172                      key.c_str(), errorCode);
173     }
174 }
175 } // namespace MultimediaPlugin
176 } // namespace OHOS
177