1 #ifndef BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_STANDARD_CALLBACKS_HPP
2 #define BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_STANDARD_CALLBACKS_HPP
4 #include <boost/assert.hpp>
5 #include <boost/property_tree/ptree.hpp>
8 namespace boost { namespace property_tree {
9 namespace json_parser { namespace detail
14 template <typename Ch> const Ch* null_value();
15 template <> inline const char* null_value() { return "null"; }
16 template <> inline const wchar_t* null_value() { return L"null"; }
18 template <typename Ch> const Ch* true_value();
19 template <> inline const char* true_value() { return "true"; }
20 template <> inline const wchar_t* true_value() { return L"true"; }
22 template <typename Ch> const Ch* false_value();
23 template <> inline const char* false_value() { return "false"; }
24 template <> inline const wchar_t* false_value() { return L"false"; }
27 template <typename Ptree>
28 class standard_callbacks {
30 typedef typename Ptree::data_type string;
31 typedef typename string::value_type char_type;
34 new_value() = constants::null_value<char_type>();
37 void on_boolean(bool b) {
38 new_value() = b ? constants::true_value<char_type>()
39 : constants::false_value<char_type>();
42 template <typename Range>
43 void on_number(Range code_units) {
44 new_value().assign(code_units.begin(), code_units.end());
46 void on_begin_number() {
49 void on_digit(char_type d) {
52 void on_end_number() {}
54 void on_begin_string() {
57 template <typename Range>
58 void on_code_units(Range code_units) {
59 current_value().append(code_units.begin(), code_units.end());
61 void on_code_unit(char_type c) {
64 void on_end_string() {}
66 void on_begin_array() {
68 stack.back().k = array;
71 if (stack.back().k == leaf) stack.pop_back();
75 void on_begin_object() {
77 stack.back().k = object;
79 void on_end_object() {
80 if (stack.back().k == leaf) stack.pop_back();
84 Ptree& output() { return root; }
88 return stack.back().k == key;
90 string& current_value() {
91 layer& l = stack.back();
93 case key: return key_buffer;
94 default: return l.t->data();
101 enum kind { array, object, key, leaf };
102 struct layer { kind k; Ptree* t; };
103 std::vector<layer> stack;
107 layer l = {leaf, &root};
111 layer& l = stack.back();
114 l.t->push_back(std::make_pair(string(), Ptree()));
115 layer nl = {leaf, &l.t->back().second};
117 return *stack.back().t;
121 BOOST_ASSERT(false); // must start with string, i.e. call new_value
123 l.t->push_back(std::make_pair(key_buffer, Ptree()));
125 layer nl = {leaf, &l.t->back().second};
127 return *stack.back().t;
134 string& new_value() {
135 if (stack.empty()) return new_tree().data();
136 layer& l = stack.back();
146 return new_tree().data();