GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
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 |
22 |
JSONWriter(std::ostream& out, bool compact) |
|
20 |
22 |
: out_(out), compact_(compact) {} |
|
21 |
|||
22 |
private: |
||
23 |
3278 |
inline void indent() { indent_ += 2; } |
|
24 |
3278 |
inline void deindent() { indent_ -= 2; } |
|
25 |
26312 |
inline void advance() { |
|
26 |
✓✓ | 26312 |
if (compact_) return; |
27 |
✓✓ | 25124 |
for (int i = 0; i < indent_; i++) out_ << ' '; |
28 |
} |
||
29 |
20316 |
inline void write_one_space() { |
|
30 |
✓✓ | 20316 |
if (compact_) return; |
31 |
19403 |
out_ << ' '; |
|
32 |
} |
||
33 |
26312 |
inline void write_new_line() { |
|
34 |
✓✓ | 26312 |
if (compact_) return; |
35 |
25124 |
out_ << '\n'; |
|
36 |
} |
||
37 |
|||
38 |
public: |
||
39 |
2394 |
inline void json_start() { |
|
40 |
✓✓ | 2394 |
if (state_ == kAfterValue) out_ << ','; |
41 |
2394 |
write_new_line(); |
|
42 |
2394 |
advance(); |
|
43 |
2394 |
out_ << '{'; |
|
44 |
2394 |
indent(); |
|
45 |
2394 |
state_ = kObjectStart; |
|
46 |
2394 |
} |
|
47 |
|||
48 |
2372 |
inline void json_end() { |
|
49 |
2372 |
write_new_line(); |
|
50 |
2372 |
deindent(); |
|
51 |
2372 |
advance(); |
|
52 |
2372 |
out_ << '}'; |
|
53 |
2372 |
state_ = kAfterValue; |
|
54 |
2372 |
} |
|
55 |
template <typename T> |
||
56 |
712 |
inline void json_objectstart(T key) { |
|
57 |
✓✓ | 712 |
if (state_ == kAfterValue) out_ << ','; |
58 |
712 |
write_new_line(); |
|
59 |
712 |
advance(); |
|
60 |
712 |
write_string(key); |
|
61 |
712 |
out_ << ':'; |
|
62 |
712 |
write_one_space(); |
|
63 |
712 |
out_ << '{'; |
|
64 |
712 |
indent(); |
|
65 |
712 |
state_ = kObjectStart; |
|
66 |
712 |
} |
|
67 |
|||
68 |
template <typename T> |
||
69 |
172 |
inline void json_arraystart(T key) { |
|
70 |
✓✗ | 172 |
if (state_ == kAfterValue) out_ << ','; |
71 |
172 |
write_new_line(); |
|
72 |
172 |
advance(); |
|
73 |
172 |
write_string(key); |
|
74 |
172 |
out_ << ':'; |
|
75 |
172 |
write_one_space(); |
|
76 |
172 |
out_ << '['; |
|
77 |
172 |
indent(); |
|
78 |
172 |
state_ = kObjectStart; |
|
79 |
172 |
} |
|
80 |
734 |
inline void json_objectend() { |
|
81 |
734 |
write_new_line(); |
|
82 |
734 |
deindent(); |
|
83 |
734 |
advance(); |
|
84 |
734 |
out_ << '}'; |
|
85 |
✓✓ | 734 |
if (indent_ == 0) { |
86 |
// Top-level object is complete, so end the line. |
||
87 |
22 |
out_ << '\n'; |
|
88 |
} |
||
89 |
734 |
state_ = kAfterValue; |
|
90 |
734 |
} |
|
91 |
|||
92 |
172 |
inline void json_arrayend() { |
|
93 |
172 |
write_new_line(); |
|
94 |
172 |
deindent(); |
|
95 |
172 |
advance(); |
|
96 |
172 |
out_ << ']'; |
|
97 |
172 |
state_ = kAfterValue; |
|
98 |
172 |
} |
|
99 |
template <typename T, typename U> |
||
100 |
19432 |
inline void json_keyvalue(const T& key, const U& value) { |
|
101 |
✓✗✓✓ ✓✓✓✗ ✓✓✓✓ ✓✗✓✗ ✓✗✓✗ ✓✓✓✗ ✓✗✓✓ ✓✗✓✓ ✓✓✓✓ ✓✓✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✓ ✓✗✗✓ ✓✗✓✗ ✓✗✓✓ ✓✗✓✗ ✓✗✓✗ ✓✓✓✗ ✗✓✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✗✓ |
19432 |
if (state_ == kAfterValue) out_ << ','; |
102 |
19432 |
write_new_line(); |
|
103 |
19432 |
advance(); |
|
104 |
19432 |
write_string(key); |
|
105 |
19432 |
out_ << ':'; |
|
106 |
19432 |
write_one_space(); |
|
107 |
19432 |
write_value(value); |
|
108 |
19432 |
state_ = kAfterValue; |
|
109 |
19432 |
} |
|
110 |
|||
111 |
template <typename U> |
||
112 |
324 |
inline void json_element(const U& value) { |
|
113 |
✗✓✓✓ |
324 |
if (state_ == kAfterValue) out_ << ','; |
114 |
324 |
write_new_line(); |
|
115 |
324 |
advance(); |
|
116 |
324 |
write_value(value); |
|
117 |
324 |
state_ = kAfterValue; |
|
118 |
324 |
} |
|
119 |
|||
120 |
struct Null {}; // Usable as a JSON value. |
||
121 |
|||
122 |
2 |
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 |
13783 |
inline void write_value(T number) { |
|
131 |
if (std::is_same<T, bool>::value) |
||
132 |
✓✓ | 463 |
out_ << (number ? "true" : "false"); |
133 |
else |
||
134 |
13320 |
out_ << number; |
|
135 |
13783 |
} |
|
136 |
|||
137 |
12 |
inline void write_value(Null null) { out_ << "null"; } |
|
138 |
4611 |
inline void write_value(const char* str) { write_string(str); } |
|
139 |
1348 |
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 |
26275 |
inline void write_string(const std::string& str) { |
|
146 |
26275 |
out_ << '"' << EscapeJsonChars(str) << '"'; |
|
147 |
26275 |
} |
|
148 |
24915 |
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_ |
Generated by: GCOVR (Version 3.4) |