Skip to content

Commit

Permalink
Formatting changes, bugfix for std::array and a merge operation with …
Browse files Browse the repository at this point in the history
…default values was added.
  • Loading branch information
5cript committed Mar 23, 2018
1 parent 181d76f commit 7fdb3e4
Show file tree
Hide file tree
Showing 42 changed files with 549 additions and 121 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ BOOST_FUSION_ADAPT_STRUCT
(std::vector <std::string>, someContainer)
)

JSON_INJECT_STRINGIFY(ConfigContent)
JSON_INJECT_PARSE(ConfigContent)
SJSON_INJECT_STRINGIFY(ConfigContent)
SJSON_INJECT_PARSE(ConfigContent)
```
## Example 2
Expand Down
10 changes: 6 additions & 4 deletions parse/jsd_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace JSON
{
try
{
GET_CHILD(name, pt, {});
SJSON_GET_CHILD(name, pt, {});

int pos = 0;
for (auto const& i : pt)
Expand All @@ -23,15 +23,17 @@ namespace JSON
value[pos++] = temp;
else if (options.invalidPropertyHandler != InvalidPropertyHandlingBehaviour::IGNORE_ALL_ERROR)
throw std::out_of_range("there is more data to be read, but the array is full");
}
}
for (; pos < static_cast <decltype(pos)>(N); ++pos)
value[pos] = 0;
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER({}, {});
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER({}, {});
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER({}, {});
SJSON_DEFAULT_PATH_ERROR_HANDLER({}, {});
}
}
}
6 changes: 3 additions & 3 deletions parse/jsd_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace JSON
{
try
{
GET_CHILD(name, pt, {});
SJSON_GET_CHILD(name, pt, {});

/*
decltype(object.tree) pt;
Expand Down Expand Up @@ -46,11 +46,11 @@ namespace JSON
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER({},{});
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER({},{});
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER({},{});
SJSON_DEFAULT_PATH_ERROR_HANDLER({},{});
}
}
}
8 changes: 4 additions & 4 deletions parse/jsd_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace JSON
};
*/

#define GET_VALUE(TYPE, NAME, TEMP, TAG_VALUE) \
#define SJSON_GET_VALUE(TYPE, NAME, TEMP, TAG_VALUE) \
if (options.invalidPathHandler == InvalidPathHandlingBehaviour::IGNORE_ALL_ERROR || \
options.invalidPathHandler == InvalidPathHandlingBehaviour::TAG) \
{ \
Expand All @@ -82,7 +82,7 @@ namespace JSON
}
// MAKRO END

#define GET_CHILD(NAME, RESULT, TAG_VALUE) \
#define SJSON_GET_CHILD(NAME, RESULT, TAG_VALUE) \
decltype(object.tree) RESULT;\
if (options.invalidPathHandler == InvalidPathHandlingBehaviour::IGNORE_ALL_ERROR || \
options.invalidPathHandler == InvalidPathHandlingBehaviour::TAG) \
Expand All @@ -105,7 +105,7 @@ namespace JSON
}
// MAKRO END

#define DEFAULT_PROPERTY_ERROR_HANDLER(DEFAULT_VALUE, TAG_VALUE) \
#define SJSON_DEFAULT_PROPERTY_ERROR_HANDLER(DEFAULT_VALUE, TAG_VALUE) \
switch (options.invalidPropertyHandler) { \
case (InvalidPropertyHandlingBehaviour::DEFAULT): \
/* value = {}; */\
Expand All @@ -120,7 +120,7 @@ namespace JSON
}
// MAKRO END

#define DEFAULT_PATH_ERROR_HANDLER(DEFAULT_VALUE, TAG_VALUE) \
#define SJSON_DEFAULT_PATH_ERROR_HANDLER(DEFAULT_VALUE, TAG_VALUE) \
switch (options.invalidPathHandler) { \
case (InvalidPathHandlingBehaviour::DEFAULT): \
/* value = {}; */\
Expand Down
6 changes: 3 additions & 3 deletions parse/jsd_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ namespace JSON
{
using integer_type = typename type_of_size <sizeof(T) * 8>::type;
integer_type temp;
GET_VALUE(integer_type, name, temp, T());
SJSON_GET_VALUE(integer_type, name, temp, T());
value = static_cast <T>(temp);
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER(T(), T());
SJSON_DEFAULT_PATH_ERROR_HANDLER(T(), T());
}
}
}
12 changes: 6 additions & 6 deletions parse/jsd_fundamental.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ namespace JSON
try
{
std::string s;
GET_VALUE(std::string, name, s, char());
SJSON_GET_VALUE(std::string, name, s, char());

if (!s.empty())
value = s[0];
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER(char(), char());
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER(char(), char());
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER(char(), char());
SJSON_DEFAULT_PATH_ERROR_HANDLER(char(), char());
}
}
void parse(wchar_t& value, std::string const& name,
Expand All @@ -31,18 +31,18 @@ namespace JSON
try
{
std::string s;
GET_VALUE(std::string, name, s, wchar_t());
SJSON_GET_VALUE(std::string, name, s, wchar_t());

if (!s.empty())
value = s[0];
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER(wchar_t(), wchar_t());
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER(wchar_t(), wchar_t());
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER(wchar_t(), wchar_t());
SJSON_DEFAULT_PATH_ERROR_HANDLER(wchar_t(), wchar_t());
}
}
}
Expand Down
27 changes: 18 additions & 9 deletions parse/jsd_fundamental.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,33 @@

namespace JSON
{
template <typename T,
class = typename std::enable_if< (std::is_arithmetic<T>::value && !std::is_same<T, char>::value && !std::is_same<T, wchar_t>::value)
&& !std::is_enum<T>::value >::type
>
void parse(T& value, std::string const& name,
PropertyTree const& object, ParsingOptions const& options = {})
template <
typename T,
class = typename std::enable_if<(
std::is_arithmetic<T>::value &&
!std::is_same<T, char>::value &&
!std::is_same<T, wchar_t>::value) &&
!std::is_enum<T>::value
>::type
>
void parse(
T& value,
std::string const& name,
PropertyTree const& object,
ParsingOptions const& options = {}
)
{
try
{
GET_VALUE(T, name, value, T());
SJSON_GET_VALUE(T, name, value, T());
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER(T(), T());
SJSON_DEFAULT_PATH_ERROR_HANDLER(T(), T());
}
}

Expand Down
2 changes: 1 addition & 1 deletion parse/jsd_fusion_adapted_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace JSON
};
}

#define JSON_INJECT_PARSE(ClassName) \
#define SJSON_INJECT_PARSE(ClassName) \
namespace JSON \
{ \
void parse( \
Expand Down
15 changes: 11 additions & 4 deletions parse/jsd_generic_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
namespace JSON
{
using namespace boost::property_tree;

//#####################################################################################################################
PropertyTree::PropertyTree(boost::property_tree::ptree tree)
: tree(tree)
{

}
// ======================================================================================================
//---------------------------------------------------------------------------------------------------------------------
void PropertyTree::to_stream(std::ostream& stream)
{
write_json(stream, tree);
}
//#####################################################################################################################
PropertyTree parse_json(std::istream& stream)
{
ptree pt;
read_json(stream, pt);
return PropertyTree(pt);
}
//---------------------------------------------------------------------------------------------------------------------
PropertyTree parse_json(std::string const& str)
{
std::stringstream sstr(str);
Expand All @@ -28,7 +34,7 @@ namespace JSON
read_json(sstr, pt);
return PropertyTree(pt);
}

//---------------------------------------------------------------------------------------------------------------------
boost::optional<PropertyTree> parse_auto(std::istream& stream)
{
ptree pt;
Expand Down Expand Up @@ -62,7 +68,7 @@ namespace JSON

return boost::make_optional( PropertyTree(pt) );
}

//---------------------------------------------------------------------------------------------------------------------
boost::optional<PropertyTree> parse_xml(std::istream& stream)
{
ptree pt;
Expand All @@ -75,4 +81,5 @@ namespace JSON
return boost::optional<PropertyTree>();
}
}
//#####################################################################################################################
}
3 changes: 2 additions & 1 deletion parse/jsd_generic_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace JSON
{
boost::property_tree::ptree tree;

PropertyTree(boost::property_tree::ptree tree = {});
PropertyTree(boost::property_tree::ptree tree = {});
void to_stream(std::ostream& stream);
};

PropertyTree parse_json(std::istream& stream);
Expand Down
6 changes: 3 additions & 3 deletions parse/jsd_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace JSON
{
try
{
GET_CHILD(name, pt, (std::map<std::string, ValueT, CompareT, AllocT>()));
SJSON_GET_CHILD(name, pt, (std::map<std::string, ValueT, CompareT, AllocT>()));
for (auto const& i : pt)
{
ValueT temp;
Expand All @@ -21,11 +21,11 @@ namespace JSON
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER((std::map<std::string, ValueT, CompareT, AllocT>()), (std::map<std::string, ValueT, CompareT, AllocT>()));
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER((std::map<std::string, ValueT, CompareT, AllocT>()), (std::map<std::string, ValueT, CompareT, AllocT>()));
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER((std::map<std::string, ValueT, CompareT, AllocT>()), (std::map<std::string, ValueT, CompareT, AllocT>()));
SJSON_DEFAULT_PATH_ERROR_HANDLER((std::map<std::string, ValueT, CompareT, AllocT>()), (std::map<std::string, ValueT, CompareT, AllocT>()));
}
}
}
2 changes: 1 addition & 1 deletion parse/jsd_optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace JSON
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
}
// cannot throw ptree_bad_path
}
Expand Down
6 changes: 3 additions & 3 deletions parse/jsd_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace JSON
{
try
{
GET_CHILD(name, pt, std::set<T>{});
SJSON_GET_CHILD(name, pt, std::set<T>{});
for (auto const& i : pt)
{
T temp;
Expand All @@ -23,11 +23,11 @@ namespace JSON
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER(std::set<T>{}, std::set<T>{});
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER(std::set<T>{}, std::set<T>{});
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER(std::set<T>{}, std::set<T>{});
SJSON_DEFAULT_PATH_ERROR_HANDLER(std::set<T>{}, std::set<T>{});
}
}
}
6 changes: 3 additions & 3 deletions parse/jsd_smart_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace JSON
{
using element_type = typename std::decay<T>::type::element_type;
value.reset(new element_type{});
GET_VALUE(element_type, name, *value, {});
SJSON_GET_VALUE(element_type, name, *value, {});
}
};

Expand Down Expand Up @@ -69,11 +69,11 @@ namespace JSON
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER({},{});
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER({},{});
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER({},{});
SJSON_DEFAULT_PATH_ERROR_HANDLER({},{});
}
}
};
Expand Down
8 changes: 4 additions & 4 deletions parse/jsd_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ void parse(std::string& value, std::string const& name,
{
if (!options.strings_are_binary)
{
GET_VALUE(std::string, name, value, "");
SJSON_GET_VALUE(std::string, name, value, "");
}
else
{
std::string encoded;
GET_VALUE(std::string, name, encoded, "");
SJSON_GET_VALUE(std::string, name, encoded, "");
decodeBase64 <char, std::basic_string> (encoded, value);
}
}
catch (boost::property_tree::ptree_bad_data& exc)
{
DEFAULT_PROPERTY_ERROR_HANDLER("", "");
SJSON_DEFAULT_PROPERTY_ERROR_HANDLER("", "");
}
catch (boost::property_tree::ptree_bad_path& exc)
{
DEFAULT_PATH_ERROR_HANDLER("", "");
SJSON_DEFAULT_PATH_ERROR_HANDLER("", "");
}
}

Expand Down
Loading

0 comments on commit 7fdb3e4

Please sign in to comment.