Skip to content

Commit c436c71

Browse files
committed
writer-json-common: adopt jsonPrettyPrint()
... from the writer-json module Related: #115
1 parent 184c906 commit c436c71

File tree

3 files changed

+105
-102
lines changed

3 files changed

+105
-102
lines changed

src/lib/writer-json-common.cc

+97
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,100 @@ object jsonSerializeScanProps(const TScanProps &scanProps)
5050

5151
return scan;
5252
}
53+
54+
static inline void prettyPrintArray(
55+
std::ostream &os,
56+
const array &arr,
57+
std::string *indent = nullptr)
58+
{
59+
os << '[';
60+
if (arr.empty()) {
61+
os << ']';
62+
return;
63+
}
64+
65+
indent->append(4, ' ');
66+
67+
std::string sep{'\n'};
68+
for (const auto &elem : arr) {
69+
os << sep << *indent;
70+
jsonPrettyPrint(os, elem, indent);
71+
sep = ",\n";
72+
}
73+
os << '\n';
74+
75+
indent->resize(indent->size() - 4);
76+
os << *indent << ']';
77+
}
78+
79+
static inline void prettyPrintObject(
80+
std::ostream &os,
81+
const object &obj,
82+
std::string *indent = nullptr)
83+
{
84+
os << '{';
85+
if (obj.empty()) {
86+
os << '}';
87+
return;
88+
}
89+
90+
indent->append(4, ' ');
91+
92+
std::string sep{'\n'};
93+
for (const auto &elem : obj) {
94+
os << sep << *indent << serialize(elem.key()) << ": ";
95+
jsonPrettyPrint(os, elem.value(), indent);
96+
sep = ",\n";
97+
}
98+
os << '\n';
99+
100+
indent->resize(indent->size() - 4);
101+
os << *indent << '}';
102+
}
103+
104+
void jsonPrettyPrint(
105+
std::ostream &os,
106+
const value &jv,
107+
std::string *indent)
108+
{
109+
std::string indent_;
110+
if (!indent)
111+
indent = &indent_;
112+
113+
switch (jv.kind()) {
114+
case kind::array:
115+
prettyPrintArray(os, jv.get_array(), indent);
116+
break;
117+
118+
case kind::object:
119+
prettyPrintObject(os, jv.get_object(), indent);
120+
break;
121+
122+
case kind::string:
123+
os << serialize(jv.get_string());
124+
break;
125+
126+
case kind::uint64:
127+
os << jv.get_uint64();
128+
break;
129+
130+
case kind::int64:
131+
os << jv.get_int64();
132+
break;
133+
134+
case kind::double_:
135+
os << jv.get_double();
136+
break;
137+
138+
case kind::bool_:
139+
os << jv.get_bool();
140+
break;
141+
142+
case kind::null:
143+
os << "null";
144+
break;
145+
}
146+
147+
if (indent->empty())
148+
os << "\n";
149+
}

src/lib/writer-json-common.hh

+6
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ std::string sanitizeUTF8(const std::string &str);
3232
/// serialize scan properties as a JSON object
3333
boost::json::object jsonSerializeScanProps(const TScanProps &scanProps);
3434

35+
/// serialize JSON value into the give output stream
36+
void jsonPrettyPrint(
37+
std::ostream &os,
38+
const boost::json::value &jv,
39+
std::string *indent = nullptr);
40+
3541
#endif /* H_GUARD_WRITER_JSON_COMMON_H */

src/lib/writer-json.cc

+2-102
Original file line numberDiff line numberDiff line change
@@ -31,106 +31,6 @@
3131

3232
using namespace boost::json;
3333

34-
static void prettyPrint(std::ostream&, const value&, std::string* = nullptr);
35-
36-
static inline void prettyPrintArray(
37-
std::ostream &os,
38-
const array &arr,
39-
std::string *indent = nullptr)
40-
{
41-
os << '[';
42-
if (arr.empty()) {
43-
os << ']';
44-
return;
45-
}
46-
47-
indent->append(4, ' ');
48-
49-
std::string sep{'\n'};
50-
for (const auto &elem : arr) {
51-
os << sep << *indent;
52-
prettyPrint(os, elem, indent);
53-
sep = ",\n";
54-
}
55-
os << '\n';
56-
57-
indent->resize(indent->size() - 4);
58-
os << *indent << ']';
59-
60-
}
61-
62-
static inline void prettyPrintObject(
63-
std::ostream &os,
64-
const object &obj,
65-
std::string *indent = nullptr)
66-
{
67-
os << '{';
68-
if (obj.empty()) {
69-
os << '}';
70-
return;
71-
}
72-
73-
indent->append(4, ' ');
74-
75-
std::string sep{'\n'};
76-
for (const auto &elem : obj) {
77-
os << sep << *indent << serialize(elem.key()) << ": ";
78-
prettyPrint(os, elem.value(), indent);
79-
sep = ",\n";
80-
}
81-
os << '\n';
82-
83-
indent->resize(indent->size() - 4);
84-
os << *indent << '}';
85-
}
86-
87-
static void prettyPrint(
88-
std::ostream &os,
89-
const value &jv,
90-
std::string *indent)
91-
{
92-
std::string indent_;
93-
if (!indent)
94-
indent = &indent_;
95-
96-
switch (jv.kind()) {
97-
case kind::array:
98-
prettyPrintArray(os, jv.get_array(), indent);
99-
break;
100-
101-
case kind::object:
102-
prettyPrintObject(os, jv.get_object(), indent);
103-
break;
104-
105-
case kind::string:
106-
os << serialize(jv.get_string());
107-
break;
108-
109-
case kind::uint64:
110-
os << jv.get_uint64();
111-
break;
112-
113-
case kind::int64:
114-
os << jv.get_int64();
115-
break;
116-
117-
case kind::double_:
118-
os << jv.get_double();
119-
break;
120-
121-
case kind::bool_:
122-
os << jv.get_bool();
123-
break;
124-
125-
case kind::null:
126-
os << "null";
127-
break;
128-
}
129-
130-
if (indent->empty())
131-
os << "\n";
132-
}
133-
13434
class SimpleTreeEncoder: public AbstractTreeEncoder {
13535
public:
13636
/// import supported scan properties
@@ -214,7 +114,7 @@ void SimpleTreeEncoder::writeTo(std::ostream &str)
214114
// create an empty "defects" node to keep format detection working
215115
pDefects_ = &root_["defects"].emplace_array();
216116

217-
prettyPrint(str, root_);
117+
jsonPrettyPrint(str, root_);
218118
}
219119

220120
// SARIF 2.1.0 is documented at:
@@ -577,7 +477,7 @@ void SarifTreeEncoder::writeTo(std::ostream &str)
577477
root["runs"] = array{std::move(run0)};
578478

579479
// encode as JSON
580-
prettyPrint(str, root);
480+
jsonPrettyPrint(str, root);
581481
}
582482

583483
struct JsonWriter::Private {

0 commit comments

Comments
 (0)