Skip to content

Commit

Permalink
Basic nlohmann-json integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlt committed Jul 20, 2018
1 parent 81c5a6e commit 1329c25
Show file tree
Hide file tree
Showing 29 changed files with 504 additions and 582 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ include(EthCcache)

# Let's find our dependencies
include(EthDependencies)
include(jsoncpp)
include_directories(SYSTEM ${JSONCPP_INCLUDE_DIR})
include(nlohmann-json)

find_package(Threads)

Expand Down
49 changes: 0 additions & 49 deletions cmake/jsoncpp.cmake

This file was deleted.

14 changes: 14 additions & 0 deletions cmake/nlohmann-json.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include(ExternalProject)

ExternalProject_Add(nlohmann-json
DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/nlohmann/json"
DOWNLOAD_NAME json.hpp
DOWNLOAD_NO_EXTRACT 1
URL https://github.com/nlohmann/json/releases/download/v3.1.2/json.hpp
URL_HASH SHA256=fbdfec4b4cf63b3b565d09f87e6c3c183bdd45c5be1864d3fcb338f6f02c1733
CMAKE_COMMAND true
BUILD_COMMAND true
INSTALL_COMMAND true
)

include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/deps/nlohmann)
4 changes: 2 additions & 2 deletions libdevcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ file(GLOB sources "*.cpp")
file(GLOB headers "*.h")

add_library(devcore ${sources} ${headers})
target_link_libraries(devcore PRIVATE jsoncpp ${Boost_FILESYSTEM_LIBRARIES} ${Boost_REGEX_LIBRARIES} ${Boost_SYSTEM_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(devcore PRIVATE ${Boost_FILESYSTEM_LIBRARIES} ${Boost_REGEX_LIBRARIES} ${Boost_SYSTEM_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(devcore PUBLIC "${CMAKE_SOURCE_DIR}")
target_include_directories(devcore SYSTEM PUBLIC ${Boost_INCLUDE_DIRS})
add_dependencies(devcore solidity_BuildInfo.h)
add_dependencies(devcore nlohmann-json solidity_BuildInfo.h)
110 changes: 39 additions & 71 deletions libdevcore/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,56 @@

using namespace std;

static_assert(
(JSONCPP_VERSION_MAJOR == 1) && (JSONCPP_VERSION_MINOR == 8) && (JSONCPP_VERSION_PATCH == 4),
"Unexpected jsoncpp version: " JSONCPP_VERSION_STRING ". Expecting 1.8.4."
);
namespace dev {

namespace dev
string jsonPrettyPrint(Json const &_input)
{

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();
return _input.dump(4);
}

/// 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)
string jsonCompactPrint(Json const &_input)
{
unique_ptr<Json::CharReader> reader(_builder.newCharReader());
return reader->parse(_input.c_str(), _input.c_str() + _input.length(), &_json, _errs);
return _input.dump(0);
}

} // end anonymous namespace

string jsonPrettyPrint(Json::Value const& _input)
bool jsonParseStrict(string const &_input, Json &_json, string *_errs /* = nullptr */)
{
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);
try {
// parsing input with a syntax error
_json = Json::parse(_input);
return true;
}
catch (Json::parse_error &e) {
/*
std::cout << "message: " << e.what() << '\n'
<< "exception id: " << e.id << '\n'
<< "byte position of error: " << e.byte << std::endl;
*/
if (_errs) {
*_errs = e.what();
}
return false;
}
}

bool jsonParse(string const& _input, Json::Value& _json, string *_errs /* = nullptr */)
bool jsonParse(string const &_input, Json &_json, string *_errs /* = nullptr */)
{
static Json::CharReaderBuilder readerBuilder;
return parse(readerBuilder, _input, _json, _errs);
try {
// parsing input with a syntax error
_json = Json::parse(_input);
return true;
}
catch (Json::parse_error &e) {
/*
std::cout << "message: " << e.what() << '\n'
<< "exception id: " << e.id << '\n'
<< "byte position of error: " << e.byte << std::endl;
*/
if (_errs) {
*_errs = e.what();
}
return false;
}
}

} // namespace dev
12 changes: 7 additions & 5 deletions libdevcore/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,32 @@

#pragma once

#include <json/json.h>
#include <json/json.hpp>

using Json = nlohmann::json;

#include <string>

namespace dev {

/// Serialise the JSON object (@a _input) with indentation
std::string jsonPrettyPrint(Json::Value const& _input);
std::string jsonPrettyPrint(Json const& _input);

/// Serialise the JSON object (@a _input) without indentation
std::string jsonCompactPrint(Json::Value const& _input);
std::string jsonCompactPrint(Json 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);
bool jsonParseStrict(std::string const& _input, Json& _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);
bool jsonParse(std::string const& _input, Json& _json, std::string* _errs = nullptr);

}
39 changes: 19 additions & 20 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <libevmasm/GasMeter.h>

#include <fstream>
#include <json/json.h>

using namespace std;
using namespace dev;
Expand Down Expand Up @@ -217,9 +216,9 @@ string Assembly::assemblyString(StringMap const& _sourceCodes) const
return tmp.str();
}

Json::Value Assembly::createJsonValue(string _name, int _begin, int _end, string _value, string _jumpType)
Json Assembly::createJsonValue(string _name, int _begin, int _end, string _value, string _jumpType)
{
Json::Value value;
Json value;
value["name"] = _name;
value["begin"] = _begin;
value["end"] = _end;
Expand All @@ -237,65 +236,65 @@ string Assembly::toStringInHex(u256 _value)
return hexStr.str();
}

Json::Value Assembly::assemblyJSON(StringMap const& _sourceCodes) const
Json Assembly::assemblyJSON(StringMap const& _sourceCodes) const
{
Json::Value root;
Json root;

Json::Value& collection = root[".code"] = Json::arrayValue;
Json& collection = root[".code"];
for (AssemblyItem const& i: m_items)
{
switch (i.type())
{
case Operation:
collection.append(
collection.emplace_back(
createJsonValue(instructionInfo(i.instruction()).name, i.location().start, i.location().end, i.getJumpTypeAsString()));
break;
case Push:
collection.append(
collection.emplace_back(
createJsonValue("PUSH", i.location().start, i.location().end, toStringInHex(i.data()), i.getJumpTypeAsString()));
break;
case PushString:
collection.append(
collection.emplace_back(
createJsonValue("PUSH tag", i.location().start, i.location().end, m_strings.at((h256)i.data())));
break;
case PushTag:
if (i.data() == 0)
collection.append(
collection.emplace_back(
createJsonValue("PUSH [ErrorTag]", i.location().start, i.location().end, ""));
else
collection.append(
collection.emplace_back(
createJsonValue("PUSH [tag]", i.location().start, i.location().end, string(i.data())));
break;
case PushSub:
collection.append(
collection.emplace_back(
createJsonValue("PUSH [$]", i.location().start, i.location().end, dev::toString(h256(i.data()))));
break;
case PushSubSize:
collection.append(
collection.emplace_back(
createJsonValue("PUSH #[$]", i.location().start, i.location().end, dev::toString(h256(i.data()))));
break;
case PushProgramSize:
collection.append(
collection.emplace_back(
createJsonValue("PUSHSIZE", i.location().start, i.location().end));
break;
case PushLibraryAddress:
collection.append(
collection.emplace_back(
createJsonValue("PUSHLIB", i.location().start, i.location().end, m_libraries.at(h256(i.data())))
);
break;
case PushDeployTimeAddress:
collection.append(
collection.emplace_back(
createJsonValue("PUSHDEPLOYADDRESS", i.location().start, i.location().end)
);
break;
case Tag:
collection.append(
collection.emplace_back(
createJsonValue("tag", i.location().start, i.location().end, string(i.data())));
collection.append(
collection.emplace_back(
createJsonValue("JUMPDEST", i.location().start, i.location().end));
break;
case PushData:
collection.append(createJsonValue("PUSH data", i.location().start, i.location().end, toStringInHex(i.data())));
collection.emplace_back(createJsonValue("PUSH data", i.location().start, i.location().end, toStringInHex(i.data())));
break;
default:
BOOST_THROW_EXCEPTION(InvalidOpcode());
Expand All @@ -304,7 +303,7 @@ Json::Value Assembly::assemblyJSON(StringMap const& _sourceCodes) const

if (!m_data.empty() || !m_subs.empty())
{
Json::Value& data = root[".data"] = Json::objectValue;
Json& data = root[".data"];
for (auto const& i: m_data)
if (u256(i.first) >= m_subs.size())
data[toStringInHex((u256)i.first)] = toHex(i.second);
Expand Down
7 changes: 3 additions & 4 deletions libevmasm/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
#include <libdevcore/Common.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/SHA3.h>

#include <json/json.h>
#include <libdevcore/JSON.h>

#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -136,7 +135,7 @@ class Assembly
) const;

/// Create a JSON representation of the assembly.
Json::Value assemblyJSON(
Json assemblyJSON(
StringMap const& _sourceCodes = StringMap()
) const;

Expand All @@ -149,7 +148,7 @@ class Assembly
unsigned bytesRequired(unsigned subTagSize) const;

private:
static Json::Value createJsonValue(std::string _name, int _begin, int _end, std::string _value = std::string(), std::string _jumpType = std::string());
static Json createJsonValue(std::string _name, int _begin, int _end, std::string _value = std::string(), std::string _jumpType = std::string());
static std::string toStringInHex(u256 _value);

protected:
Expand Down
Loading

0 comments on commit 1329c25

Please sign in to comment.