GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — node/src/json_utils.h at main · Varstahl/node Expand file tree/ Copy pathjson_utils.hMore file actions 172 lines (150 loc) · 3.8 KB / Copy pathjson_utils.hFile metadata and controls172 lines (150 loc) · 3.8 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 #ifndef SRC_JSON_UTILS_H_ #define SRC_JSON_UTILS_H_ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include <iomanip> #include <limits> #include <ostream> #include <string> #include <string_view> namespace node { constexpr bool NeedsJsonEscape(std::string_view str) { for (const char c : str) { if (c == '\\' || c == '"' || c < 0x20) return true; } return false; } std::string EscapeJsonChars(std::string_view 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 constexpr (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(std::string_view str) { write_string(str); } inline void write_value(const ForeignJSON& json) { out_ << Reindent(json.as_string, indent_); } inline void write_string(std::string_view str) { out_ << '"'; if (NeedsJsonEscape(str)) // only create temporary std::string if necessary out_ << EscapeJsonChars(str); else out_ << str; out_ << '"'; } 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_ |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |