diff options
author | chriseth <chris@ethereum.org> | 2018-03-08 03:20:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-08 03:20:57 +0800 |
commit | dfe3193c7382c80f1814247a162663a97c3f5e67 (patch) | |
tree | d0540c2dfc43a76f0add46840f60ff2e28604a19 /libdevcore/JSON.cpp | |
parent | 3155dd8058672ce8f04bc2c0f2536cb549067d0a (diff) | |
parent | 15920dc75dd5a46a036d5ff16fb8eee0534be6e1 (diff) | |
download | dexon-solidity-dfe3193c7382c80f1814247a162663a97c3f5e67.tar dexon-solidity-dfe3193c7382c80f1814247a162663a97c3f5e67.tar.gz dexon-solidity-dfe3193c7382c80f1814247a162663a97c3f5e67.tar.bz2 dexon-solidity-dfe3193c7382c80f1814247a162663a97c3f5e67.tar.lz dexon-solidity-dfe3193c7382c80f1814247a162663a97c3f5e67.tar.xz dexon-solidity-dfe3193c7382c80f1814247a162663a97c3f5e67.tar.zst dexon-solidity-dfe3193c7382c80f1814247a162663a97c3f5e67.zip |
Merge pull request #3678 from ethereum/develop
Merge develop into release.
Diffstat (limited to 'libdevcore/JSON.cpp')
-rw-r--r-- | libdevcore/JSON.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/libdevcore/JSON.cpp b/libdevcore/JSON.cpp new file mode 100644 index 00000000..079d4d51 --- /dev/null +++ b/libdevcore/JSON.cpp @@ -0,0 +1,109 @@ +/* + 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/>. +*/ +/** @file JSON.cpp + * @author Alexander Arlt <alexander.arlt@arlt-labs.com> + * @date 2018 + */ + +#include "JSON.h" + +#include <sstream> +#include <map> +#include <memory> + +using namespace std; + +namespace dev +{ + +namespace +{ + +/// StreamWriterBuilder that can be constructed with specific settings +class StreamWriterBuilder: public Json::StreamWriterBuilder +{ +public: + explicit StreamWriterBuilder(map<string, string> const& _settings) + { + for (auto const& iter :_settings) + 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 +string print(Json::Value const& _input, Json::StreamWriterBuilder const& _builder) +{ + stringstream stream; + unique_ptr<Json::StreamWriter> writer(_builder.newStreamWriter()); + 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. +bool parse(Json::CharReaderBuilder& _builder, string const& _input, Json::Value& _json, string* _errs) +{ + unique_ptr<Json::CharReader> reader(_builder.newCharReader()); + return reader->parse(_input.c_str(), _input.c_str() + _input.length(), &_json, _errs); +} + +} // end anonymous namespace + +string jsonPrettyPrint(Json::Value const& _input) +{ + static map<string, string> settings{{"indentation", " "}}; + static StreamWriterBuilder writerBuilder(settings); + return print(_input, writerBuilder); +} + +string jsonCompactPrint(Json::Value const& _input) +{ + static map<string, string> settings{{"indentation", ""}}; + static StreamWriterBuilder writerBuilder(settings); + return print(_input, writerBuilder); +} + +bool jsonParseStrict(string const& _input, Json::Value& _json, string* _errs /* = nullptr */) +{ + static StrictModeCharReaderBuilder readerBuilder; + return parse(readerBuilder, _input, _json, _errs); +} + +bool jsonParse(string const& _input, Json::Value& _json, string *_errs /* = nullptr */) +{ + static Json::CharReaderBuilder readerBuilder; + return parse(readerBuilder, _input, _json, _errs); +} + +} // namespace dev |