3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/json.h"
36 bool GetStringFromJson(const Json::Value& in, std::string* out) {
40 s << std::boolalpha << in.asBool();
41 } else if (in.isInt()) {
43 } else if (in.isUInt()) {
45 } else if (in.isDouble()) {
57 bool GetIntFromJson(const Json::Value& in, int* out) {
60 ret = in.isConvertibleTo(Json::intValue);
66 const char* c_str = in.asCString();
69 val = strtol(c_str, &end_ptr, 10); // NOLINT
70 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno &&
71 val >= INT_MIN && val <= INT_MAX);
77 bool GetUIntFromJson(const Json::Value& in, unsigned int* out) {
80 ret = in.isConvertibleTo(Json::uintValue);
85 unsigned long val; // NOLINT
86 const char* c_str = in.asCString();
89 val = strtoul(c_str, &end_ptr, 10); // NOLINT
90 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno &&
97 bool GetBoolFromJson(const Json::Value& in, bool* out) {
100 ret = in.isConvertibleTo(Json::booleanValue);
105 if (in.asString() == "true") {
108 } else if (in.asString() == "false") {
118 bool GetDoubleFromJson(const Json::Value& in, double* out) {
120 if (!in.isString()) {
121 ret = in.isConvertibleTo(Json::realValue);
123 *out = in.asDouble();
127 const char* c_str = in.asCString();
130 val = strtod(c_str, &end_ptr);
131 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno);
139 bool JsonArrayToVector(const Json::Value& value,
140 bool (*getter)(const Json::Value& in, T* out),
141 std::vector<T> *vec) {
143 if (!value.isArray()) {
147 for (Json::Value::ArrayIndex i = 0; i < value.size(); ++i) {
149 if (!getter(value[i], &val)) {
157 // Trivial getter helper
158 bool GetValueFromJson(const Json::Value& in, Json::Value* out) {
162 } // unnamed namespace
164 bool JsonArrayToValueVector(const Json::Value& in,
165 std::vector<Json::Value>* out) {
166 return JsonArrayToVector(in, GetValueFromJson, out);
169 bool JsonArrayToIntVector(const Json::Value& in,
170 std::vector<int>* out) {
171 return JsonArrayToVector(in, GetIntFromJson, out);
174 bool JsonArrayToUIntVector(const Json::Value& in,
175 std::vector<unsigned int>* out) {
176 return JsonArrayToVector(in, GetUIntFromJson, out);
179 bool JsonArrayToStringVector(const Json::Value& in,
180 std::vector<std::string>* out) {
181 return JsonArrayToVector(in, GetStringFromJson, out);
184 bool JsonArrayToBoolVector(const Json::Value& in,
185 std::vector<bool>* out) {
186 return JsonArrayToVector(in, GetBoolFromJson, out);
189 bool JsonArrayToDoubleVector(const Json::Value& in,
190 std::vector<double>* out) {
191 return JsonArrayToVector(in, GetDoubleFromJson, out);
196 Json::Value VectorToJsonArray(const std::vector<T>& vec) {
197 Json::Value result(Json::arrayValue);
198 for (size_t i = 0; i < vec.size(); ++i) {
199 result.append(Json::Value(vec[i]));
203 } // unnamed namespace
205 Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in) {
206 return VectorToJsonArray(in);
209 Json::Value IntVectorToJsonArray(const std::vector<int>& in) {
210 return VectorToJsonArray(in);
213 Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in) {
214 return VectorToJsonArray(in);
217 Json::Value StringVectorToJsonArray(const std::vector<std::string>& in) {
218 return VectorToJsonArray(in);
221 Json::Value BoolVectorToJsonArray(const std::vector<bool>& in) {
222 return VectorToJsonArray(in);
225 Json::Value DoubleVectorToJsonArray(const std::vector<double>& in) {
226 return VectorToJsonArray(in);
229 bool GetValueFromJsonArray(const Json::Value& in, size_t n,
231 if (!in.isArray() || !in.isValidIndex(static_cast<int>(n))) {
235 *out = in[static_cast<Json::Value::ArrayIndex>(n)];
239 bool GetIntFromJsonArray(const Json::Value& in, size_t n,
242 return GetValueFromJsonArray(in, n, &x) && GetIntFromJson(x, out);
245 bool GetUIntFromJsonArray(const Json::Value& in, size_t n,
248 return GetValueFromJsonArray(in, n, &x) && GetUIntFromJson(x, out);
251 bool GetStringFromJsonArray(const Json::Value& in, size_t n,
254 return GetValueFromJsonArray(in, n, &x) && GetStringFromJson(x, out);
257 bool GetBoolFromJsonArray(const Json::Value& in, size_t n,
260 return GetValueFromJsonArray(in, n, &x) && GetBoolFromJson(x, out);
263 bool GetDoubleFromJsonArray(const Json::Value& in, size_t n,
266 return GetValueFromJsonArray(in, n, &x) && GetDoubleFromJson(x, out);
269 bool GetValueFromJsonObject(const Json::Value& in, const std::string& k,
271 if (!in.isObject() || !in.isMember(k)) {
279 bool GetIntFromJsonObject(const Json::Value& in, const std::string& k,
282 return GetValueFromJsonObject(in, k, &x) && GetIntFromJson(x, out);
285 bool GetUIntFromJsonObject(const Json::Value& in, const std::string& k,
288 return GetValueFromJsonObject(in, k, &x) && GetUIntFromJson(x, out);
291 bool GetStringFromJsonObject(const Json::Value& in, const std::string& k,
294 return GetValueFromJsonObject(in, k, &x) && GetStringFromJson(x, out);
297 bool GetBoolFromJsonObject(const Json::Value& in, const std::string& k,
300 return GetValueFromJsonObject(in, k, &x) && GetBoolFromJson(x, out);
303 bool GetDoubleFromJsonObject(const Json::Value& in, const std::string& k,
306 return GetValueFromJsonObject(in, k, &x) && GetDoubleFromJson(x, out);
309 std::string JsonValueToString(const Json::Value& json) {
311 std::string value = w.write(json);
312 return value.substr(0, value.size() - 1); // trim trailing newline