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