GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/json_utils.h Lines: 98 98 100.0 %
Date: 2021-06-07 04:11:51 Branches: 85 124 68.5 %

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
24
  JSONWriter(std::ostream& out, bool compact)
20
24
    : out_(out), compact_(compact) {}
21
22
 private:
23
3624
  inline void indent() { indent_ += 2; }
24
3624
  inline void deindent() { indent_ -= 2; }
25
29037
  inline void advance() {
26
29037
    if (compact_) return;
27
27836
    for (int i = 0; i < indent_; i++) out_ << ' ';
28
  }
29
22409
  inline void write_one_space() {
30
22409
    if (compact_) return;
31
21487
    out_ << ' ';
32
  }
33
29037
  inline void write_new_line() {
34
29037
    if (compact_) return;
35
27836
    out_ << '\n';
36
  }
37
38
 public:
39
2660
  inline void json_start() {
40
2660
    if (state_ == kAfterValue) out_ << ',';
41
2660
    write_new_line();
42
2660
    advance();
43
2660
    out_ << '{';
44
2660
    indent();
45
2660
    state_ = kObjectStart;
46
2660
  }
47
48
2636
  inline void json_end() {
49
2636
    write_new_line();
50
2636
    deindent();
51
2636
    advance();
52
2636
    out_ << '}';
53
2636
    state_ = kAfterValue;
54
2636
  }
55
  template <typename T>
56
776
  inline void json_objectstart(T key) {
57
776
    if (state_ == kAfterValue) out_ << ',';
58
776
    write_new_line();
59
776
    advance();
60
776
    write_string(key);
61
776
    out_ << ':';
62
776
    write_one_space();
63
776
    out_ << '{';
64
776
    indent();
65
776
    state_ = kObjectStart;
66
776
  }
67
68
  template <typename T>
69
188
  inline void json_arraystart(T key) {
70
188
    if (state_ == kAfterValue) out_ << ',';
71
188
    write_new_line();
72
188
    advance();
73
188
    write_string(key);
74
188
    out_ << ':';
75
188
    write_one_space();
76
188
    out_ << '[';
77
188
    indent();
78
188
    state_ = kObjectStart;
79
188
  }
80
800
  inline void json_objectend() {
81
800
    write_new_line();
82
800
    deindent();
83
800
    advance();
84
800
    out_ << '}';
85
800
    if (indent_ == 0) {
86
      // Top-level object is complete, so end the line.
87
24
      out_ << '\n';
88
    }
89
800
    state_ = kAfterValue;
90
800
  }
91
92
188
  inline void json_arrayend() {
93
188
    write_new_line();
94
188
    deindent();
95
188
    advance();
96
188
    out_ << ']';
97
188
    state_ = kAfterValue;
98
188
  }
99
  template <typename T, typename U>
100
21445
  inline void json_keyvalue(const T& key, const U& value) {
101

























21445
    if (state_ == kAfterValue) out_ << ',';
102
21445
    write_new_line();
103
21445
    advance();
104
21445
    write_string(key);
105
21445
    out_ << ':';
106
21445
    write_one_space();
107
21445
    write_value(value);
108
21445
    state_ = kAfterValue;
109
21445
  }
110
111
  template <typename U>
112
344
  inline void json_element(const U& value) {
113

344
    if (state_ == kAfterValue) out_ << ',';
114
344
    write_new_line();
115
344
    advance();
116
344
    write_value(value);
117
344
    state_ = kAfterValue;
118
344
  }
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
15150
  inline void write_value(T number) {
131
    if (std::is_same<T, bool>::value)
132
610
      out_ << (number ? "true" : "false");
133
    else
134
14540
      out_ << number;
135
15150
  }
136
137
23
  inline void write_value(Null null) { out_ << "null"; }
138
5058
  inline void write_value(const char* str) { write_string(str); }
139
1556
  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
29023
  inline void write_string(const std::string& str) {
146
29023
    out_ << '"' << EscapeJsonChars(str) << '"';
147
29023
  }
148
27455
  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_