1 | #ifndef JSON_PARSER_PREFIXING_CALLBACKS_HPP |
2 | #define JSON_PARSER_PREFIXING_CALLBACKS_HPP |
3 | |
4 | #include <boost/property_tree/json_parser/detail/standard_callbacks.hpp> |
5 | |
6 | namespace constants |
7 | { |
8 | template <typename Ch> const Ch* null_prefix(); |
9 | template <> inline const char* null_prefix() { return "_:" ; } |
10 | template <> inline const wchar_t* null_prefix() { return L"_:" ; } |
11 | |
12 | template <typename Ch> const Ch* boolean_prefix(); |
13 | template <> inline const char* boolean_prefix() { return "b:" ; } |
14 | template <> inline const wchar_t* boolean_prefix() { return L"b:" ; } |
15 | |
16 | template <typename Ch> const Ch* number_prefix(); |
17 | template <> inline const char* number_prefix() { return "n:" ; } |
18 | template <> inline const wchar_t* number_prefix() { return L"n:" ; } |
19 | |
20 | template <typename Ch> const Ch* string_prefix(); |
21 | template <> inline const char* string_prefix() { return "s:" ; } |
22 | template <> inline const wchar_t* string_prefix() { return L"s:" ; } |
23 | |
24 | template <typename Ch> const Ch* array_prefix(); |
25 | template <> inline const char* array_prefix() { return "a:" ; } |
26 | template <> inline const wchar_t* array_prefix() { return L"a:" ; } |
27 | |
28 | template <typename Ch> const Ch* object_prefix(); |
29 | template <> inline const char* object_prefix() { return "o:" ; } |
30 | template <> inline const wchar_t* object_prefix() { return L"o:" ; } |
31 | } |
32 | |
33 | template <typename Ptree> |
34 | struct prefixing_callbacks |
35 | : boost::property_tree::json_parser::detail::standard_callbacks<Ptree> { |
36 | typedef boost::property_tree::json_parser::detail::standard_callbacks<Ptree> |
37 | base; |
38 | typedef typename base::string string; |
39 | typedef typename base::char_type char_type; |
40 | |
41 | void on_null() { |
42 | base::on_null(); |
43 | this->current_value().insert(0, constants::null_prefix<char_type>()); |
44 | } |
45 | |
46 | void on_boolean(bool b) { |
47 | base::on_boolean(b); |
48 | this->current_value().insert(0, constants::boolean_prefix<char_type>()); |
49 | } |
50 | |
51 | template <typename Range> |
52 | void on_number(Range code_units) { |
53 | base::on_number(code_units); |
54 | this->current_value().insert(0, constants::number_prefix<char_type>()); |
55 | } |
56 | void on_begin_number() { |
57 | base::on_begin_number(); |
58 | this->current_value() = constants::number_prefix<char_type>(); |
59 | } |
60 | |
61 | void on_begin_string() { |
62 | base::on_begin_string(); |
63 | if (!this->is_key()) { |
64 | this->current_value() = constants::string_prefix<char_type>(); |
65 | } |
66 | } |
67 | |
68 | void on_begin_array() { |
69 | base::on_begin_array(); |
70 | this->current_value() = constants::array_prefix<char_type>(); |
71 | } |
72 | |
73 | void on_begin_object() { |
74 | base::on_begin_object(); |
75 | this->current_value() = constants::object_prefix<char_type>(); |
76 | } |
77 | }; |
78 | |
79 | #endif |
80 | |