json_parser.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. // ----------------------------------------------------------------------------
  10. #ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
  11. #define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
  12. #include <boost/property_tree/ptree.hpp>
  13. #include <boost/property_tree/detail/json_parser_read.hpp>
  14. #include <boost/property_tree/detail/json_parser_write.hpp>
  15. #include <boost/property_tree/detail/json_parser_error.hpp>
  16. #include <fstream>
  17. #include <string>
  18. #include <locale>
  19. namespace boost { namespace property_tree { namespace json_parser
  20. {
  21. /**
  22. * Read JSON from a the given stream and translate it to a property tree.
  23. * @note Clears existing contents of property tree. In case of error the
  24. * property tree unmodified.
  25. * @note Items of JSON arrays are translated into ptree keys with empty
  26. * names. Members of objects are translated into named keys.
  27. * @note JSON data can be a string, a numeric value, or one of literals
  28. * "null", "true" and "false". During parse, any of the above is
  29. * copied verbatim into ptree data string.
  30. * @throw json_parser_error In case of error deserializing the property
  31. * tree.
  32. * @param stream Stream from which to read in the property tree.
  33. * @param[out] pt The property tree to populate.
  34. */
  35. template<class Ptree>
  36. void read_json(std::basic_istream<
  37. typename Ptree::key_type::value_type
  38. > &stream,
  39. Ptree &pt)
  40. {
  41. read_json_internal(stream, pt, std::string());
  42. }
  43. /**
  44. * Read JSON from a the given file and translate it to a property tree.
  45. * @note Clears existing contents of property tree. In case of error the
  46. * property tree unmodified.
  47. * @note Items of JSON arrays are translated into ptree keys with empty
  48. * names. Members of objects are translated into named keys.
  49. * @note JSON data can be a string, a numeric value, or one of literals
  50. * "null", "true" and "false". During parse, any of the above is
  51. * copied verbatim into ptree data string.
  52. * @throw json_parser_error In case of error deserializing the property
  53. * tree.
  54. * @param filename Name of file from which to read in the property tree.
  55. * @param[out] pt The property tree to populate.
  56. * @param loc The locale to use when reading in the file contents.
  57. */
  58. template<class Ptree>
  59. void read_json(const std::string &filename,
  60. Ptree &pt,
  61. const std::locale &loc = std::locale())
  62. {
  63. std::basic_ifstream<typename Ptree::key_type::value_type>
  64. stream(filename.c_str());
  65. if (!stream)
  66. BOOST_PROPERTY_TREE_THROW(json_parser_error(
  67. "cannot open file", filename, 0));
  68. stream.imbue(loc);
  69. read_json_internal(stream, pt, filename);
  70. }
  71. /**
  72. * Translates the property tree to JSON and writes it the given output
  73. * stream.
  74. * @note Any property tree key containing only unnamed subkeys will be
  75. * rendered as JSON arrays.
  76. * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
  77. * @throw json_parser_error In case of error translating the property tree
  78. * to JSON or writing to the output stream.
  79. * @param stream The stream to which to write the JSON representation of the
  80. * property tree.
  81. * @param pt The property tree to tranlsate to JSON and output.
  82. * @param pretty Whether to pretty-print. Defaults to true for backward
  83. * compatibility.
  84. */
  85. template<class Ptree>
  86. void write_json(std::basic_ostream<
  87. typename Ptree::key_type::value_type
  88. > &stream,
  89. const Ptree &pt,
  90. bool pretty = true)
  91. {
  92. write_json_internal(stream, pt, std::string(), pretty);
  93. }
  94. /**
  95. * Translates the property tree to JSON and writes it the given file.
  96. * @note Any property tree key containing only unnamed subkeys will be
  97. * rendered as JSON arrays.
  98. * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
  99. * @throw json_parser_error In case of error translating the property tree
  100. * to JSON or writing to the file.
  101. * @param filename The name of the file to which to write the JSON
  102. * representation of the property tree.
  103. * @param pt The property tree to translate to JSON and output.
  104. * @param loc The locale to use when writing out to the output file.
  105. * @param pretty Whether to pretty-print. Defaults to true and last place
  106. * for backward compatibility.
  107. */
  108. template<class Ptree>
  109. void write_json(const std::string &filename,
  110. const Ptree &pt,
  111. const std::locale &loc = std::locale(),
  112. bool pretty = true)
  113. {
  114. std::basic_ofstream<typename Ptree::key_type::value_type>
  115. stream(filename.c_str());
  116. if (!stream)
  117. BOOST_PROPERTY_TREE_THROW(json_parser_error(
  118. "cannot open file", filename, 0));
  119. stream.imbue(loc);
  120. write_json_internal(stream, pt, filename, pretty);
  121. }
  122. } } }
  123. namespace boost { namespace property_tree
  124. {
  125. using json_parser::read_json;
  126. using json_parser::write_json;
  127. using json_parser::json_parser_error;
  128. } }
  129. #endif