Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: move JSONWriter into its own file #32552

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@
'src/js_native_api_v8.h',
'src/js_native_api_v8_internals.h',
'src/js_stream.cc',
'src/json_utils.cc',
'src/module_wrap.cc',
'src/node.cc',
'src/node_api.cc',
Expand Down Expand Up @@ -645,6 +646,7 @@
'src/histogram.h',
'src/histogram-inl.h',
'src/js_stream.h',
'src/json_utils.h',
'src/large_pages/node_large_page.cc',
'src/large_pages/node_large_page.h',
'src/memory_tracker.h',
Expand Down Expand Up @@ -1142,7 +1144,7 @@
'test/cctest/test_linked_binding.cc',
'test/cctest/test_per_process.cc',
'test/cctest/test_platform.cc',
'test/cctest/test_report_util.cc',
'test/cctest/test_json_utils.cc',
'test/cctest/test_sockaddr.cc',
'test/cctest/test_traced_value.cc',
'test/cctest/test_util.cc',
Expand Down
67 changes: 67 additions & 0 deletions src/json_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "json_utils.h"

namespace node {

std::string EscapeJsonChars(const std::string& str) {
const std::string control_symbols[0x20] = {
"\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005",
"\\u0006", "\\u0007", "\\b", "\\t", "\\n", "\\v", "\\f", "\\r",
"\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013",
"\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019",
"\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f"
};

std::string ret;
size_t last_pos = 0;
size_t pos = 0;
for (; pos < str.size(); ++pos) {
std::string replace;
char ch = str[pos];
if (ch == '\\') {
replace = "\\\\";
} else if (ch == '\"') {
replace = "\\\"";
} else {
size_t num = static_cast<size_t>(ch);
if (num < 0x20) replace = control_symbols[num];
}
if (!replace.empty()) {
if (pos > last_pos) {
ret += str.substr(last_pos, pos - last_pos);
}
last_pos = pos + 1;
ret += replace;
}
}
// Append any remaining symbols.
if (last_pos < str.size()) {
ret += str.substr(last_pos, pos - last_pos);
}
return ret;
}

std::string Reindent(const std::string& str, int indent_depth) {
std::string indent;
for (int i = 0; i < indent_depth; i++) indent += ' ';

std::string out;
std::string::size_type pos = 0;
do {
std::string::size_type prev_pos = pos;
pos = str.find('\n', pos);

out.append(indent);

if (pos == std::string::npos) {
out.append(str, prev_pos, std::string::npos);
break;
} else {
pos++;
out.append(str, prev_pos, pos - prev_pos);
}
} while (true);

return out;
}

} // namespace node
160 changes: 160 additions & 0 deletions src/json_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#ifndef SRC_JSON_UTILS_H_
#define SRC_JSON_UTILS_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <iomanip>
#include <ostream>
#include <limits>

namespace node {

std::string EscapeJsonChars(const std::string& str);
std::string Reindent(const std::string& str, int indentation);

// JSON compiler definitions.
class JSONWriter {
public:
JSONWriter(std::ostream& out, bool compact)
: out_(out), compact_(compact) {}

private:
inline void indent() { indent_ += 2; }
inline void deindent() { indent_ -= 2; }
inline void advance() {
if (compact_) return;
for (int i = 0; i < indent_; i++) out_ << ' ';
}
inline void write_one_space() {
if (compact_) return;
out_ << ' ';
}
inline void write_new_line() {
if (compact_) return;
out_ << '\n';
}

public:
inline void json_start() {
if (state_ == kAfterValue) out_ << ',';
write_new_line();
advance();
out_ << '{';
indent();
state_ = kObjectStart;
}

inline void json_end() {
write_new_line();
deindent();
advance();
out_ << '}';
state_ = kAfterValue;
}
template <typename T>
inline void json_objectstart(T key) {
if (state_ == kAfterValue) out_ << ',';
write_new_line();
advance();
write_string(key);
out_ << ':';
write_one_space();
out_ << '{';
indent();
state_ = kObjectStart;
}

template <typename T>
inline void json_arraystart(T key) {
if (state_ == kAfterValue) out_ << ',';
write_new_line();
advance();
write_string(key);
out_ << ':';
write_one_space();
out_ << '[';
indent();
state_ = kObjectStart;
}
inline void json_objectend() {
write_new_line();
deindent();
advance();
out_ << '}';
if (indent_ == 0) {
// Top-level object is complete, so end the line.
out_ << '\n';
}
state_ = kAfterValue;
}

inline void json_arrayend() {
write_new_line();
deindent();
advance();
out_ << ']';
state_ = kAfterValue;
}
template <typename T, typename U>
inline void json_keyvalue(const T& key, const U& value) {
if (state_ == kAfterValue) out_ << ',';
write_new_line();
advance();
write_string(key);
out_ << ':';
write_one_space();
write_value(value);
state_ = kAfterValue;
}

template <typename U>
inline void json_element(const U& value) {
if (state_ == kAfterValue) out_ << ',';
write_new_line();
advance();
write_value(value);
state_ = kAfterValue;
}

struct Null {}; // Usable as a JSON value.

struct ForeignJSON {
std::string as_string;
};

private:
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(Null null) { out_ << "null"; }
inline void write_value(const char* str) { write_string(str); }
inline void write_value(const std::string& str) { write_string(str); }

inline void write_value(const ForeignJSON& json) {
out_ << Reindent(json.as_string, indent_);
}

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_;
bool compact_;
int indent_ = 0;
int state_ = kObjectStart;
};

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_JSON_UTILS_H_
3 changes: 3 additions & 0 deletions src/node_report.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "env-inl.h"
#include "json_utils.h"
#include "node_report.h"
#include "debug_utils-inl.h"
#include "diagnosticfilename-inl.h"
Expand All @@ -16,6 +17,7 @@
#include <dlfcn.h>
#endif

#include <iostream>
#include <cstring>
#include <ctime>
#include <cwctype>
Expand All @@ -30,6 +32,7 @@ using node::arraysize;
using node::ConditionVariable;
using node::DiagnosticFilename;
using node::Environment;
using node::JSONWriter;
using node::Mutex;
using node::NativeSymbolDebuggingContext;
using node::PerIsolateOptions;
Expand Down
Loading