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