From 81c50143f2bff6f589ab1237d68c8820107f18b9 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 15 Nov 2016 17:33:28 +0000 Subject: Move JSON helpers to libdevcore/json --- libdevcore/JSON.h | 44 ++++++++++++++++++++++++++++++++ solc/CommandLineInterface.cpp | 25 +++++------------- solc/jsonCompiler.cpp | 14 +++------- test/libsolidity/SolidityNatspecJSON.cpp | 3 ++- 4 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 libdevcore/JSON.h diff --git a/libdevcore/JSON.h b/libdevcore/JSON.h new file mode 100644 index 00000000..7876dfb2 --- /dev/null +++ b/libdevcore/JSON.h @@ -0,0 +1,44 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum 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. + + cpp-ethereum 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 cpp-ethereum. If not, see . +*/ +/** @file JSON.h + * @date 2016 + * + * JSON related helpers + */ + +#pragma once + +#include + +namespace dev +{ + +/// Serialise the JSON object (@a _input) with identation +std::string jsonPrettyPrint(Json::Value const& _input) +{ + return Json::StyledWriter().write(_input); +} + +/// Serialise theJ SON object (@a _input) without identation +std::string jsonCompactPrint(Json::Value const& _input) +{ + Json::FastWriter writer; + writer.omitEndingLineFeed(); + return writer.write(_input); +} + +} diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 5509a414..7b23f886 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -107,18 +108,6 @@ static void version() exit(0); } -string jsonPrettyPrint(Json::Value const& input) -{ - return Json::StyledWriter().write(input); -} - -string jsonCompactPrint(Json::Value const& input) -{ - Json::FastWriter writer; - writer.omitEndingLineFeed(); - return writer.write(input); -} - static bool needsHumanTargetedStdout(po::variables_map const& _args) { if (_args.count(g_argGas)) @@ -244,9 +233,9 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co { std::string output; if (_type == DocumentationType::ABIInterface) - output = jsonCompactPrint(m_compiler->metadata(_contract, _type)); + output = dev::jsonCompactPrint(m_compiler->metadata(_contract, _type)); else - output = jsonPrettyPrint(m_compiler->metadata(_contract, _type)); + output = dev::jsonPrettyPrint(m_compiler->metadata(_contract, _type)); if (m_args.count("output-dir")) createFile(_contract + suffix, output); @@ -669,7 +658,7 @@ void CommandLineInterface::handleCombinedJSON() { Json::Value contractData(Json::objectValue); if (requests.count("abi")) - contractData["abi"] = jsonCompactPrint(m_compiler->interface(contractName)); + contractData["abi"] = dev::jsonCompactPrint(m_compiler->interface(contractName)); if (requests.count("bin")) contractData["bin"] = m_compiler->object(contractName).toHex(); if (requests.count("bin-runtime")) @@ -694,9 +683,9 @@ void CommandLineInterface::handleCombinedJSON() contractData["srcmap-runtime"] = map ? *map : ""; } if (requests.count("devdoc")) - contractData["devdoc"] = jsonCompactPrint(m_compiler->metadata(contractName, DocumentationType::NatspecDev)); + contractData["devdoc"] = dev::jsonCompactPrint(m_compiler->metadata(contractName, DocumentationType::NatspecDev)); if (requests.count("userdoc")) - contractData["userdoc"] = jsonCompactPrint(m_compiler->metadata(contractName, DocumentationType::NatspecUser)); + contractData["userdoc"] = dev::jsonCompactPrint(m_compiler->metadata(contractName, DocumentationType::NatspecUser)); output["contracts"][contractName] = contractData; } @@ -720,7 +709,7 @@ void CommandLineInterface::handleCombinedJSON() output["sources"][sourceCode.first]["AST"] = converter.json(); } } - cout << jsonCompactPrint(output) << endl; + cout << dev::jsonCompactPrint(output) << endl; } void CommandLineInterface::handleAst(string const& _argStr) diff --git a/solc/jsonCompiler.cpp b/solc/jsonCompiler.cpp index 52f796a5..771f0df8 100644 --- a/solc/jsonCompiler.cpp +++ b/solc/jsonCompiler.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -125,13 +126,6 @@ Json::Value estimateGas(CompilerStack const& _compiler, string const& _contract) return gasEstimates; } -string jsonCompactPrint(Json::Value const& input) -{ - Json::FastWriter writer; - writer.omitEndingLineFeed(); - return writer.write(input); -} - string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback _readCallback) { Json::Value output(Json::objectValue); @@ -220,7 +214,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback for (string const& contractName: compiler.contractNames()) { Json::Value contractData(Json::objectValue); - contractData["interface"] = jsonCompactPrint(compiler.interface(contractName)); + contractData["interface"] = dev::jsonCompactPrint(compiler.interface(contractName)); contractData["bytecode"] = compiler.object(contractName).toHex(); contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex(); contractData["opcodes"] = solidity::disassemble(compiler.object(contractName).bytecode); @@ -281,7 +275,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback try { - return jsonCompactPrint(output); + return dev::jsonCompactPrint(output); } catch (...) { @@ -299,7 +293,7 @@ string compileMulti(string const& _input, bool _optimize, CStyleReadFileCallback errors.append("Error parsing input JSON: " + reader.getFormattedErrorMessages()); Json::Value output(Json::objectValue); output["errors"] = errors; - return jsonCompactPrint(output); + return dev::jsonCompactPrint(output); } else { diff --git a/test/libsolidity/SolidityNatspecJSON.cpp b/test/libsolidity/SolidityNatspecJSON.cpp index facfcda7..f05542b1 100644 --- a/test/libsolidity/SolidityNatspecJSON.cpp +++ b/test/libsolidity/SolidityNatspecJSON.cpp @@ -26,6 +26,7 @@ #include #include #include +#include namespace dev { @@ -57,7 +58,7 @@ public: BOOST_CHECK_MESSAGE( expectedDocumentation == generatedDocumentation, "Expected " << _expectedDocumentationString << - "\n but got:\n" << Json::StyledWriter().write(generatedDocumentation) + "\n but got:\n" << dev::jsonPrettyPrint(generatedDocumentation) ); } -- cgit v1.2.3