From 1dfdbc6cf709ff2741a0cef207e833748c23dc59 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 23 Jan 2019 00:47:34 +0100 Subject: [PATCH] report: refactor JSON writer - Support non-string entry types - Prefer single-character writes over string writes - Rename the state constants and adjust style to match more common Node.js style PR-URL: https://github.com/nodejs/node/pull/25651 Reviewed-By: Colin Ihrig Reviewed-By: Matheus Marchini Reviewed-By: Gireesh Punathil Reviewed-By: Beth Griggs Reviewed-By: Richard Lau Reviewed-By: Michael Dawson --- src/node_report.h | 101 +++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 42 deletions(-) diff --git a/src/node_report.h b/src/node_report.h index 0ccc2321d4a..ac4b304b77d 100644 --- a/src/node_report.h +++ b/src/node_report.h @@ -19,6 +19,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -67,93 +68,109 @@ extern double prog_start_time; // JSON compiler definitions. class JSONWriter { public: - explicit JSONWriter(std::ostream& out) - : out_(out), indent_(0), state_(JSONOBJECT) {} + explicit JSONWriter(std::ostream& out) : out_(out) {} inline void indent() { indent_ += 2; } inline void deindent() { indent_ -= 2; } inline void advance() { - for (int i = 0; i < indent_; i++) out_ << " "; + for (int i = 0; i < indent_; i++) out_ << ' '; } inline void json_start() { - if (state_ == JSONVALUE) out_ << ","; - out_ << "\n"; + if (state_ == kAfterValue) out_ << ','; + out_ << '\n'; advance(); - out_ << "{"; + out_ << '{'; indent(); - state_ = JSONOBJECT; + state_ = kObjectStart; } inline void json_end() { - out_ << "\n"; + out_ << '\n'; deindent(); advance(); - out_ << "}"; - state_ = JSONVALUE; + out_ << '}'; + state_ = kAfterValue; } template inline void json_objectstart(T key) { - if (state_ == JSONVALUE) out_ << ","; - out_ << "\n"; + if (state_ == kAfterValue) out_ << ','; + out_ << '\n'; advance(); - out_ << "\"" << key << "\"" - << ": {"; + write_string(key); + out_ << ": {"; indent(); - state_ = JSONOBJECT; + state_ = kObjectStart; } template inline void json_arraystart(T key) { - if (state_ == JSONVALUE) out_ << ","; - out_ << "\n"; + if (state_ == kAfterValue) out_ << ','; + out_ << '\n'; advance(); - out_ << "\"" << key << "\"" - << ": ["; + write_string(key); + out_ << ": ["; indent(); - state_ = JSONOBJECT; + state_ = kObjectStart; } inline void json_objectend() { - out_ << "\n"; + out_ << '\n'; deindent(); advance(); - out_ << "}"; - state_ = JSONVALUE; + out_ << '}'; + state_ = kAfterValue; } inline void json_arrayend() { - out_ << "\n"; + out_ << '\n'; deindent(); advance(); - out_ << "]"; - state_ = JSONVALUE; + out_ << ']'; + state_ = kAfterValue; } template - inline void json_keyvalue(T key, U value) { - if (state_ == JSONVALUE) out_ << ","; - out_ << "\n"; + inline void json_keyvalue(const T& key, const U& value) { + if (state_ == kAfterValue) out_ << ','; + out_ << '\n'; advance(); - out_ << "\"" << key << "\"" - << ": " - << "\""; - out_ << EscapeJsonChars(value) << "\""; - state_ = JSONVALUE; + write_string(key); + out_ << ": "; + write_value(value); + state_ = kAfterValue; } template - inline void json_element(U value) { - if (state_ == JSONVALUE) out_ << ","; - out_ << "\n"; + inline void json_element(const U& value) { + if (state_ == kAfterValue) out_ << ','; + out_ << '\n'; advance(); - out_ << "\"" << EscapeJsonChars(value) << "\""; - state_ = JSONVALUE; + write_value(value); + state_ = kAfterValue; } private: - enum JSONState { JSONOBJECT, JSONVALUE }; + template ::is_specialized, bool>::type> + inline void write_value(T number) { + if (std::is_same::value) + out_ << (number ? "true" : "false"); + else + out_ << number; + } + + inline void write_value(const char* str) { write_string(str); } + inline void write_value(const std::string& str) { write_string(str); } + + inline void write_string(const std::string& str) { + out_ << '"' << EscapeJsonChars(str) << '"'; + } + inline void write_string(const char* str) { write_string(std::string(str)); } + + enum JSONState { kObjectStart, kAfterValue }; std::ostream& out_; - int indent_; - int state_; + int indent_ = 0; + int state_ = kObjectStart; }; } // namespace report