json_parser.hpp 5.8 KB

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