Skip to content

Commit 9c79dfa

Browse files
committed
Can now parse optionals, which do not throw an error, if missing.
1 parent 3a5b0ac commit 9c79dfa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

parse/jsd_optional.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef JSD_OPTIONAL_H_INCLUDED
2+
#define JSD_OPTIONAL_H_INCLUDED
3+
4+
#include <boost/optional.hpp>
5+
6+
namespace JSON
7+
{
8+
template <typename T>
9+
void parse(boost::optional <T>& value, std::string const& name,
10+
PropertyTree const& object, ParsingOptions const& options = DEFAULT_PARSER_OPTIONS)
11+
{
12+
try
13+
{
14+
auto opt = object.tree.get_optional <T> (name);
15+
if (!opt)
16+
return; // its ok, it was optional
17+
else
18+
value = opt.get();
19+
}
20+
catch (boost::property_tree::ptree_bad_data& exc)
21+
{
22+
DEFAULT_PROPERTY_ERROR_HANDLER(T(), T());
23+
}
24+
// cannot throw ptree_bad_path
25+
}
26+
}
27+
28+
#endif // JSD_OPTIONAL_H_INCLUDED

0 commit comments

Comments
 (0)