Move rtc_json code from API dir, enable unit test, unmark testonly
This change does three things:
- Move rtc_json into rtc_base/strings/, a non-API directory more fitting to
its purpose.
- Make a target for the currently unused json_unittest.
- Make the code available for use in non-test code again.
Bug: webrtc:9802
Change-Id: Id964a8a4b47b732a962a364894a4dbd3e7f4650f
Reviewed-on: https://webrtc-review.googlesource.com/103126
Commit-Queue: Sam Zackrisson <saza@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24932}
diff --git a/rtc_base/strings/json.cc b/rtc_base/strings/json.cc
new file mode 100644
index 0000000..efcb97a
--- /dev/null
+++ b/rtc_base/strings/json.cc
@@ -0,0 +1,294 @@
+/*
+ * Copyright 2004 The WebRTC Project Authors. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "rtc_base/strings/json.h"
+
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#include "rtc_base/stringencode.h"
+
+namespace rtc {
+
+bool GetStringFromJson(const Json::Value& in, std::string* out) {
+ if (!in.isString()) {
+ if (in.isBool()) {
+ *out = rtc::ToString(in.asBool());
+ } else if (in.isInt()) {
+ *out = rtc::ToString(in.asInt());
+ } else if (in.isUInt()) {
+ *out = rtc::ToString(in.asUInt());
+ } else if (in.isDouble()) {
+ *out = rtc::ToString(in.asDouble());
+ } else {
+ return false;
+ }
+ } else {
+ *out = in.asString();
+ }
+ return true;
+}
+
+bool GetIntFromJson(const Json::Value& in, int* out) {
+ bool ret;
+ if (!in.isString()) {
+ ret = in.isConvertibleTo(Json::intValue);
+ if (ret) {
+ *out = in.asInt();
+ }
+ } else {
+ long val; // NOLINT
+ const char* c_str = in.asCString();
+ char* end_ptr;
+ errno = 0;
+ val = strtol(c_str, &end_ptr, 10); // NOLINT
+ ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val >= INT_MIN &&
+ val <= INT_MAX);
+ *out = val;
+ }
+ return ret;
+}
+
+bool GetUIntFromJson(const Json::Value& in, unsigned int* out) {
+ bool ret;
+ if (!in.isString()) {
+ ret = in.isConvertibleTo(Json::uintValue);
+ if (ret) {
+ *out = in.asUInt();
+ }
+ } else {
+ unsigned long val; // NOLINT
+ const char* c_str = in.asCString();
+ char* end_ptr;
+ errno = 0;
+ val = strtoul(c_str, &end_ptr, 10); // NOLINT
+ ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val <= UINT_MAX);
+ *out = val;
+ }
+ return ret;
+}
+
+bool GetBoolFromJson(const Json::Value& in, bool* out) {
+ bool ret;
+ if (!in.isString()) {
+ ret = in.isConvertibleTo(Json::booleanValue);
+ if (ret) {
+ *out = in.asBool();
+ }
+ } else {
+ if (in.asString() == "true") {
+ *out = true;
+ ret = true;
+ } else if (in.asString() == "false") {
+ *out = false;
+ ret = true;
+ } else {
+ ret = false;
+ }
+ }
+ return ret;
+}
+
+bool GetDoubleFromJson(const Json::Value& in, double* out) {
+ bool ret;
+ if (!in.isString()) {
+ ret = in.isConvertibleTo(Json::realValue);
+ if (ret) {
+ *out = in.asDouble();
+ }
+ } else {
+ double val;
+ const char* c_str = in.asCString();
+ char* end_ptr;
+ errno = 0;
+ val = strtod(c_str, &end_ptr);
+ ret = (end_ptr != c_str && *end_ptr == '\0' && !errno);
+ *out = val;
+ }
+ return ret;
+}
+
+namespace {
+template <typename T>
+bool JsonArrayToVector(const Json::Value& value,
+ bool (*getter)(const Json::Value& in, T* out),
+ std::vector<T>* vec) {
+ vec->clear();
+ if (!value.isArray()) {
+ return false;
+ }
+
+ for (Json::Value::ArrayIndex i = 0; i < value.size(); ++i) {
+ T val;
+ if (!getter(value[i], &val)) {
+ return false;
+ }
+ vec->push_back(val);
+ }
+
+ return true;
+}
+// Trivial getter helper
+bool GetValueFromJson(const Json::Value& in, Json::Value* out) {
+ *out = in;
+ return true;
+}
+} // unnamed namespace
+
+bool JsonArrayToValueVector(const Json::Value& in,
+ std::vector<Json::Value>* out) {
+ return JsonArrayToVector(in, GetValueFromJson, out);
+}
+
+bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out) {
+ return JsonArrayToVector(in, GetIntFromJson, out);
+}
+
+bool JsonArrayToUIntVector(const Json::Value& in,
+ std::vector<unsigned int>* out) {
+ return JsonArrayToVector(in, GetUIntFromJson, out);
+}
+
+bool JsonArrayToStringVector(const Json::Value& in,
+ std::vector<std::string>* out) {
+ return JsonArrayToVector(in, GetStringFromJson, out);
+}
+
+bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out) {
+ return JsonArrayToVector(in, GetBoolFromJson, out);
+}
+
+bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out) {
+ return JsonArrayToVector(in, GetDoubleFromJson, out);
+}
+
+namespace {
+template <typename T>
+Json::Value VectorToJsonArray(const std::vector<T>& vec) {
+ Json::Value result(Json::arrayValue);
+ for (size_t i = 0; i < vec.size(); ++i) {
+ result.append(Json::Value(vec[i]));
+ }
+ return result;
+}
+} // unnamed namespace
+
+Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in) {
+ return VectorToJsonArray(in);
+}
+
+Json::Value IntVectorToJsonArray(const std::vector<int>& in) {
+ return VectorToJsonArray(in);
+}
+
+Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in) {
+ return VectorToJsonArray(in);
+}
+
+Json::Value StringVectorToJsonArray(const std::vector<std::string>& in) {
+ return VectorToJsonArray(in);
+}
+
+Json::Value BoolVectorToJsonArray(const std::vector<bool>& in) {
+ return VectorToJsonArray(in);
+}
+
+Json::Value DoubleVectorToJsonArray(const std::vector<double>& in) {
+ return VectorToJsonArray(in);
+}
+
+bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out) {
+ if (!in.isArray() || !in.isValidIndex(static_cast<int>(n))) {
+ return false;
+ }
+
+ *out = in[static_cast<Json::Value::ArrayIndex>(n)];
+ return true;
+}
+
+bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out) {
+ Json::Value x;
+ return GetValueFromJsonArray(in, n, &x) && GetIntFromJson(x, out);
+}
+
+bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out) {
+ Json::Value x;
+ return GetValueFromJsonArray(in, n, &x) && GetUIntFromJson(x, out);
+}
+
+bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out) {
+ Json::Value x;
+ return GetValueFromJsonArray(in, n, &x) && GetStringFromJson(x, out);
+}
+
+bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out) {
+ Json::Value x;
+ return GetValueFromJsonArray(in, n, &x) && GetBoolFromJson(x, out);
+}
+
+bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out) {
+ Json::Value x;
+ return GetValueFromJsonArray(in, n, &x) && GetDoubleFromJson(x, out);
+}
+
+bool GetValueFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ Json::Value* out) {
+ if (!in.isObject() || !in.isMember(k)) {
+ return false;
+ }
+
+ *out = in[k];
+ return true;
+}
+
+bool GetIntFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ int* out) {
+ Json::Value x;
+ return GetValueFromJsonObject(in, k, &x) && GetIntFromJson(x, out);
+}
+
+bool GetUIntFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ unsigned int* out) {
+ Json::Value x;
+ return GetValueFromJsonObject(in, k, &x) && GetUIntFromJson(x, out);
+}
+
+bool GetStringFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ std::string* out) {
+ Json::Value x;
+ return GetValueFromJsonObject(in, k, &x) && GetStringFromJson(x, out);
+}
+
+bool GetBoolFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ bool* out) {
+ Json::Value x;
+ return GetValueFromJsonObject(in, k, &x) && GetBoolFromJson(x, out);
+}
+
+bool GetDoubleFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ double* out) {
+ Json::Value x;
+ return GetValueFromJsonObject(in, k, &x) && GetDoubleFromJson(x, out);
+}
+
+std::string JsonValueToString(const Json::Value& json) {
+ Json::FastWriter w;
+ std::string value = w.write(json);
+ return value.substr(0, value.size() - 1); // trim trailing newline
+}
+
+} // namespace rtc
diff --git a/rtc_base/strings/json.h b/rtc_base/strings/json.h
new file mode 100644
index 0000000..0cb9542
--- /dev/null
+++ b/rtc_base/strings/json.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2004 The WebRTC Project Authors. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef RTC_BASE_STRINGS_JSON_H_
+#define RTC_BASE_STRINGS_JSON_H_
+
+#include <string>
+#include <vector>
+
+#if !defined(WEBRTC_EXTERNAL_JSON)
+#include "json/json.h"
+#else
+#include "third_party/jsoncpp/json.h"
+#endif
+
+namespace rtc {
+
+///////////////////////////////////////////////////////////////////////////////
+// JSON Helpers
+///////////////////////////////////////////////////////////////////////////////
+
+// Robust conversion operators, better than the ones in JsonCpp.
+bool GetIntFromJson(const Json::Value& in, int* out);
+bool GetUIntFromJson(const Json::Value& in, unsigned int* out);
+bool GetStringFromJson(const Json::Value& in, std::string* out);
+bool GetBoolFromJson(const Json::Value& in, bool* out);
+bool GetDoubleFromJson(const Json::Value& in, double* out);
+
+// Pull values out of a JSON array.
+bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out);
+bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out);
+bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out);
+bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out);
+bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out);
+bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out);
+
+// Convert json arrays to std::vector
+bool JsonArrayToValueVector(const Json::Value& in,
+ std::vector<Json::Value>* out);
+bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out);
+bool JsonArrayToUIntVector(const Json::Value& in,
+ std::vector<unsigned int>* out);
+bool JsonArrayToStringVector(const Json::Value& in,
+ std::vector<std::string>* out);
+bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out);
+bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out);
+
+// Convert std::vector to json array
+Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in);
+Json::Value IntVectorToJsonArray(const std::vector<int>& in);
+Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in);
+Json::Value StringVectorToJsonArray(const std::vector<std::string>& in);
+Json::Value BoolVectorToJsonArray(const std::vector<bool>& in);
+Json::Value DoubleVectorToJsonArray(const std::vector<double>& in);
+
+// Pull values out of a JSON object.
+bool GetValueFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ Json::Value* out);
+bool GetIntFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ int* out);
+bool GetUIntFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ unsigned int* out);
+bool GetStringFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ std::string* out);
+bool GetBoolFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ bool* out);
+bool GetDoubleFromJsonObject(const Json::Value& in,
+ const std::string& k,
+ double* out);
+
+// Writes out a Json value as a string.
+std::string JsonValueToString(const Json::Value& json);
+
+} // namespace rtc
+
+#endif // RTC_BASE_STRINGS_JSON_H_
diff --git a/rtc_base/strings/json_unittest.cc b/rtc_base/strings/json_unittest.cc
new file mode 100644
index 0000000..2215769
--- /dev/null
+++ b/rtc_base/strings/json_unittest.cc
@@ -0,0 +1,283 @@
+/*
+ * Copyright 2009 The WebRTC Project Authors. All rights reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "rtc_base/strings/json.h"
+
+#include <vector>
+
+#include "rtc_base/gunit.h"
+
+namespace rtc {
+
+static Json::Value in_s("foo");
+static Json::Value in_sn("99");
+static Json::Value in_si("-99");
+static Json::Value in_sb("true");
+static Json::Value in_sd("1.2");
+static Json::Value in_n(12);
+static Json::Value in_i(-12);
+static Json::Value in_u(34U);
+static Json::Value in_b(true);
+static Json::Value in_d(1.2);
+static Json::Value big_sn("12345678901234567890");
+static Json::Value big_si("-12345678901234567890");
+static Json::Value big_u(0xFFFFFFFF);
+static Json::Value bad_a(Json::arrayValue);
+static Json::Value bad_o(Json::objectValue);
+
+TEST(JsonTest, GetString) {
+ std::string out;
+ EXPECT_TRUE(GetStringFromJson(in_s, &out));
+ EXPECT_EQ("foo", out);
+ EXPECT_TRUE(GetStringFromJson(in_sn, &out));
+ EXPECT_EQ("99", out);
+ EXPECT_TRUE(GetStringFromJson(in_si, &out));
+ EXPECT_EQ("-99", out);
+ EXPECT_TRUE(GetStringFromJson(in_i, &out));
+ EXPECT_EQ("-12", out);
+ EXPECT_TRUE(GetStringFromJson(in_n, &out));
+ EXPECT_EQ("12", out);
+ EXPECT_TRUE(GetStringFromJson(in_u, &out));
+ EXPECT_EQ("34", out);
+ EXPECT_TRUE(GetStringFromJson(in_b, &out));
+ EXPECT_EQ("true", out);
+ // Not supported here yet.
+ EXPECT_FALSE(GetStringFromJson(bad_a, &out));
+ EXPECT_FALSE(GetStringFromJson(bad_o, &out));
+}
+
+TEST(JsonTest, GetInt) {
+ int out;
+ EXPECT_TRUE(GetIntFromJson(in_sn, &out));
+ EXPECT_EQ(99, out);
+ EXPECT_TRUE(GetIntFromJson(in_si, &out));
+ EXPECT_EQ(-99, out);
+ EXPECT_TRUE(GetIntFromJson(in_n, &out));
+ EXPECT_EQ(12, out);
+ EXPECT_TRUE(GetIntFromJson(in_i, &out));
+ EXPECT_EQ(-12, out);
+ EXPECT_TRUE(GetIntFromJson(in_u, &out));
+ EXPECT_EQ(34, out);
+ EXPECT_TRUE(GetIntFromJson(in_b, &out));
+ EXPECT_EQ(1, out);
+ EXPECT_FALSE(GetIntFromJson(in_s, &out));
+ EXPECT_FALSE(GetIntFromJson(big_sn, &out));
+ EXPECT_FALSE(GetIntFromJson(big_si, &out));
+ EXPECT_FALSE(GetIntFromJson(big_u, &out));
+ EXPECT_FALSE(GetIntFromJson(bad_a, &out));
+ EXPECT_FALSE(GetIntFromJson(bad_o, &out));
+}
+
+TEST(JsonTest, GetUInt) {
+ unsigned int out;
+ EXPECT_TRUE(GetUIntFromJson(in_sn, &out));
+ EXPECT_EQ(99U, out);
+ EXPECT_TRUE(GetUIntFromJson(in_n, &out));
+ EXPECT_EQ(12U, out);
+ EXPECT_TRUE(GetUIntFromJson(in_u, &out));
+ EXPECT_EQ(34U, out);
+ EXPECT_TRUE(GetUIntFromJson(in_b, &out));
+ EXPECT_EQ(1U, out);
+ EXPECT_TRUE(GetUIntFromJson(big_u, &out));
+ EXPECT_EQ(0xFFFFFFFFU, out);
+ EXPECT_FALSE(GetUIntFromJson(in_s, &out));
+ // TODO(bugs.webrtc.org/9804): Fail reading negative strings.
+ // EXPECT_FALSE(GetUIntFromJson(in_si, &out));
+ EXPECT_FALSE(GetUIntFromJson(in_i, &out));
+ EXPECT_FALSE(GetUIntFromJson(big_sn, &out));
+ EXPECT_FALSE(GetUIntFromJson(big_si, &out));
+ EXPECT_FALSE(GetUIntFromJson(bad_a, &out));
+ EXPECT_FALSE(GetUIntFromJson(bad_o, &out));
+}
+
+TEST(JsonTest, GetBool) {
+ bool out;
+ EXPECT_TRUE(GetBoolFromJson(in_sb, &out));
+ EXPECT_EQ(true, out);
+ EXPECT_TRUE(GetBoolFromJson(in_n, &out));
+ EXPECT_EQ(true, out);
+ EXPECT_TRUE(GetBoolFromJson(in_i, &out));
+ EXPECT_EQ(true, out);
+ EXPECT_TRUE(GetBoolFromJson(in_u, &out));
+ EXPECT_EQ(true, out);
+ EXPECT_TRUE(GetBoolFromJson(in_b, &out));
+ EXPECT_EQ(true, out);
+ EXPECT_TRUE(GetBoolFromJson(big_u, &out));
+ EXPECT_EQ(true, out);
+ EXPECT_FALSE(GetBoolFromJson(in_s, &out));
+ EXPECT_FALSE(GetBoolFromJson(in_sn, &out));
+ EXPECT_FALSE(GetBoolFromJson(in_si, &out));
+ EXPECT_FALSE(GetBoolFromJson(big_sn, &out));
+ EXPECT_FALSE(GetBoolFromJson(big_si, &out));
+ EXPECT_FALSE(GetBoolFromJson(bad_a, &out));
+ EXPECT_FALSE(GetBoolFromJson(bad_o, &out));
+}
+
+TEST(JsonTest, GetDouble) {
+ double out;
+ EXPECT_TRUE(GetDoubleFromJson(in_sn, &out));
+ EXPECT_EQ(99, out);
+ EXPECT_TRUE(GetDoubleFromJson(in_si, &out));
+ EXPECT_EQ(-99, out);
+ EXPECT_TRUE(GetDoubleFromJson(in_sd, &out));
+ EXPECT_EQ(1.2, out);
+ EXPECT_TRUE(GetDoubleFromJson(in_n, &out));
+ EXPECT_EQ(12, out);
+ EXPECT_TRUE(GetDoubleFromJson(in_i, &out));
+ EXPECT_EQ(-12, out);
+ EXPECT_TRUE(GetDoubleFromJson(in_u, &out));
+ EXPECT_EQ(34, out);
+ EXPECT_TRUE(GetDoubleFromJson(in_b, &out));
+ EXPECT_EQ(1, out);
+ EXPECT_TRUE(GetDoubleFromJson(in_d, &out));
+ EXPECT_EQ(1.2, out);
+ EXPECT_FALSE(GetDoubleFromJson(in_s, &out));
+}
+
+TEST(JsonTest, GetFromArray) {
+ Json::Value a, out;
+ a.append(in_s);
+ a.append(in_i);
+ a.append(in_u);
+ a.append(in_b);
+ EXPECT_TRUE(GetValueFromJsonArray(a, 0, &out));
+ EXPECT_TRUE(GetValueFromJsonArray(a, 3, &out));
+ EXPECT_FALSE(GetValueFromJsonArray(a, 99, &out));
+ EXPECT_FALSE(GetValueFromJsonArray(a, 0xFFFFFFFF, &out));
+}
+
+TEST(JsonTest, GetFromObject) {
+ Json::Value o, out;
+ o["string"] = in_s;
+ o["int"] = in_i;
+ o["uint"] = in_u;
+ o["bool"] = in_b;
+ EXPECT_TRUE(GetValueFromJsonObject(o, "int", &out));
+ EXPECT_TRUE(GetValueFromJsonObject(o, "bool", &out));
+ EXPECT_FALSE(GetValueFromJsonObject(o, "foo", &out));
+ EXPECT_FALSE(GetValueFromJsonObject(o, "", &out));
+}
+
+namespace {
+template <typename T>
+std::vector<T> VecOf3(const T& a, const T& b, const T& c) {
+ std::vector<T> in;
+ in.push_back(a);
+ in.push_back(b);
+ in.push_back(c);
+ return in;
+}
+template <typename T>
+Json::Value JsonVecOf3(const T& a, const T& b, const T& c) {
+ Json::Value in(Json::arrayValue);
+ in.append(a);
+ in.append(b);
+ in.append(c);
+ return in;
+}
+} // unnamed namespace
+
+TEST(JsonTest, ValueVectorToFromArray) {
+ std::vector<Json::Value> in = VecOf3<Json::Value>("a", "b", "c");
+ Json::Value out = ValueVectorToJsonArray(in);
+ EXPECT_EQ(in.size(), out.size());
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
+ EXPECT_EQ(in[i].asString(), out[i].asString());
+ }
+ Json::Value inj = JsonVecOf3<Json::Value>("a", "b", "c");
+ EXPECT_EQ(inj, out);
+ std::vector<Json::Value> outj;
+ EXPECT_TRUE(JsonArrayToValueVector(inj, &outj));
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
+ EXPECT_EQ(in[i], outj[i]);
+ }
+}
+
+TEST(JsonTest, IntVectorToFromArray) {
+ std::vector<int> in = VecOf3<int>(1, 2, 3);
+ Json::Value out = IntVectorToJsonArray(in);
+ EXPECT_EQ(in.size(), out.size());
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
+ EXPECT_EQ(in[i], out[i].asInt());
+ }
+ Json::Value inj = JsonVecOf3<int>(1, 2, 3);
+ EXPECT_EQ(inj, out);
+ std::vector<int> outj;
+ EXPECT_TRUE(JsonArrayToIntVector(inj, &outj));
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
+ EXPECT_EQ(in[i], outj[i]);
+ }
+}
+
+TEST(JsonTest, UIntVectorToFromArray) {
+ std::vector<unsigned int> in = VecOf3<unsigned int>(1, 2, 3);
+ Json::Value out = UIntVectorToJsonArray(in);
+ EXPECT_EQ(in.size(), out.size());
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
+ EXPECT_EQ(in[i], out[i].asUInt());
+ }
+ Json::Value inj = JsonVecOf3<unsigned int>(1, 2, 3);
+ EXPECT_EQ(inj, out);
+ std::vector<unsigned int> outj;
+ EXPECT_TRUE(JsonArrayToUIntVector(inj, &outj));
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
+ EXPECT_EQ(in[i], outj[i]);
+ }
+}
+
+TEST(JsonTest, StringVectorToFromArray) {
+ std::vector<std::string> in = VecOf3<std::string>("a", "b", "c");
+ Json::Value out = StringVectorToJsonArray(in);
+ EXPECT_EQ(in.size(), out.size());
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
+ EXPECT_EQ(in[i], out[i].asString());
+ }
+ Json::Value inj = JsonVecOf3<std::string>("a", "b", "c");
+ EXPECT_EQ(inj, out);
+ std::vector<std::string> outj;
+ EXPECT_TRUE(JsonArrayToStringVector(inj, &outj));
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
+ EXPECT_EQ(in[i], outj[i]);
+ }
+}
+
+TEST(JsonTest, BoolVectorToFromArray) {
+ std::vector<bool> in = VecOf3<bool>(false, true, false);
+ Json::Value out = BoolVectorToJsonArray(in);
+ EXPECT_EQ(in.size(), out.size());
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
+ EXPECT_EQ(in[i], out[i].asBool());
+ }
+ Json::Value inj = JsonVecOf3<bool>(false, true, false);
+ EXPECT_EQ(inj, out);
+ std::vector<bool> outj;
+ EXPECT_TRUE(JsonArrayToBoolVector(inj, &outj));
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
+ EXPECT_EQ(in[i], outj[i]);
+ }
+}
+
+TEST(JsonTest, DoubleVectorToFromArray) {
+ std::vector<double> in = VecOf3<double>(1.0, 2.0, 3.0);
+ Json::Value out = DoubleVectorToJsonArray(in);
+ EXPECT_EQ(in.size(), out.size());
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); ++i) {
+ EXPECT_EQ(in[i], out[i].asDouble());
+ }
+ Json::Value inj = JsonVecOf3<double>(1.0, 2.0, 3.0);
+ EXPECT_EQ(inj, out);
+ std::vector<double> outj;
+ EXPECT_TRUE(JsonArrayToDoubleVector(inj, &outj));
+ for (Json::Value::ArrayIndex i = 0; i < in.size(); i++) {
+ EXPECT_EQ(in[i], outj[i]);
+ }
+}
+
+} // namespace rtc