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 if (jsonNum < 0) {
70 IMAGE_LOGE("GetUint32Value: not a unsigned integer type value, num: %{public}lld.",
71 static_cast<long long>(jsonNum));
72 return ERR_DATA_TYPE;
73 }
74
75 if (jsonNum > UINT32_MAX_VALUE) {
76 IMAGE_LOGE("GetUint32Value: out of range value, num: %{public}llu.",
77 static_cast<unsigned long long>(jsonNum));
78 return ERR_DATA_TYPE;
79 }
80
81 value = jsonNum;
82 return SUCCESS;
83 }
84
GetUint32Value(const json & jsonObject,const string & key,uint32_t & value)85 uint32_t JsonHelper::GetUint32Value(const json &jsonObject, const string &key, uint32_t &value)
86 {
87 uint32_t result;
88 const json &jsonNum = GetJsonElement(jsonObject, key, result);
89 if (result != SUCCESS) {
90 PrintElementMissingLog("GetUint32Value", key, result);
91 return result;
92 }
93
94 return GetUint32Value(jsonNum, value);
95 }
96
GetUint16Value(const json & jsonObject,const string & key,uint16_t & value)97 uint32_t JsonHelper::GetUint16Value(const json &jsonObject, const string &key, uint16_t &value)
98 {
99 uint32_t result;
100 const json &jsonNum = GetJsonElement(jsonObject, key, result);
101 if (result != SUCCESS) {
102 PrintElementMissingLog("GetUint16Value", key, result);
103 return result;
104 }
105
106 if (!jsonNum.is_number_integer()) {
107 IMAGE_LOGE("GetUint16Value: not a integer type value for key %{public}s.", key.c_str());
108 return ERR_DATA_TYPE;
109 }
110
111 if (jsonNum < 0) {
112 IMAGE_LOGE("GetUint16Value: not a unsigned integer type value for key %{public}s, num: %{public}lld.",
113 key.c_str(), static_cast<long long>(jsonNum));
114 return ERR_DATA_TYPE;
115 }
116
117 if (jsonNum > UINT16_MAX_VALUE) {
118 IMAGE_LOGE("GetUint16Value: out of range value for key %{public}s, num: %{public}llu.", key.c_str(),
119 static_cast<unsigned long long>(jsonNum));
120 return ERR_DATA_TYPE;
121 }
122
123 value = jsonNum;
124 return SUCCESS;
125 }
126
GetArraySize(const json & jsonObject,const string & key,size_t & size)127 uint32_t JsonHelper::GetArraySize(const json &jsonObject, const string &key, size_t &size)
128 {
129 uint32_t result;
130 const json &jsonArray = GetJsonElement(jsonObject, key, result);
131 if (result != SUCCESS) {
132 PrintElementMissingLog("GetArraySize", key, result);
133 return result;
134 }
135
136 if (!jsonArray.is_array()) {
137 IMAGE_LOGE("GetArraySize: not a array type value for key %{public}s.", key.c_str());
138 return ERR_DATA_TYPE;
139 }
140
141 size = jsonArray.size();
142 return SUCCESS;
143 }
144
145 // ------------------------------- private method -------------------------------
GetJsonElement(const json & jsonObject,const string & key,uint32_t & errorCode)146 const json &JsonHelper::GetJsonElement(const json &jsonObject, const string &key, uint32_t &errorCode)
147 {
148 if (!jsonObject.is_object()) {
149 IMAGE_LOGE("GetJsonElement: not an object type json for key %{public}s.", key.c_str());
150 errorCode = ERR_DATA_TYPE;
151 return nullJson_;
152 }
153
154 auto iter = jsonObject.find(key);
155 if (iter == jsonObject.end()) {
156 // some elements are optional, it is normal to miss them, so do not use error level here.
157 IMAGE_LOGD("GetJsonElement: failed to find key %{public}s.", key.c_str());
158 errorCode = ERR_NO_TARGET;
159 return nullJson_;
160 }
161
162 errorCode = SUCCESS;
163 return *iter;
164 }
165
PrintElementMissingLog(const std::string & identifier,const std::string & key,uint32_t errorCode)166 void JsonHelper::PrintElementMissingLog(const std::string &identifier, const std::string &key, uint32_t errorCode)
167 {
168 if (errorCode == ERR_NO_TARGET) {
169 // some elements are optional, it is normal to miss them, so do not use error level here.
170 IMAGE_LOGD("%{public}s: failed to find key %{public}s, ERRNO: %{public}u.", identifier.c_str(),
171 key.c_str(), errorCode);
172 } else {
173 IMAGE_LOGE("%{public}s: failed to find key %{public}s, ERRNO: %{public}u.", identifier.c_str(),
174 key.c_str(), errorCode);
175 }
176 }
177 } // namespace MultimediaPlugin
178 } // namespace OHOS
179