Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ add_library(cs STATIC
writer-cov.cc
writer-html.cc
writer-json.cc
writer-json-common.cc
writer-json-sarif.cc
writer-json-simple.cc
)
149 changes: 149 additions & 0 deletions src/lib/writer-json-common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (C) 2011 - 2023 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/

#include "writer-json-common.hh"

#include <boost/nowide/utf/convert.hpp>
#include <boost/lexical_cast.hpp>

using namespace boost::json;

std::string sanitizeUTF8(const std::string &str)
{
using boost::nowide::utf::convert_string;

// every non-UTF8 sequence will be replaced with 0xEF 0xBF 0xBD which
// corresponds to REPLACEMENT CHARACTER U+FFFD
return convert_string<char>(str.data(), str.data() + str.size());
}

// TODO: This should not necessary! TScanProps should be able to contain
// any type so that no conversions here are needed.
object jsonSerializeScanProps(const TScanProps &scanProps)
{
static auto isDigit = [](unsigned char c){ return std::isdigit(c); };

object scan;
for (const auto &prop : scanProps) {
const auto &val = prop.second;
if (std::all_of(val.begin(), val.end(), isDigit))
scan[prop.first] = boost::lexical_cast<int>(val);
else
scan[prop.first] = val;
}

return scan;
}

static inline void prettyPrintArray(
std::ostream &os,
const array &arr,
std::string *indent = nullptr)
{
os << '[';
if (arr.empty()) {
os << ']';
return;
}

indent->append(4, ' ');

std::string sep{'\n'};
for (const auto &elem : arr) {
os << sep << *indent;
jsonPrettyPrint(os, elem, indent);
sep = ",\n";
}
os << '\n';

indent->resize(indent->size() - 4);
os << *indent << ']';
}

static inline void prettyPrintObject(
std::ostream &os,
const object &obj,
std::string *indent = nullptr)
{
os << '{';
if (obj.empty()) {
os << '}';
return;
}

indent->append(4, ' ');

std::string sep{'\n'};
for (const auto &elem : obj) {
os << sep << *indent << serialize(elem.key()) << ": ";
jsonPrettyPrint(os, elem.value(), indent);
sep = ",\n";
}
os << '\n';

indent->resize(indent->size() - 4);
os << *indent << '}';
}

void jsonPrettyPrint(
std::ostream &os,
const value &jv,
std::string *indent)
{
std::string indent_;
if (!indent)
indent = &indent_;

switch (jv.kind()) {
case kind::array:
prettyPrintArray(os, jv.get_array(), indent);
break;

case kind::object:
prettyPrintObject(os, jv.get_object(), indent);
break;

case kind::string:
os << serialize(jv.get_string());
break;

case kind::uint64:
os << jv.get_uint64();
break;

case kind::int64:
os << jv.get_int64();
break;

case kind::double_:
os << jv.get_double();
break;

case kind::bool_:
os << jv.get_bool();
break;

case kind::null:
os << "null";
break;
}

if (indent->empty())
os << "\n";
}
41 changes: 41 additions & 0 deletions src/lib/writer-json-common.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2011 - 2023 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef H_GUARD_WRITER_JSON_COMMON_H
#define H_GUARD_WRITER_JSON_COMMON_H

#include "parser.hh" // for TScanProps

#include <string>

#include <boost/json.hpp>

/// sanitize byte sequences that are not valid in UTF-8 encoding
std::string sanitizeUTF8(const std::string &str);

/// serialize scan properties as a JSON object
boost::json::object jsonSerializeScanProps(const TScanProps &scanProps);

/// serialize JSON value into the give output stream
void jsonPrettyPrint(
std::ostream &os,
const boost::json::value &jv,
std::string *indent = nullptr);

#endif /* H_GUARD_WRITER_JSON_COMMON_H */
Loading