|
| 1 | +#ifndef SRC_JSON_UTILS_H_ |
| 2 | +#define SRC_JSON_UTILS_H_ |
| 3 | + |
| 4 | +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
| 5 | + |
| 6 | +#include <iomanip> |
| 7 | +#include <ostream> |
| 8 | +#include <limits> |
| 9 | + |
| 10 | +namespace node { |
| 11 | + |
| 12 | +std::string EscapeJsonChars(const std::string& str); |
| 13 | +std::string Reindent(const std::string& str, int indentation); |
| 14 | + |
| 15 | +// JSON compiler definitions. |
| 16 | +class JSONWriter { |
| 17 | + public: |
| 18 | + JSONWriter(std::ostream& out, bool compact) |
| 19 | + : out_(out), compact_(compact) {} |
| 20 | + |
| 21 | + private: |
| 22 | + inline void indent() { indent_ += 2; } |
| 23 | + inline void deindent() { indent_ -= 2; } |
| 24 | + inline void advance() { |
| 25 | + if (compact_) return; |
| 26 | + for (int i = 0; i < indent_; i++) out_ << ' '; |
| 27 | + } |
| 28 | + inline void write_one_space() { |
| 29 | + if (compact_) return; |
| 30 | + out_ << ' '; |
| 31 | + } |
| 32 | + inline void write_new_line() { |
| 33 | + if (compact_) return; |
| 34 | + out_ << '\n'; |
| 35 | + } |
| 36 | + |
| 37 | + public: |
| 38 | + inline void json_start() { |
| 39 | + if (state_ == kAfterValue) out_ << ','; |
| 40 | + write_new_line(); |
| 41 | + advance(); |
| 42 | + out_ << '{'; |
| 43 | + indent(); |
| 44 | + state_ = kObjectStart; |
| 45 | + } |
| 46 | + |
| 47 | + inline void json_end() { |
| 48 | + write_new_line(); |
| 49 | + deindent(); |
| 50 | + advance(); |
| 51 | + out_ << '}'; |
| 52 | + state_ = kAfterValue; |
| 53 | + } |
| 54 | + template <typename T> |
| 55 | + inline void json_objectstart(T key) { |
| 56 | + if (state_ == kAfterValue) out_ << ','; |
| 57 | + write_new_line(); |
| 58 | + advance(); |
| 59 | + write_string(key); |
| 60 | + out_ << ':'; |
| 61 | + write_one_space(); |
| 62 | + out_ << '{'; |
| 63 | + indent(); |
| 64 | + state_ = kObjectStart; |
| 65 | + } |
| 66 | + |
| 67 | + template <typename T> |
| 68 | + inline void json_arraystart(T key) { |
| 69 | + if (state_ == kAfterValue) out_ << ','; |
| 70 | + write_new_line(); |
| 71 | + advance(); |
| 72 | + write_string(key); |
| 73 | + out_ << ':'; |
| 74 | + write_one_space(); |
| 75 | + out_ << '['; |
| 76 | + indent(); |
| 77 | + state_ = kObjectStart; |
| 78 | + } |
| 79 | + inline void json_objectend() { |
| 80 | + write_new_line(); |
| 81 | + deindent(); |
| 82 | + advance(); |
| 83 | + out_ << '}'; |
| 84 | + if (indent_ == 0) { |
| 85 | + // Top-level object is complete, so end the line. |
| 86 | + out_ << '\n'; |
| 87 | + } |
| 88 | + state_ = kAfterValue; |
| 89 | + } |
| 90 | + |
| 91 | + inline void json_arrayend() { |
| 92 | + write_new_line(); |
| 93 | + deindent(); |
| 94 | + advance(); |
| 95 | + out_ << ']'; |
| 96 | + state_ = kAfterValue; |
| 97 | + } |
| 98 | + template <typename T, typename U> |
| 99 | + inline void json_keyvalue(const T& key, const U& value) { |
| 100 | + if (state_ == kAfterValue) out_ << ','; |
| 101 | + write_new_line(); |
| 102 | + advance(); |
| 103 | + write_string(key); |
| 104 | + out_ << ':'; |
| 105 | + write_one_space(); |
| 106 | + write_value(value); |
| 107 | + state_ = kAfterValue; |
| 108 | + } |
| 109 | + |
| 110 | + template <typename U> |
| 111 | + inline void json_element(const U& value) { |
| 112 | + if (state_ == kAfterValue) out_ << ','; |
| 113 | + write_new_line(); |
| 114 | + advance(); |
| 115 | + write_value(value); |
| 116 | + state_ = kAfterValue; |
| 117 | + } |
| 118 | + |
| 119 | + struct Null {}; // Usable as a JSON value. |
| 120 | + |
| 121 | + struct ForeignJSON { |
| 122 | + std::string as_string; |
| 123 | + }; |
| 124 | + |
| 125 | + private: |
| 126 | + template <typename T, |
| 127 | + typename test_for_number = typename std:: |
| 128 | + enable_if<std::numeric_limits<T>::is_specialized, bool>::type> |
| 129 | + inline void write_value(T number) { |
| 130 | + if (std::is_same<T, bool>::value) |
| 131 | + out_ << (number ? "true" : "false"); |
| 132 | + else |
| 133 | + out_ << number; |
| 134 | + } |
| 135 | + |
| 136 | + inline void write_value(Null null) { out_ << "null"; } |
| 137 | + inline void write_value(const char* str) { write_string(str); } |
| 138 | + inline void write_value(const std::string& str) { write_string(str); } |
| 139 | + |
| 140 | + inline void write_value(const ForeignJSON& json) { |
| 141 | + out_ << Reindent(json.as_string, indent_); |
| 142 | + } |
| 143 | + |
| 144 | + inline void write_string(const std::string& str) { |
| 145 | + out_ << '"' << EscapeJsonChars(str) << '"'; |
| 146 | + } |
| 147 | + inline void write_string(const char* str) { write_string(std::string(str)); } |
| 148 | + |
| 149 | + enum JSONState { kObjectStart, kAfterValue }; |
| 150 | + std::ostream& out_; |
| 151 | + bool compact_; |
| 152 | + int indent_ = 0; |
| 153 | + int state_ = kObjectStart; |
| 154 | +}; |
| 155 | + |
| 156 | +} // namespace node |
| 157 | + |
| 158 | +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
| 159 | + |
| 160 | +#endif // SRC_JSON_UTILS_H_ |
0 commit comments