summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-01-23 00:47:34 +0100
committerAnna Henningsen <anna@addaleax.net>2019-01-27 20:38:41 +0100
commit08e9111696577c59b50cd8c9b4c3e2acf9710030 (patch)
tree49a855805cef62b58e2a7a197fff3bf15a70ef22 /src
parentc4216194be1b6856b9d02999ea0adc25cc036804 (diff)
downloadnode-new-08e9111696577c59b50cd8c9b4c3e2acf9710030.tar.gz
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 <cjihrig@gmail.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'src')
-rw-r--r--src/node_report.h101
1 files changed, 59 insertions, 42 deletions
diff --git a/src/node_report.h b/src/node_report.h
index 0ccc2321d4..ac4b304b77 100644
--- a/src/node_report.h
+++ b/src/node_report.h
@@ -19,6 +19,7 @@
#include <iomanip>
#include <iostream>
#include <sstream>
+#include <limits>
#ifdef _WIN32
#include <time.h>
@@ -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 <typename T>
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 <typename T>
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 <typename T, typename U>
- 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 <typename U>
- 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 <typename T,
+ typename test_for_number = typename std::
+ enable_if<std::numeric_limits<T>::is_specialized, bool>::type>
+ inline void write_value(T number) {
+ if (std::is_same<T, bool>::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