Ethereum  PoC-8
The C++ Implementation of Ethereum
JsonUtils.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum 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  cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "JsonSpiritHeaders.h"
19 #include <libdevcore/JsonUtils.h>
20 #include <boost/algorithm/string/join.hpp>
21 #include <ostream>
22 #include <set>
23 #include <string>
24 
25 void dev::validateFieldNames(json_spirit::mObject const& _obj, std::set<std::string> const& _allowedFields)
26 {
27  for (auto const& elm: _obj)
28  if (_allowedFields.find(elm.first) == _allowedFields.end())
29  {
30  std::string const comment = "Unknown field in config: " + elm.first;
31  std::cerr << comment << "\n";
32  BOOST_THROW_EXCEPTION(UnknownField() << errinfo_comment(comment));
33  }
34 }
35 
36 std::string dev::jsonTypeAsString(json_spirit::Value_type _type)
37 {
38  switch (_type)
39  {
40  case json_spirit::obj_type:
41  return "json Object";
42  case json_spirit::array_type:
43  return "json Array";
44  case json_spirit::str_type:
45  return "json String";
46  case json_spirit::bool_type:
47  return "json Bool";
48  case json_spirit::int_type:
49  return "json Int";
50  case json_spirit::real_type:
51  return "json Real";
52  case json_spirit::null_type:
53  return "json Null";
54  default:
55  return "json n/a";
56  }
57 }
58 
59 void dev::requireJsonFields(json_spirit::mObject const& _o, std::string const& _config,
60  std::map<std::string, JsonFieldOptions> const& _validationMap)
61 {
62  // check for unexpected fiedls
63  for (auto const& field : _o)
64  {
65  if (!_validationMap.count(field.first))
66  {
67  std::string const comment =
68  "Unexpected field '" + field.first + "' in config: " + _config;
69  std::cerr << comment << "\n"
70  << json_spirit::write_string((json_spirit::mValue)_o, true) << "\n";
71  BOOST_THROW_EXCEPTION(UnknownField() << errinfo_comment(comment));
72  }
73  }
74 
75  // check field types with validation map
76  for (auto const vmap : _validationMap)
77  {
78  auto const& expectedFieldName = vmap.first;
79  auto const& expectedFieldPresence = vmap.second.second;
80  // check that all required fields are in the object
81  if (!_o.count(expectedFieldName))
82  {
83  if (expectedFieldPresence == JsonFieldPresence::Required)
84  {
85  std::string const comment =
86  "Expected field '" + expectedFieldName + "' not found in config: " + _config;
87  std::cerr << comment << "\n"
88  << json_spirit::write_string((json_spirit::mValue)_o, true) << "\n";
89  BOOST_THROW_EXCEPTION(MissingField() << errinfo_comment(comment));
90  }
91  else if (expectedFieldPresence == JsonFieldPresence::Optional)
92  continue;
93  }
94 
95  // check that field type is one of allowed field types
96  auto const& expectedFieldTypes = vmap.second.first;
97  bool matched = expectedFieldTypes.count(_o.at(expectedFieldName).type());
98  if (matched == false)
99  {
100  std::vector<std::string> types;
101  for (auto const& type : expectedFieldTypes)
102  types.push_back(jsonTypeAsString(type));
103  std::string sTypes = boost::algorithm::join(types, " or ");
104 
105  std::string const comment = "Field '" + expectedFieldName + "' is expected to be " +
106  sTypes + ", but is set to " +
107  jsonTypeAsString(_o.at(expectedFieldName).type()) + " in " +
108  _config;
109  std::cerr << comment << "\n"
110  << json_spirit::write_string((json_spirit::mValue)_o, true) << "\n";
111  BOOST_THROW_EXCEPTION(WrongFieldType() << errinfo_comment(comment));
112  }
113  }
114 }
dev::requireJsonFields
void requireJsonFields(json_spirit::mObject const &_o, std::string const &_configName, std::map< std::string, JsonFieldOptions > const &_validationMap)
Definition: JsonUtils.cpp:59
dev::jsonTypeAsString
std::string jsonTypeAsString(json_spirit::Value_type _type)
Definition: JsonUtils.cpp:36
JsonSpiritHeaders.h
JsonUtils.h
dev::validateFieldNames
void validateFieldNames(json_spirit::mObject const &_obj, std::set< std::string > const &_allowedFields)
Definition: JsonUtils.cpp:25
dev::errinfo_comment
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69