Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Unified Diff: webrtc/stats/rtcstats.cc

Issue 2983243002: Make RTCStatsReport::ToString() return JSON-parseable string. (Closed)
Patch Set: Replace ToString with ToJson in test files. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/stats/BUILD.gn ('k') | webrtc/stats/rtcstats_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/stats/rtcstats.cc
diff --git a/webrtc/stats/rtcstats.cc b/webrtc/stats/rtcstats.cc
index 5d0a937e6f31c12abadc55fa95be9adfb4fac6da..0ef49c1a5ecce739a9ea664050744d49c15596b0 100644
--- a/webrtc/stats/rtcstats.cc
+++ b/webrtc/stats/rtcstats.cc
@@ -10,6 +10,7 @@
#include "webrtc/api/stats/rtcstats.h"
+#include <iomanip>
#include <sstream>
#include "webrtc/rtc_base/stringencode.h"
@@ -18,33 +19,56 @@ namespace webrtc {
namespace {
-// Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type|
+// Produces "[a,b,c]". Works for non-vector |RTCStatsMemberInterface::Type|
// types.
template<typename T>
std::string VectorToString(const std::vector<T>& vector) {
if (vector.empty())
- return "{}";
+ return "[]";
std::ostringstream oss;
- oss << "{ " << rtc::ToString<T>(vector[0]);
+ oss << "[" << rtc::ToString<T>(vector[0]);
for (size_t i = 1; i < vector.size(); ++i) {
- oss << ", " << rtc::ToString<T>(vector[i]);
+ oss << "," << rtc::ToString<T>(vector[i]);
}
- oss << " }";
+ oss << "]";
return oss.str();
}
-// Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and
+// Produces "[\"a\",\"b\",\"c\"]". Works for vectors of both const char* and
// std::string element types.
template<typename T>
std::string VectorOfStringsToString(const std::vector<T>& strings) {
if (strings.empty())
- return "{}";
+ return "[]";
std::ostringstream oss;
- oss << "{ \"" << rtc::ToString<T>(strings[0]) << '\"';
+ oss << "[\"" << rtc::ToString<T>(strings[0]) << '\"';
for (size_t i = 1; i < strings.size(); ++i) {
- oss << ", \"" << rtc::ToString<T>(strings[i]) << '\"';
+ oss << ",\"" << rtc::ToString<T>(strings[i]) << '\"';
}
- oss << " }";
+ oss << "]";
+ return oss.str();
+}
+
+template <typename T>
+std::string ToStringAsDouble(const T value) {
+ // JSON represents numbers as floating point numbers with about 15 decimal
+ // digits of precision.
+ const int JSON_PRECISION = 16;
+ std::ostringstream oss;
+ oss << std::setprecision(JSON_PRECISION) << static_cast<double>(value);
+ return oss.str();
+}
+
+template <typename T>
+std::string VectorToStringAsDouble(const std::vector<T>& vector) {
+ if (vector.empty())
+ return "[]";
+ std::ostringstream oss;
+ oss << "[" << ToStringAsDouble<T>(vector[0]);
+ for (size_t i = 1; i < vector.size(); ++i) {
+ oss << "," << ToStringAsDouble<T>(vector[i]);
+ }
+ oss << "]";
return oss.str();
}
@@ -71,22 +95,21 @@ bool RTCStats::operator!=(const RTCStats& other) const {
return !(*this == other);
}
-std::string RTCStats::ToString() const {
+std::string RTCStats::ToJson() const {
std::ostringstream oss;
- oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: "
- << timestamp_us_ << '\n';
+ oss << "{\"type\":\"" << type() << "\","
+ << "\"id\":\"" << id_ << "\","
+ << "\"timestamp\":" << timestamp_us_;
for (const RTCStatsMemberInterface* member : Members()) {
- oss << " " << member->name() << ": ";
if (member->is_defined()) {
+ oss << ",\"" << member->name() << "\":";
if (member->is_string())
- oss << '"' << member->ValueToString() << "\"\n";
+ oss << '"' << member->ValueToJson() << '"';
else
- oss << member->ValueToString() << '\n';
- } else {
- oss << "undefined\n";
+ oss << member->ValueToJson();
}
}
- oss << '}';
+ oss << "}";
return oss.str();
}
@@ -102,54 +125,112 @@ RTCStats::MembersOfThisObjectAndAncestors(
return members;
}
-#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str) \
- template<> \
+#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \
+ template <> \
const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
RTCStatsMemberInterface::type; \
- template<> \
- bool RTCStatsMember<T>::is_sequence() const { return is_seq; } \
- template<> \
- bool RTCStatsMember<T>::is_string() const { return is_str; } \
- template<> \
+ template <> \
+ bool RTCStatsMember<T>::is_sequence() const { \
+ return is_seq; \
+ } \
+ template <> \
+ bool RTCStatsMember<T>::is_string() const { \
+ return is_str; \
+ } \
+ template <> \
std::string RTCStatsMember<T>::ValueToString() const { \
RTC_DCHECK(is_defined_); \
return to_str; \
+ } \
+ template <> \
+ std::string RTCStatsMember<T>::ValueToJson() const { \
+ RTC_DCHECK(is_defined_); \
+ return to_json; \
}
-WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false,
+WEBRTC_DEFINE_RTCSTATSMEMBER(bool,
+ kBool,
+ false,
+ false,
+ rtc::ToString(value_),
rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false,
+WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t,
+ kInt32,
+ false,
+ false,
+ rtc::ToString(value_),
rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false,
+WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t,
+ kUint32,
+ false,
+ false,
+ rtc::ToString(value_),
rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true,
+WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t,
+ kInt64,
+ false,
+ false,
+ rtc::ToString(value_),
+ ToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t,
+ kUint64,
+ false,
+ false,
+ rtc::ToString(value_),
+ ToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(double,
+ kDouble,
+ false,
+ false,
+ rtc::ToString(value_),
+ ToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::string,
+ kString,
+ false,
+ true,
+ value_,
value_);
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<bool>, kSequenceBool, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<int32_t>, kSequenceInt32, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<uint32_t>, kSequenceUint32, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<int64_t>, kSequenceInt64, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<uint64_t>, kSequenceUint64, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<double>, kSequenceDouble, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<std::string>, kSequenceString, true, false,
- VectorOfStringsToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>,
+ kSequenceBool,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>,
+ kSequenceInt32,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>,
+ kSequenceUint32,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>,
+ kSequenceInt64,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>,
+ kSequenceUint64,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>,
+ kSequenceDouble,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>,
+ kSequenceString,
+ true,
+ false,
+ VectorOfStringsToString(value_),
+ VectorOfStringsToString(value_));
} // namespace webrtc
« no previous file with comments | « webrtc/stats/BUILD.gn ('k') | webrtc/stats/rtcstats_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698