Skip to content

Commit 184c906

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

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/lib/writer-json-common.cc

+21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "writer-json-common.hh"
2121

2222
#include <boost/nowide/utf/convert.hpp>
23+
#include <boost/lexical_cast.hpp>
24+
25+
using namespace boost::json;
2326

2427
std::string sanitizeUTF8(const std::string &str)
2528
{
@@ -29,3 +32,21 @@ std::string sanitizeUTF8(const std::string &str)
2932
// corresponds to REPLACEMENT CHARACTER U+FFFD
3033
return convert_string<char>(str.data(), str.data() + str.size());
3134
}
35+
36+
// TODO: This should not necessary! TScanProps should be able to contain
37+
// any type so that no conversions here are needed.
38+
object jsonSerializeScanProps(const TScanProps &scanProps)
39+
{
40+
static auto isDigit = [](unsigned char c){ return std::isdigit(c); };
41+
42+
object scan;
43+
for (const auto &prop : scanProps) {
44+
const auto &val = prop.second;
45+
if (std::all_of(val.begin(), val.end(), isDigit))
46+
scan[prop.first] = boost::lexical_cast<int>(val);
47+
else
48+
scan[prop.first] = val;
49+
}
50+
51+
return scan;
52+
}

src/lib/writer-json-common.hh

+7
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@
2020
#ifndef H_GUARD_WRITER_JSON_COMMON_H
2121
#define H_GUARD_WRITER_JSON_COMMON_H
2222

23+
#include "parser.hh" // for TScanProps
24+
2325
#include <string>
2426

27+
#include <boost/json.hpp>
28+
2529
/// sanitize byte sequences that are not valid in UTF-8 encoding
2630
std::string sanitizeUTF8(const std::string &str);
2731

32+
/// serialize scan properties as a JSON object
33+
boost::json::object jsonSerializeScanProps(const TScanProps &scanProps);
34+
2835
#endif /* H_GUARD_WRITER_JSON_COMMON_H */

src/lib/writer-json.cc

+2-20
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <queue>
2929

3030
#include <boost/json/src.hpp>
31-
#include <boost/lexical_cast.hpp>
3231

3332
using namespace boost::json;
3433

@@ -132,23 +131,6 @@ static void prettyPrint(
132131
os << "\n";
133132
}
134133

135-
// TODO: This should not necessary! TScanProps should be able to contain
136-
// any type so that no conversions here are needed.
137-
static object serializeScanProps(const TScanProps &scanProps) {
138-
static auto isDigit = [](unsigned char c){ return std::isdigit(c); };
139-
140-
object scan;
141-
for (const auto &prop : scanProps) {
142-
const auto &val = prop.second;
143-
if (std::all_of(val.begin(), val.end(), isDigit))
144-
scan[prop.first] = boost::lexical_cast<int>(val);
145-
else
146-
scan[prop.first] = val;
147-
}
148-
149-
return scan;
150-
}
151-
152134
class SimpleTreeEncoder: public AbstractTreeEncoder {
153135
public:
154136
/// import supported scan properties
@@ -170,7 +152,7 @@ void SimpleTreeEncoder::importScanProps(const TScanProps &scanProps)
170152
if (scanProps.empty())
171153
return;
172154

173-
root_["scan"] = serializeScanProps(scanProps);
155+
root_["scan"] = jsonSerializeScanProps(scanProps);
174156
}
175157

176158
void SimpleTreeEncoder::appendDef(const Defect &def)
@@ -572,7 +554,7 @@ void SarifTreeEncoder::writeTo(std::ostream &str)
572554
if (!scanProps_.empty()) {
573555
// scan props
574556
root["inlineExternalProperties"] = {
575-
{{ "externalizedProperties", serializeScanProps(scanProps_) }}
557+
{{ "externalizedProperties", jsonSerializeScanProps(scanProps_) }}
576558
};
577559
}
578560

0 commit comments

Comments
 (0)