Using jsoncpp for exporting ABI interface from solidity

- Also changing the interface JSON test to have a shorter name
  plus to provide meaningful error message in case of failure
This commit is contained in:
Lefteris Karapetsas 2014-12-01 18:01:42 +01:00
parent 43d6726dd7
commit 0f79ed6957

View File

@ -85,54 +85,52 @@ void CompilerStack::streamAssembly(ostream& _outStream)
string const& CompilerStack::getInterface() string const& CompilerStack::getInterface()
{ {
Json::StyledWriter writer;
if (!m_parseSuccessful) if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
if (m_interface.empty()) if (m_interface.empty())
{ {
stringstream interface; Json::Value methods(Json::arrayValue);
interface << '[';
vector<FunctionDefinition const*> exportedFunctions = m_contractASTNode->getInterfaceFunctions(); vector<FunctionDefinition const*> exportedFunctions = m_contractASTNode->getInterfaceFunctions();
unsigned functionsCount = exportedFunctions.size();
for (FunctionDefinition const* f: exportedFunctions) for (FunctionDefinition const* f: exportedFunctions)
{ {
auto streamVariables = [&](vector<ASTPointer<VariableDeclaration>> const& _vars) Json::Value method;
Json::Value inputs(Json::arrayValue);
Json::Value outputs(Json::arrayValue);
auto streamVariables = [&](vector<ASTPointer<VariableDeclaration>> const& _vars,
Json::Value &json)
{ {
unsigned varCount = _vars.size();
for (ASTPointer<VariableDeclaration> const& var: _vars) for (ASTPointer<VariableDeclaration> const& var: _vars)
{ {
interface << "{" Json::Value input;
<< "\"name\":" << escaped(var->getName(), false) << "," input["name"] = var->getName();
<< "\"type\":" << escaped(var->getType()->toString(), false) input["type"] = var->getType()->toString();
<< "}"; json.append(input);
if (--varCount > 0)
interface << ",";
} }
}; };
interface << '{' method["name"] = f->getName();
<< "\"name\":" << escaped(f->getName(), false) << "," streamVariables(f->getParameters(), inputs);
<< "\"inputs\":["; method["inputs"] = inputs;
streamVariables(f->getParameters()); streamVariables(f->getReturnParameters(), outputs);
interface << "]," method["outputs"] = outputs;
<< "\"outputs\":[";
streamVariables(f->getReturnParameters()); methods.append(method);
interface << "]"
<< "}";
if (--functionsCount > 0)
interface << ",";
} }
interface << ']'; m_interface = writer.write(methods);
m_interface = interface.str();
} }
return m_interface; return m_interface;
} }
string const& CompilerStack::getDocumentation() string const& CompilerStack::getDocumentation()
{ {
Json::StyledWriter writer; Json::StyledWriter writer;
if (!m_parseSuccessful) if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
if (m_documentation.empty()) if (m_documentation.empty())
{ {
Json::Value doc; Json::Value doc;