Skip to content

Commit

Permalink
Merge pull request #15079 from ethereum/remove_jsoncpp_formatting
Browse files Browse the repository at this point in the history
Reformat tests in nlohmann::json style.
  • Loading branch information
aarlt committed May 7, 2024
2 parents 38ea398 + 3485bc9 commit 5091b5b
Show file tree
Hide file tree
Showing 470 changed files with 4,707 additions and 9,427 deletions.
64 changes: 0 additions & 64 deletions libsolutil/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
#include <libsolutil/CommonData.h>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>

#include <map>
#include <sstream>

#ifdef STRICT_NLOHMANN_JSON_VERSION_CHECK
Expand Down Expand Up @@ -65,64 +63,6 @@ void removeNullMembersHelper(Json& _json)
}
}

std::string trimRightAllLines(std::string const& input)
{
std::vector<std::string> lines;
std::string output;
boost::split(lines, input, boost::is_any_of("\n"));
for (auto& line: lines)
{
boost::trim_right(line);
if (!line.empty())
output += line + "\n";
}
return boost::trim_right_copy(output);
}

std::string formatLikeJsoncpp(std::string const& _dumped, JsonFormat const& _format)
{
uint32_t indentLevel = 0;
std::stringstream reformatted;
bool inQuotes = false;
for (size_t i = 0; i < _dumped.size(); ++i)
{
char c = _dumped[i];
bool emptyThing = false;

if (c == '"' && (i == 0 || _dumped[i - 1] != '\\'))
inQuotes = !inQuotes;

if (!inQuotes)
{
if (i < _dumped.size() - 1)
{
char nc = _dumped[i + 1];
if ((c == '[' && nc == ']') || (c == '{' && nc == '}'))
emptyThing = true;
}
if (c == '[' || c == '{')
{
if (i > 0 && _dumped[i - 1] != '\n')
if (!emptyThing)
reformatted << '\n' << std::string(indentLevel * _format.indent, ' ');
indentLevel++;
}
else if (c == ']' || c == '}')
{
indentLevel--;
if (i + 1 < _dumped.size() && _dumped[i + 1] != '\n'
&& (_dumped[i + 1] == ']' || _dumped[i + 1] == '}'))
reformatted << '\n' << std::string(indentLevel * _format.indent, ' ');
}
}
reformatted << c;
if (!emptyThing && !inQuotes && (c == '[' || c == '{') && indentLevel > 0 && i + 1 < _dumped.size()
&& _dumped[i + 1] != '\n')
reformatted << '\n' << std::string(indentLevel * _format.indent, ' ');
}
return trimRightAllLines(reformatted.str());
}

std::string escapeNewlinesAndTabsWithinStringLiterals(std::string const& _json)
{
std::stringstream fixed;
Expand Down Expand Up @@ -201,10 +141,6 @@ std::string jsonPrint(Json const& _input, JsonFormat const& _format)
/* ensure_ascii */ true
);

// let's remove this once all test-cases having the correct output.
if (_format.format == JsonFormat::Pretty)
dumped = formatLikeJsoncpp(dumped, _format);

return dumped;
}

Expand Down
36 changes: 12 additions & 24 deletions test/cmdlineTests/abi_via_ir/output
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
Contract JSON ABI
[
{
"inputs":
[
"inputs": [
{
"internalType": "uint256",
"name": "z",
Expand All @@ -20,8 +19,7 @@ Contract JSON ABI
"type": "error"
},
{
"inputs":
[
"inputs": [
{
"internalType": "uint256",
"name": "z",
Expand All @@ -32,8 +30,7 @@ Contract JSON ABI
"type": "error"
},
{
"inputs":
[
"inputs": [
{
"internalType": "uint256",
"name": "r",
Expand All @@ -45,8 +42,7 @@ Contract JSON ABI
},
{
"anonymous": true,
"inputs":
[
"inputs": [
{
"indexed": false,
"internalType": "uint256",
Expand All @@ -59,8 +55,7 @@ Contract JSON ABI
},
{
"anonymous": false,
"inputs":
[
"inputs": [
{
"indexed": false,
"internalType": "uint256",
Expand All @@ -73,8 +68,7 @@ Contract JSON ABI
},
{
"anonymous": false,
"inputs":
[
"inputs": [
{
"indexed": false,
"internalType": "uint256",
Expand All @@ -86,11 +80,9 @@ Contract JSON ABI
"type": "event"
},
{
"inputs":
[
"inputs": [
{
"components":
[
"components": [
{
"internalType": "uint256",
"name": "x",
Expand All @@ -113,8 +105,7 @@ Contract JSON ABI
Contract JSON ABI
[
{
"inputs":
[
"inputs": [
{
"internalType": "uint256",
"name": "r",
Expand All @@ -125,8 +116,7 @@ Contract JSON ABI
"type": "error"
},
{
"inputs":
[
"inputs": [
{
"internalType": "uint256",
"name": "u",
Expand All @@ -138,8 +128,7 @@ Contract JSON ABI
},
{
"anonymous": false,
"inputs":
[
"inputs": [
{
"indexed": false,
"internalType": "uint256",
Expand All @@ -152,8 +141,7 @@ Contract JSON ABI
},
{
"anonymous": false,
"inputs":
[
"inputs": [
{
"indexed": false,
"internalType": "uint256",
Expand Down
15 changes: 5 additions & 10 deletions test/cmdlineTests/asm_json/output
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
======= asm_json/input.sol:C =======
EVM assembly:
{
".code":
[
".code": [
{
"begin": 60,
"end": 160,
Expand Down Expand Up @@ -140,13 +139,10 @@ EVM assembly:
"source": 0
}
],
".data":
{
"0":
{
".data": {
"0": {
".auxdata": "<BYTECODE REMOVED>",
".code":
[
".code": [
{
"begin": 60,
"end": 160,
Expand Down Expand Up @@ -1570,8 +1566,7 @@ EVM assembly:
]
}
},
"sourceList":
[
"sourceList": [
"asm_json/input.sol",
"#utility.yul"
]
Expand Down
27 changes: 9 additions & 18 deletions test/cmdlineTests/ast_compact_json_with_base_path/output
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ JSON AST (compact format):
======= ast_compact_json_with_base_path/c.sol =======
{
"absolutePath": "ast_compact_json_with_base_path/c.sol",
"exportedSymbols":
{
"C":
[
"exportedSymbols": {
"C": [
5
]
},
"id": 6,
"license": "GPL-3.0",
"nodeType": "SourceUnit",
"nodes":
[
"nodes": [
{
"id": 4,
"literals":
[
"literals": [
"solidity",
">=",
"0.0"
Expand All @@ -35,8 +31,7 @@ JSON AST (compact format):
"contractKind": "contract",
"fullyImplemented": true,
"id": 5,
"linearizedBaseContracts":
[
"linearizedBaseContracts": [
5
],
"name": "C",
Expand All @@ -54,22 +49,18 @@ JSON AST (compact format):
======= ast_compact_json_with_base_path/input.sol =======
{
"absolutePath": "ast_compact_json_with_base_path/input.sol",
"exportedSymbols":
{
"C":
[
"exportedSymbols": {
"C": [
5
]
},
"id": 3,
"license": "GPL-3.0",
"nodeType": "SourceUnit",
"nodes":
[
"nodes": [
{
"id": 1,
"literals":
[
"literals": [
"solidity",
">=",
"0.0"
Expand Down
Loading

0 comments on commit 5091b5b

Please sign in to comment.