2018-02-07 01:05:20 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-02-07 01:05:20 +00:00
|
|
|
/** @file JSON.cpp
|
|
|
|
* @author Alexander Arlt <alexander.arlt@arlt-labs.com>
|
|
|
|
* @date 2018
|
|
|
|
*/
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/JSON.h>
|
2018-02-07 01:05:20 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonIO.h>
|
2018-11-15 15:47:52 +00:00
|
|
|
|
2019-07-02 16:06:16 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
|
2018-02-07 01:05:20 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
|
2018-04-17 10:00:02 +00:00
|
|
|
static_assert(
|
2020-07-10 17:42:24 +00:00
|
|
|
(JSONCPP_VERSION_MAJOR == 1) && (JSONCPP_VERSION_MINOR == 9) && (JSONCPP_VERSION_PATCH == 3),
|
|
|
|
"Unexpected jsoncpp version: " JSONCPP_VERSION_STRING ". Expecting 1.9.3."
|
2018-04-17 10:00:02 +00:00
|
|
|
);
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::util
|
2018-02-07 01:05:20 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// StreamWriterBuilder that can be constructed with specific settings
|
|
|
|
class StreamWriterBuilder: public Json::StreamWriterBuilder
|
|
|
|
{
|
|
|
|
public:
|
2023-08-17 13:53:20 +00:00
|
|
|
explicit StreamWriterBuilder(std::map<std::string, Json::Value> const& _settings)
|
2018-02-07 01:05:20 +00:00
|
|
|
{
|
2019-07-02 16:06:16 +00:00
|
|
|
for (auto const& iter: _settings)
|
2018-02-07 01:05:20 +00:00
|
|
|
this->settings_[iter.first] = iter.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// CharReaderBuilder with strict-mode settings
|
|
|
|
class StrictModeCharReaderBuilder: public Json::CharReaderBuilder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StrictModeCharReaderBuilder()
|
|
|
|
{
|
|
|
|
Json::CharReaderBuilder::strictMode(&this->settings_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Serialise the JSON object (@a _input) with specific builder (@a _builder)
|
|
|
|
/// \param _input JSON input string
|
|
|
|
/// \param _builder StreamWriterBuilder that is used to create new Json::StreamWriter
|
|
|
|
/// \return serialized json object
|
2023-08-17 13:53:20 +00:00
|
|
|
std::string print(Json::Value const& _input, Json::StreamWriterBuilder const& _builder)
|
2018-02-07 01:05:20 +00:00
|
|
|
{
|
2023-08-17 13:53:20 +00:00
|
|
|
std::stringstream stream;
|
|
|
|
std::unique_ptr<Json::StreamWriter> writer(_builder.newStreamWriter());
|
2018-02-07 01:05:20 +00:00
|
|
|
writer->write(_input, &stream);
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a JSON string (@a _input) with specified builder (@ _builder) and writes resulting JSON object to (@a _json)
|
|
|
|
/// \param _builder CharReaderBuilder that is used to create new Json::CharReaders
|
|
|
|
/// \param _input JSON input string
|
|
|
|
/// \param _json [out] resulting JSON object
|
|
|
|
/// \param _errs [out] Formatted error messages
|
|
|
|
/// \return \c true if the document was successfully parsed, \c false if an error occurred.
|
2023-08-17 13:53:20 +00:00
|
|
|
bool parse(Json::CharReaderBuilder& _builder, std::string const& _input, Json::Value& _json, std::string* _errs)
|
2018-02-07 01:05:20 +00:00
|
|
|
{
|
2023-08-17 13:53:20 +00:00
|
|
|
std::unique_ptr<Json::CharReader> reader(_builder.newCharReader());
|
2018-02-07 01:05:20 +00:00
|
|
|
return reader->parse(_input.c_str(), _input.c_str() + _input.length(), &_json, _errs);
|
|
|
|
}
|
|
|
|
|
2020-04-27 08:28:54 +00:00
|
|
|
/// Takes a JSON value (@ _json) and removes all its members with value 'null' recursively.
|
|
|
|
void removeNullMembersHelper(Json::Value& _json)
|
|
|
|
{
|
|
|
|
if (_json.type() == Json::ValueType::arrayValue)
|
|
|
|
for (auto& child: _json)
|
|
|
|
removeNullMembersHelper(child);
|
|
|
|
else if (_json.type() == Json::ValueType::objectValue)
|
|
|
|
for (auto const& key: _json.getMemberNames())
|
|
|
|
{
|
|
|
|
Json::Value& value = _json[key];
|
|
|
|
if (value.isNull())
|
|
|
|
_json.removeMember(key);
|
|
|
|
else
|
|
|
|
removeNullMembersHelper(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 01:05:20 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2020-04-27 08:28:54 +00:00
|
|
|
Json::Value removeNullMembers(Json::Value _json)
|
|
|
|
{
|
|
|
|
removeNullMembersHelper(_json);
|
|
|
|
return _json;
|
|
|
|
}
|
|
|
|
|
2023-08-17 13:53:20 +00:00
|
|
|
std::string jsonPrettyPrint(Json::Value const& _input)
|
2018-02-07 01:05:20 +00:00
|
|
|
{
|
2021-07-07 11:18:24 +00:00
|
|
|
return jsonPrint(_input, JsonFormat{ JsonFormat::Pretty });
|
2018-02-07 01:05:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 13:53:20 +00:00
|
|
|
std::string jsonCompactPrint(Json::Value const& _input)
|
2018-02-07 01:05:20 +00:00
|
|
|
{
|
2021-07-07 11:18:24 +00:00
|
|
|
return jsonPrint(_input, JsonFormat{ JsonFormat::Compact });
|
|
|
|
}
|
|
|
|
|
2023-08-17 13:53:20 +00:00
|
|
|
std::string jsonPrint(Json::Value const& _input, JsonFormat const& _format)
|
2021-07-07 11:18:24 +00:00
|
|
|
{
|
2023-08-17 13:53:20 +00:00
|
|
|
std::map<std::string, Json::Value> settings;
|
2021-07-07 11:18:24 +00:00
|
|
|
if (_format.format == JsonFormat::Pretty)
|
|
|
|
{
|
2023-08-17 13:53:20 +00:00
|
|
|
settings["indentation"] = std::string(_format.indent, ' ');
|
2021-07-07 11:18:24 +00:00
|
|
|
settings["enableYAMLCompatibility"] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
settings["indentation"] = "";
|
|
|
|
StreamWriterBuilder writerBuilder(settings);
|
2023-08-17 13:53:20 +00:00
|
|
|
std::string result = print(_input, writerBuilder);
|
2021-07-07 11:18:24 +00:00
|
|
|
if (_format.format == JsonFormat::Pretty)
|
|
|
|
boost::replace_all(result, " \n", "\n");
|
|
|
|
return result;
|
2018-02-07 01:05:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 13:53:20 +00:00
|
|
|
bool jsonParseStrict(std::string const& _input, Json::Value& _json, std::string* _errs /* = nullptr */)
|
2018-02-07 01:05:20 +00:00
|
|
|
{
|
|
|
|
static StrictModeCharReaderBuilder readerBuilder;
|
|
|
|
return parse(readerBuilder, _input, _json, _errs);
|
|
|
|
}
|
|
|
|
|
2023-05-29 08:58:48 +00:00
|
|
|
std::optional<Json::Value> jsonValueByPath(Json::Value const& _node, std::string_view _jsonPath)
|
|
|
|
{
|
|
|
|
if (!_node.isObject() || _jsonPath.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string memberName = std::string(_jsonPath.substr(0, _jsonPath.find_first_of('.')));
|
|
|
|
if (!_node.isMember(memberName))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (memberName == _jsonPath)
|
|
|
|
return _node[memberName];
|
|
|
|
|
|
|
|
return jsonValueByPath(_node[memberName], _jsonPath.substr(memberName.size() + 1));
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
} // namespace solidity::util
|