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 |
35 |
JSONWriter(std::ostream& out, bool compact) |
|
20 |
35 |
: out_(out), compact_(compact) {} |
|
21 |
|||
22 |
private: |
||
23 |
5280 |
inline void indent() { indent_ += 2; } |
|
24 |
5280 |
inline void deindent() { indent_ -= 2; } |
|
25 |
42463 |
inline void advance() { |
|
26 |
✓✓ | 42463 |
if (compact_) return; |
27 |
✓✓ | 41159 |
for (int i = 0; i < indent_; i++) out_ << ' '; |
28 |
} |
||
29 |
32730 |
inline void write_one_space() { |
|
30 |
✓✓ | 32730 |
if (compact_) return; |
31 |
31745 |
out_ << ' '; |
|
32 |
} |
||
33 |
42463 |
inline void write_new_line() { |
|
34 |
✓✓ | 42463 |
if (compact_) return; |
35 |
41159 |
out_ << '\n'; |
|
36 |
} |
||
37 |
|||
38 |
public: |
||
39 |
3926 |
inline void json_start() { |
|
40 |
✓✓ | 3926 |
if (state_ == kAfterValue) out_ << ','; |
41 |
3926 |
write_new_line(); |
|
42 |
3926 |
advance(); |
|
43 |
3926 |
out_ << '{'; |
|
44 |
3926 |
indent(); |
|
45 |
3926 |
state_ = kObjectStart; |
|
46 |
3926 |
} |
|
47 |
|||
48 |
3891 |
inline void json_end() { |
|
49 |
3891 |
write_new_line(); |
|
50 |
3891 |
deindent(); |
|
51 |
3891 |
advance(); |
|
52 |
3891 |
out_ << '}'; |
|
53 |
3891 |
state_ = kAfterValue; |
|
54 |
3891 |
} |
|
55 |
template <typename T> |
||
56 |
1085 |
inline void json_objectstart(T key) { |
|
57 |
✓✓ | 1085 |
if (state_ == kAfterValue) out_ << ','; |
58 |
1085 |
write_new_line(); |
|
59 |
1085 |
advance(); |
|
60 |
1085 |
write_string(key); |
|
61 |
1085 |
out_ << ':'; |
|
62 |
1085 |
write_one_space(); |
|
63 |
1085 |
out_ << '{'; |
|
64 |
1085 |
indent(); |
|
65 |
1085 |
state_ = kObjectStart; |
|
66 |
1085 |
} |
|
67 |
|||
68 |
template <typename T> |
||
69 |
269 |
inline void json_arraystart(T key) { |
|
70 |
✓✗ | 269 |
if (state_ == kAfterValue) out_ << ','; |
71 |
269 |
write_new_line(); |
|
72 |
269 |
advance(); |
|
73 |
269 |
write_string(key); |
|
74 |
269 |
out_ << ':'; |
|
75 |
269 |
write_one_space(); |
|
76 |
269 |
out_ << '['; |
|
77 |
269 |
indent(); |
|
78 |
269 |
state_ = kObjectStart; |
|
79 |
269 |
} |
|
80 |
1120 |
inline void json_objectend() { |
|
81 |
1120 |
write_new_line(); |
|
82 |
1120 |
deindent(); |
|
83 |
1120 |
advance(); |
|
84 |
1120 |
out_ << '}'; |
|
85 |
✓✓ | 1120 |
if (indent_ == 0) { |
86 |
// Top-level object is complete, so end the line. |
||
87 |
35 |
out_ << '\n'; |
|
88 |
} |
||
89 |
1120 |
state_ = kAfterValue; |
|
90 |
1120 |
} |
|
91 |
|||
92 |
269 |
inline void json_arrayend() { |
|
93 |
269 |
write_new_line(); |
|
94 |
269 |
deindent(); |
|
95 |
269 |
advance(); |
|
96 |
269 |
out_ << ']'; |
|
97 |
269 |
state_ = kAfterValue; |
|
98 |
269 |
} |
|
99 |
template <typename T, typename U> |
||
100 |
31376 |
inline void json_keyvalue(const T& key, const U& value) { |
|
101 |
✓✗✓✓ ✓✓✓✗ ✗✓✓✓ ✓✗✓✗ ✓✗✓✗ ✓✓✓✗ ✓✗✓✗ ✓✓✓✗ ✓✗✓✓ ✓✗✓✗ ✓✓✓✗ ✓✓✓✓ ✓✓✗✗ ✓✗✓✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✓ ✓✗✗✓ ✓✗✓✗ ✓✗✓✓ ✓✗✓✗ ✓✗✓✗ ✓✓✓✗ ✗✓✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✓✓✗ ✗✓ |
31376 |
if (state_ == kAfterValue) out_ << ','; |
102 |
31376 |
write_new_line(); |
|
103 |
31376 |
advance(); |
|
104 |
31376 |
write_string(key); |
|
105 |
31376 |
out_ << ':'; |
|
106 |
31376 |
write_one_space(); |
|
107 |
31376 |
write_value(value); |
|
108 |
31376 |
state_ = kAfterValue; |
|
109 |
31376 |
} |
|
110 |
|||
111 |
template <typename U> |
||
112 |
527 |
inline void json_element(const U& value) { |
|
113 |
✗✓✗✓ ✓✓ |
527 |
if (state_ == kAfterValue) out_ << ','; |
114 |
527 |
write_new_line(); |
|
115 |
527 |
advance(); |
|
116 |
527 |
write_value(value); |
|
117 |
527 |
state_ = kAfterValue; |
|
118 |
527 |
} |
|
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 |
22081 |
inline void write_value(T number) { |
|
131 |
if constexpr (std::is_same<T, bool>::value) |
||
132 |
✓✓ | 788 |
out_ << (number ? "true" : "false"); |
133 |
else |
||
134 |
21293 |
out_ << number; |
|
135 |
22081 |
} |
|
136 |
|||
137 |
45 |
inline void write_value(Null null) { out_ << "null"; } |
|
138 |
7348 |
inline void write_value(const char* str) { write_string(str); } |
|
139 |
2427 |
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 |
42505 |
inline void write_string(const std::string& str) { |
|
146 |
42505 |
out_ << '"' << EscapeJsonChars(str) << '"'; |
|
147 |
42505 |
} |
|
148 |
40065 |
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 4.2) |