Move JSON helpers to libdevcore/json

This commit is contained in:
Alex Beregszaszi 2016-11-15 17:33:28 +00:00
parent 9205662de9
commit 81c50143f2
4 changed files with 57 additions and 29 deletions

44
libdevcore/JSON.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
/** @file JSON.h
* @date 2016
*
* JSON related helpers
*/
#pragma once
#include <json/json.h>
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);
}
}

View File

@ -41,6 +41,7 @@
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <libdevcore/JSON.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libevmasm/GasMeter.h> #include <libevmasm/GasMeter.h>
#include <libsolidity/interface/Version.h> #include <libsolidity/interface/Version.h>
@ -107,18 +108,6 @@ static void version()
exit(0); 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) static bool needsHumanTargetedStdout(po::variables_map const& _args)
{ {
if (_args.count(g_argGas)) if (_args.count(g_argGas))
@ -244,9 +233,9 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co
{ {
std::string output; std::string output;
if (_type == DocumentationType::ABIInterface) if (_type == DocumentationType::ABIInterface)
output = jsonCompactPrint(m_compiler->metadata(_contract, _type)); output = dev::jsonCompactPrint(m_compiler->metadata(_contract, _type));
else else
output = jsonPrettyPrint(m_compiler->metadata(_contract, _type)); output = dev::jsonPrettyPrint(m_compiler->metadata(_contract, _type));
if (m_args.count("output-dir")) if (m_args.count("output-dir"))
createFile(_contract + suffix, output); createFile(_contract + suffix, output);
@ -669,7 +658,7 @@ void CommandLineInterface::handleCombinedJSON()
{ {
Json::Value contractData(Json::objectValue); Json::Value contractData(Json::objectValue);
if (requests.count("abi")) if (requests.count("abi"))
contractData["abi"] = jsonCompactPrint(m_compiler->interface(contractName)); contractData["abi"] = dev::jsonCompactPrint(m_compiler->interface(contractName));
if (requests.count("bin")) if (requests.count("bin"))
contractData["bin"] = m_compiler->object(contractName).toHex(); contractData["bin"] = m_compiler->object(contractName).toHex();
if (requests.count("bin-runtime")) if (requests.count("bin-runtime"))
@ -694,9 +683,9 @@ void CommandLineInterface::handleCombinedJSON()
contractData["srcmap-runtime"] = map ? *map : ""; contractData["srcmap-runtime"] = map ? *map : "";
} }
if (requests.count("devdoc")) 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")) 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; output["contracts"][contractName] = contractData;
} }
@ -720,7 +709,7 @@ void CommandLineInterface::handleCombinedJSON()
output["sources"][sourceCode.first]["AST"] = converter.json(); output["sources"][sourceCode.first]["AST"] = converter.json();
} }
} }
cout << jsonCompactPrint(output) << endl; cout << dev::jsonCompactPrint(output) << endl;
} }
void CommandLineInterface::handleAst(string const& _argStr) void CommandLineInterface::handleAst(string const& _argStr)

View File

@ -27,6 +27,7 @@
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <libdevcore/JSON.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libevmasm/GasMeter.h> #include <libevmasm/GasMeter.h>
#include <libsolidity/parsing/Scanner.h> #include <libsolidity/parsing/Scanner.h>
@ -125,13 +126,6 @@ Json::Value estimateGas(CompilerStack const& _compiler, string const& _contract)
return gasEstimates; 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) string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback _readCallback)
{ {
Json::Value output(Json::objectValue); Json::Value output(Json::objectValue);
@ -220,7 +214,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback
for (string const& contractName: compiler.contractNames()) for (string const& contractName: compiler.contractNames())
{ {
Json::Value contractData(Json::objectValue); 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["bytecode"] = compiler.object(contractName).toHex();
contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex(); contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex();
contractData["opcodes"] = solidity::disassemble(compiler.object(contractName).bytecode); contractData["opcodes"] = solidity::disassemble(compiler.object(contractName).bytecode);
@ -281,7 +275,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback
try try
{ {
return jsonCompactPrint(output); return dev::jsonCompactPrint(output);
} }
catch (...) catch (...)
{ {
@ -299,7 +293,7 @@ string compileMulti(string const& _input, bool _optimize, CStyleReadFileCallback
errors.append("Error parsing input JSON: " + reader.getFormattedErrorMessages()); errors.append("Error parsing input JSON: " + reader.getFormattedErrorMessages());
Json::Value output(Json::objectValue); Json::Value output(Json::objectValue);
output["errors"] = errors; output["errors"] = errors;
return jsonCompactPrint(output); return dev::jsonCompactPrint(output);
} }
else else
{ {

View File

@ -26,6 +26,7 @@
#include <libsolidity/interface/CompilerStack.h> #include <libsolidity/interface/CompilerStack.h>
#include <libsolidity/interface/Exceptions.h> #include <libsolidity/interface/Exceptions.h>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include <libdevcore/JSON.h>
namespace dev namespace dev
{ {
@ -57,7 +58,7 @@ public:
BOOST_CHECK_MESSAGE( BOOST_CHECK_MESSAGE(
expectedDocumentation == generatedDocumentation, expectedDocumentation == generatedDocumentation,
"Expected " << _expectedDocumentationString << "Expected " << _expectedDocumentationString <<
"\n but got:\n" << Json::StyledWriter().write(generatedDocumentation) "\n but got:\n" << dev::jsonPrettyPrint(generatedDocumentation)
); );
} }