1 /*
2 * Copyright (C) 2025 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 #define MLOG_TAG "Media_Client"
16
17 #include "json_helper.h"
18
19 #include <string>
20
21 #include "media_log.h"
22 // LCOV_EXCL_START
GetStringFromJson(const Json::Value & data,const std::string key,const std::string defaultValue)23 std::string JsonHelper::GetStringFromJson(
24 const Json::Value &data, const std::string key, const std::string defaultValue)
25 {
26 if (data.isObject() && data.isMember(key.c_str()) && data[key].isString()) {
27 return data[key].asString();
28 }
29 return defaultValue;
30 }
GetIntFromJson(const Json::Value & data,const std::string key,const int defaultValue)31 int JsonHelper::GetIntFromJson(const Json::Value &data, const std::string key, const int defaultValue)
32 {
33 if (data.isObject() && data.isMember(key.c_str()) && data[key].isInt()) {
34 return data[key].asInt();
35 }
36 return defaultValue;
37 }
GetUIntFromJson(const Json::Value & data,const std::string key,const uint32_t defaultValue)38 uint32_t JsonHelper::GetUIntFromJson(const Json::Value &data, const std::string key, const uint32_t defaultValue)
39 {
40 if (data.isObject() && data.isMember(key.c_str()) && data[key].isUInt()) {
41 return data[key].asUInt();
42 }
43 return defaultValue;
44 }
GetBoolFromJson(const Json::Value & data,const std::string key,const bool defaultValue)45 bool JsonHelper::GetBoolFromJson(const Json::Value &data, const std::string key, const bool defaultValue)
46 {
47 if (data.isObject() && data.isMember(key.c_str()) && data[key].isBool()) {
48 return data[key].asBool();
49 }
50 return defaultValue;
51 }
GetUInt64FromJson(const Json::Value & data,const std::string key,const uint64_t defaultValue)52 uint64_t JsonHelper::GetUInt64FromJson(const Json::Value &data, const std::string key, const uint64_t defaultValue)
53 {
54 if (data.isObject() && data.isMember(key.c_str()) && data[key].isUInt64()) {
55 return data[key].asUInt64();
56 }
57 return defaultValue;
58 }
GetInt64FromJson(const Json::Value & data,const std::string key,const int64_t defaultValue)59 int64_t JsonHelper::GetInt64FromJson(const Json::Value &data, const std::string key, const int64_t defaultValue)
60 {
61 if (data.isObject() && data.isMember(key.c_str()) && data[key].isInt64()) {
62 return data[key].asInt64();
63 }
64 return defaultValue;
65 }
GetDoubleFromJson(const Json::Value & data,const std::string key,const double defaultValue)66 double JsonHelper::GetDoubleFromJson(const Json::Value &data, const std::string key, const double defaultValue)
67 {
68 if (data.isObject() && data.isMember(key.c_str()) && data[key].isDouble()) {
69 return data[key].asDouble();
70 }
71 return defaultValue;
72 }
JsonToString(const Json::Value & data)73 std::string JsonHelper::JsonToString(const Json::Value &data)
74 {
75 Json::StreamWriterBuilder writerBuilder;
76 writerBuilder["indentation"] = "";
77 return Json::writeString(writerBuilder, data);
78 }
StringToJson(const std::string & data)79 Json::Value JsonHelper::StringToJson(const std::string &data)
80 {
81 Json::Value jsValue;
82 Json::CharReaderBuilder builder;
83 Json::CharReader *reader = builder.newCharReader();
84 if (reader == nullptr) {
85 return jsValue;
86 }
87 std::string errors;
88 if (!reader->parse(data.c_str(), data.c_str() + data.size(), &jsValue, &errors)) {
89 delete reader;
90 return jsValue;
91 }
92 delete reader;
93 return jsValue;
94 }
JsonArrayToString(const Json::Value & data,const std::string & sep)95 std::string JsonHelper::JsonArrayToString(const Json::Value &data, const std::string &sep)
96 {
97 std::string out = "";
98 if (!data.isArray()) {
99 return out;
100 }
101 Json::ArrayIndex maxCount = 500;
102 for (Json::ArrayIndex i = 0; i < data.size() && i < maxCount; ++i) {
103 if (!data[i].isString()) {
104 continue;
105 }
106 out.append(data[i].asString());
107 if (i + 1 < data.size()) {
108 out.append(sep);
109 }
110 }
111 return out;
112 }
113
JsonToStrVec(const Json::Value & data,std::vector<std::string> & vec)114 bool JsonHelper::JsonToStrVec(const Json::Value &data, std::vector<std::string> &vec)
115 {
116 if (!data.isArray()) {
117 return false;
118 }
119 Json::ArrayIndex maxCount = 500;
120 for (Json::ArrayIndex i = 0; i < data.size() && i < maxCount; ++i) {
121 if (!data[i].isString()) {
122 continue;
123 }
124 vec.emplace_back(data[i].asString());
125 }
126 return vec.empty() ? false : true;
127 }
128
HasSpecifiedKey(const Json::Value & data,const std::string & key)129 bool JsonHelper::HasSpecifiedKey(const Json::Value &data, const std::string &key)
130 {
131 if (data.isObject() && data.isMember(key.c_str())) {
132 return true;
133 }
134 return false;
135 }
136 // LCOV_EXCL_STOP