GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: json_utils.h Lines: 96 96 100.0 %
Date: 2022-08-30 04:20:47 Branches: 21 22 95.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
33
  JSONWriter(std::ostream& out, bool compact)
20
33
    : out_(out), compact_(compact) {}
21
22
 private:
23
4965
  inline void indent() { indent_ += 2; }
24
4965
  inline void deindent() { indent_ -= 2; }
25
39931
  inline void advance() {
26
39931
    if (compact_) return;
27
298345
    for (int i = 0; i < indent_; i++) out_ << ' ';
28
  }
29
30819
  inline void write_one_space() {
30
30819
    if (compact_) return;
31
29855
    out_ << ' ';
32
  }
33
39931
  inline void write_new_line() {
34
39931
    if (compact_) return;
35
38659
    out_ << '\n';
36
  }
37
38
 public:
39
3691
  inline void json_start() {
40
3691
    if (state_ == kAfterValue) out_ << ',';
41
3691
    write_new_line();
42
3691
    advance();
43
3691
    out_ << '{';
44
3691
    indent();
45
3691
    state_ = kObjectStart;
46
3691
  }
47
48
3658
  inline void json_end() {
49
3658
    write_new_line();
50
3658
    deindent();
51
3658
    advance();
52
3658
    out_ << '}';
53
3658
    state_ = kAfterValue;
54
3658
  }
55
  template <typename T>
56
1021
  inline void json_objectstart(T key) {
57
1021
    if (state_ == kAfterValue) out_ << ',';
58
1021
    write_new_line();
59
1021
    advance();
60
1021
    write_string(key);
61
1021
    out_ << ':';
62
1021
    write_one_space();
63
1021
    out_ << '{';
64
1021
    indent();
65
1021
    state_ = kObjectStart;
66
1021
  }
67
68
  template <typename T>
69
253
  inline void json_arraystart(T key) {
70
253
    if (state_ == kAfterValue) out_ << ',';
71
253
    write_new_line();
72
253
    advance();
73
253
    write_string(key);
74
253
    out_ << ':';
75
253
    write_one_space();
76
253
    out_ << '[';
77
253
    indent();
78
253
    state_ = kObjectStart;
79
253
  }
80
1054
  inline void json_objectend() {
81
1054
    write_new_line();
82
1054
    deindent();
83
1054
    advance();
84
1054
    out_ << '}';
85
1054
    if (indent_ == 0) {
86
      // Top-level object is complete, so end the line.
87
33
      out_ << '\n';
88
    }
89
1054
    state_ = kAfterValue;
90
1054
  }
91
92
253
  inline void json_arrayend() {
93
253
    write_new_line();
94
253
    deindent();
95
253
    advance();
96
253
    out_ << ']';
97
253
    state_ = kAfterValue;
98
253
  }
99
  template <typename T, typename U>
100
59090
  inline void json_keyvalue(const T& key, const U& value) {
101
59090
    if (state_ == kAfterValue) out_ << ',';
102
59090
    write_new_line();
103
59090
    advance();
104
59090
    write_string(key);
105
59090
    out_ << ':';
106
59090
    write_one_space();
107
59090
    write_value(value);
108
59090
    state_ = kAfterValue;
109
  }
110
111
  template <typename U>
112
912
  inline void json_element(const U& value) {
113
912
    if (state_ == kAfterValue) out_ << ',';
114
912
    write_new_line();
115
912
    advance();
116
912
    write_value(value);
117
912
    state_ = kAfterValue;
118
912
  }
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
41634
  inline void write_value(T number) {
131
    if constexpr (std::is_same<T, bool>::value)
132
1520
      out_ << (number ? "true" : "false");
133
    else
134
40114
      out_ << number;
135
41634
  }
136
137
33
  inline void write_value(Null null) { out_ << "null"; }
138
6934
  inline void write_value(const char* str) { write_string(str); }
139
2215
  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
39968
  inline void write_string(const std::string& str) {
146
39968
    out_ << '"' << EscapeJsonChars(str) << '"';
147
39968
  }
148
37740
  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_