Coverage Report

Created: 2022-08-24 06:55

/src/solidity/libsolutil/JSON.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
  This file is part of solidity.
3
4
  solidity is free software: you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
9
  solidity is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
// SPDX-License-Identifier: GPL-3.0
18
/** @file JSON.h
19
 * @date 2016
20
 *
21
 * JSON related helpers
22
 */
23
24
#pragma once
25
26
#include <json/json.h>
27
28
#include <string>
29
30
namespace solidity::util
31
{
32
33
/// Removes members with null value recursively from (@a _json).
34
Json::Value removeNullMembers(Json::Value _json);
35
36
/// JSON printing format.
37
struct JsonFormat
38
{
39
  enum Format
40
  {
41
    Compact, // No unnecessary whitespace (including new lines and indentation)
42
    Pretty,  // Nicely indented, with new lines
43
  };
44
45
  static constexpr uint32_t defaultIndent = 2;
46
47
0
  bool operator==(JsonFormat const& _other) const noexcept { return (format == _other.format) && (indent == _other.indent); }
48
0
  bool operator!=(JsonFormat const& _other) const noexcept { return !(*this == _other); }
49
50
  Format format = Compact;
51
  uint32_t indent = defaultIndent;
52
};
53
54
/// Serialise the JSON object (@a _input) with indentation
55
std::string jsonPrettyPrint(Json::Value const& _input);
56
57
/// Serialise the JSON object (@a _input) without indentation
58
std::string jsonCompactPrint(Json::Value const& _input);
59
60
/// Serialise the JSON object (@a _input) using specified format (@a _format)
61
std::string jsonPrint(Json::Value const& _input, JsonFormat const& _format);
62
63
/// Parse a JSON string (@a _input) with enabled strict-mode and writes resulting JSON object to (@a _json)
64
/// \param _input JSON input string
65
/// \param _json [out] resulting JSON object
66
/// \param _errs [out] Formatted error messages
67
/// \return \c true if the document was successfully parsed, \c false if an error occurred.
68
bool jsonParseStrict(std::string const& _input, Json::Value& _json, std::string* _errs = nullptr);
69
70
}