forked from 5cript/SimpleJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsd_generic_parser.cpp
More file actions
78 lines (67 loc) · 1.37 KB
/
jsd_generic_parser.cpp
File metadata and controls
78 lines (67 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "jsd_generic_parser.h"
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <sstream>
namespace JSON
{
using namespace boost::property_tree;
PropertyTree::PropertyTree(boost::property_tree::ptree tree)
: tree(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);
ptree pt;
read_json(sstr, pt);
return PropertyTree(pt);
}
boost::optional<PropertyTree> parse_auto(std::istream& stream)
{
ptree pt;
do {
try {
read_xml(stream, pt);
break;
}
catch (...) {
// failed
}
try {
read_ini(stream, pt);
break;
}
catch (...) {
// failed
}
try {
read_json(stream, pt);
break;
}
catch (...) {
// failed
}
return boost::optional<PropertyTree>();
} while (0);
return boost::make_optional( PropertyTree(pt) );
}
boost::optional<PropertyTree> parse_xml(std::istream& stream)
{
ptree pt;
try {
read_xml(stream, pt);
return boost::make_optional( PropertyTree(pt) );
}
catch (...) {
return boost::optional<PropertyTree>();
}
}
}