aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore
diff options
context:
space:
mode:
authorAlexander Arlt <alexander.arlt@arlt-labs.com>2018-02-07 09:05:20 +0800
committerAlex Beregszaszi <alex@rtfs.hu>2018-02-21 04:58:26 +0800
commit0f29ac4e563f60be781b31ed9ef2693e1a19dcc8 (patch)
tree888d535ad5698b41f6979622040c3dcce3d300ca /libdevcore
parentdcc4083b231e3574a64b5b2a329a7401677610da (diff)
downloaddexon-solidity-0f29ac4e563f60be781b31ed9ef2693e1a19dcc8.tar
dexon-solidity-0f29ac4e563f60be781b31ed9ef2693e1a19dcc8.tar.gz
dexon-solidity-0f29ac4e563f60be781b31ed9ef2693e1a19dcc8.tar.bz2
dexon-solidity-0f29ac4e563f60be781b31ed9ef2693e1a19dcc8.tar.lz
dexon-solidity-0f29ac4e563f60be781b31ed9ef2693e1a19dcc8.tar.xz
dexon-solidity-0f29ac4e563f60be781b31ed9ef2693e1a19dcc8.tar.zst
dexon-solidity-0f29ac4e563f60be781b31ed9ef2693e1a19dcc8.zip
Add new JSON API for better abstraction and for supporting strict JSON parsing
Diffstat (limited to 'libdevcore')
-rw-r--r--libdevcore/CMakeLists.txt4
-rw-r--r--libdevcore/JSON.cpp109
-rw-r--r--libdevcore/JSON.h31
3 files changed, 131 insertions, 13 deletions
diff --git a/libdevcore/CMakeLists.txt b/libdevcore/CMakeLists.txt
index a1c4c2d3..d107f701 100644
--- a/libdevcore/CMakeLists.txt
+++ b/libdevcore/CMakeLists.txt
@@ -2,7 +2,9 @@ file(GLOB sources "*.cpp")
file(GLOB headers "*.h")
add_library(devcore ${sources} ${headers})
-target_link_libraries(devcore PRIVATE ${Boost_FILESYSTEM_LIBRARIES} ${Boost_REGEX_LIBRARIES} ${Boost_SYSTEM_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
+target_link_libraries(devcore PRIVATE ${JSONCPP_LIBRARY} ${Boost_FILESYSTEM_LIBRARIES} ${Boost_REGEX_LIBRARIES} ${Boost_SYSTEM_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(devcore SYSTEM PUBLIC ${Boost_INCLUDE_DIRS})
target_include_directories(devcore PUBLIC "${CMAKE_SOURCE_DIR}")
+target_include_directories(devcore PUBLIC "${JSONCPP_INCLUDE_DIR}")
+add_dependencies(devcore jsoncpp)
add_dependencies(devcore solidity_BuildInfo.h)
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
diff --git a/libdevcore/JSON.h b/libdevcore/JSON.h
index 8499d623..1ce822cd 100644
--- a/libdevcore/JSON.h
+++ b/libdevcore/JSON.h
@@ -24,21 +24,28 @@
#include <json/json.h>
-namespace dev
-{
+#include <string>
+
+namespace dev {
/// Serialise the JSON object (@a _input) with indentation
-inline std::string jsonPrettyPrint(Json::Value const& _input)
-{
- return Json::StyledWriter().write(_input);
-}
+std::string jsonPrettyPrint(Json::Value const& _input);
/// Serialise the JSON object (@a _input) without indentation
-inline std::string jsonCompactPrint(Json::Value const& _input)
-{
- Json::FastWriter writer;
- writer.omitEndingLineFeed();
- return writer.write(_input);
-}
+std::string jsonCompactPrint(Json::Value const& _input);
+
+/// Parse a JSON string (@a _input) with enabled strict-mode and writes resulting JSON object to (@a _json)
+/// \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 jsonParseStrict(std::string const& _input, Json::Value& _json, std::string* _errs = nullptr);
+
+/// Parse a JSON string (@a _input) and writes resulting JSON object to (@a _json)
+/// \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 jsonParse(std::string const& _input, Json::Value& _json, std::string* _errs = nullptr);
}