1 /*
2 This file is part of solidity.
3
4 solidity is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 solidity is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with solidity. If not, see <http://www.gnu.org/licenses/>.
16 */
17 // SPDX-License-Identifier: GPL-3.0
18 /** @file JSON.cpp
19 * @author Alexander Arlt <alexander.arlt@arlt-labs.com>
20 * @date 2018
21 */
22
23 #include <libsolutil/JSON.h>
24
25 #include <libsolutil/CommonIO.h>
26
27 #include <boost/algorithm/string/replace.hpp>
28
29 #include <sstream>
30 #include <map>
31 #include <memory>
32
33 using namespace std;
34
35 /*
36 static_assert(
37 (JSONCPP_VERSION_MAJOR == 1) && (JSONCPP_VERSION_MINOR == 9) && (JSONCPP_VERSION_PATCH == 3),
38 "Unexpected jsoncpp version: " JSONCPP_VERSION_STRING ". Expecting 1.9.3."
39 );
40 */
41
42 namespace solidity::util
43 {
44
45 namespace
46 {
47
48 /// StreamWriterBuilder that can be constructed with specific settings
49 class StreamWriterBuilder: public Json::StreamWriterBuilder
50 {
51 public:
StreamWriterBuilder(map<string,Json::Value> const & _settings)52 explicit StreamWriterBuilder(map<string, Json::Value> const& _settings)
53 {
54 for (auto const& iter: _settings)
55 this->settings_[iter.first] = iter.second;
56 }
57 };
58
59 /// CharReaderBuilder with strict-mode settings
60 class StrictModeCharReaderBuilder: public Json::CharReaderBuilder
61 {
62 public:
StrictModeCharReaderBuilder()63 StrictModeCharReaderBuilder()
64 {
65 Json::CharReaderBuilder::strictMode(&this->settings_);
66 }
67 };
68
69 /// Serialise the JSON object (@a _input) with specific builder (@a _builder)
70 /// \param _input JSON input string
71 /// \param _builder StreamWriterBuilder that is used to create new Json::StreamWriter
72 /// \return serialized json object
print(Json::Value const & _input,Json::StreamWriterBuilder const & _builder)73 string print(Json::Value const& _input, Json::StreamWriterBuilder const& _builder)
74 {
75 stringstream stream;
76 unique_ptr<Json::StreamWriter> writer(_builder.newStreamWriter());
77 writer->write(_input, &stream);
78 return stream.str();
79 }
80
81 /// Parse a JSON string (@a _input) with specified builder (@ _builder) and writes resulting JSON object to (@a _json)
82 /// \param _builder CharReaderBuilder that is used to create new Json::CharReaders
83 /// \param _input JSON input string
84 /// \param _json [out] resulting JSON object
85 /// \param _errs [out] Formatted error messages
86 /// \return \c true if the document was successfully parsed, \c false if an error occurred.
parse(Json::CharReaderBuilder & _builder,string const & _input,Json::Value & _json,string * _errs)87 bool parse(Json::CharReaderBuilder& _builder, string const& _input, Json::Value& _json, string* _errs)
88 {
89 unique_ptr<Json::CharReader> reader(_builder.newCharReader());
90 return reader->parse(_input.c_str(), _input.c_str() + _input.length(), &_json, _errs);
91 }
92
93 /// Takes a JSON value (@ _json) and removes all its members with value 'null' recursively.
removeNullMembersHelper(Json::Value & _json)94 void removeNullMembersHelper(Json::Value& _json)
95 {
96 if (_json.type() == Json::ValueType::arrayValue)
97 for (auto& child: _json)
98 removeNullMembersHelper(child);
99 else if (_json.type() == Json::ValueType::objectValue)
100 for (auto const& key: _json.getMemberNames())
101 {
102 Json::Value& value = _json[key];
103 if (value.isNull())
104 _json.removeMember(key);
105 else
106 removeNullMembersHelper(value);
107 }
108 }
109
110 } // end anonymous namespace
111
removeNullMembers(Json::Value _json)112 Json::Value removeNullMembers(Json::Value _json)
113 {
114 removeNullMembersHelper(_json);
115 return _json;
116 }
117
jsonPrettyPrint(Json::Value const & _input)118 string jsonPrettyPrint(Json::Value const& _input)
119 {
120 return jsonPrint(_input, JsonFormat{ JsonFormat::Pretty });
121 }
122
jsonCompactPrint(Json::Value const & _input)123 string jsonCompactPrint(Json::Value const& _input)
124 {
125 return jsonPrint(_input, JsonFormat{ JsonFormat::Compact });
126 }
127
jsonPrint(Json::Value const & _input,JsonFormat const & _format)128 string jsonPrint(Json::Value const& _input, JsonFormat const& _format)
129 {
130 map<string, Json::Value> settings;
131 if (_format.format == JsonFormat::Pretty)
132 {
133 settings["indentation"] = string(_format.indent, ' ');
134 settings["enableYAMLCompatibility"] = true;
135 }
136 else
137 settings["indentation"] = "";
138 StreamWriterBuilder writerBuilder(settings);
139 string result = print(_input, writerBuilder);
140 if (_format.format == JsonFormat::Pretty)
141 boost::replace_all(result, " \n", "\n");
142 return result;
143 }
144
jsonParseStrict(string const & _input,Json::Value & _json,string * _errs)145 bool jsonParseStrict(string const& _input, Json::Value& _json, string* _errs /* = nullptr */)
146 {
147 static StrictModeCharReaderBuilder readerBuilder;
148 return parse(readerBuilder, _input, _json, _errs);
149 }
150
151 } // namespace solidity::util
152