prefixing_callbacks.hpp 2.7 KB

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