json_parser_write.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_DETAIL_JSON_PARSER_WRITE_HPP_INCLUDED
  11. #define BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_WRITE_HPP_INCLUDED
  12. #include <boost/property_tree/ptree.hpp>
  13. #include <boost/next_prior.hpp>
  14. #include <boost/type_traits/make_unsigned.hpp>
  15. #include <string>
  16. #include <ostream>
  17. #include <iomanip>
  18. namespace boost { namespace property_tree { namespace json_parser
  19. {
  20. // Create necessary escape sequences from illegal characters
  21. template<class Ch>
  22. std::basic_string<Ch> create_escapes(const std::basic_string<Ch> &s)
  23. {
  24. std::basic_string<Ch> result;
  25. typename std::basic_string<Ch>::const_iterator b = s.begin();
  26. typename std::basic_string<Ch>::const_iterator e = s.end();
  27. while (b != e)
  28. {
  29. // This assumes an ASCII superset. But so does everything in PTree.
  30. // We escape everything outside ASCII, because this code can't
  31. // handle high unicode characters.
  32. if (*b == 0x20 || *b == 0x21 || (*b >= 0x23 && *b <= 0x2E) ||
  33. (*b >= 0x30 && *b <= 0x5B) || (*b >= 0x5D && *b <= 0xFF)
  34. || (*b >= -0x80 && *b < 0))
  35. result += *b;
  36. else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
  37. else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');
  38. else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n');
  39. else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r');
  40. else if (*b == Ch('/')) result += Ch('\\'), result += Ch('/');
  41. else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
  42. else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
  43. else
  44. {
  45. const char *hexdigits = "0123456789ABCDEF";
  46. typedef typename make_unsigned<Ch>::type UCh;
  47. unsigned long u = (std::min)(static_cast<unsigned long>(
  48. static_cast<UCh>(*b)),
  49. 0xFFFFul);
  50. int d1 = u / 4096; u -= d1 * 4096;
  51. int d2 = u / 256; u -= d2 * 256;
  52. int d3 = u / 16; u -= d3 * 16;
  53. int d4 = u;
  54. result += Ch('\\'); result += Ch('u');
  55. result += Ch(hexdigits[d1]); result += Ch(hexdigits[d2]);
  56. result += Ch(hexdigits[d3]); result += Ch(hexdigits[d4]);
  57. }
  58. ++b;
  59. }
  60. return result;
  61. }
  62. template<class Ptree>
  63. void write_json_helper(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
  64. const Ptree &pt,
  65. int indent, bool pretty)
  66. {
  67. typedef typename Ptree::key_type::value_type Ch;
  68. typedef typename std::basic_string<Ch> Str;
  69. // Value or object or array
  70. if (indent > 0 && pt.empty())
  71. {
  72. // Write value
  73. Str data = create_escapes(pt.template get_value<Str>());
  74. stream << Ch('"') << data << Ch('"');
  75. }
  76. else if (indent > 0 && pt.count(Str()) == pt.size())
  77. {
  78. // Write array
  79. stream << Ch('[');
  80. if (pretty) stream << Ch('\n');
  81. typename Ptree::const_iterator it = pt.begin();
  82. for (; it != pt.end(); ++it)
  83. {
  84. if (pretty) stream << Str(4 * (indent + 1), Ch(' '));
  85. write_json_helper(stream, it->second, indent + 1, pretty);
  86. if (boost::next(it) != pt.end())
  87. stream << Ch(',');
  88. if (pretty) stream << Ch('\n');
  89. }
  90. stream << Str(4 * indent, Ch(' ')) << Ch(']');
  91. }
  92. else
  93. {
  94. // Write object
  95. stream << Ch('{');
  96. if (pretty) stream << Ch('\n');
  97. typename Ptree::const_iterator it = pt.begin();
  98. for (; it != pt.end(); ++it)
  99. {
  100. if (pretty) stream << Str(4 * (indent + 1), Ch(' '));
  101. stream << Ch('"') << create_escapes(it->first) << Ch('"') << Ch(':');
  102. if (pretty) {
  103. if (it->second.empty())
  104. stream << Ch(' ');
  105. else
  106. stream << Ch('\n') << Str(4 * (indent + 1), Ch(' '));
  107. }
  108. write_json_helper(stream, it->second, indent + 1, pretty);
  109. if (boost::next(it) != pt.end())
  110. stream << Ch(',');
  111. if (pretty) stream << Ch('\n');
  112. }
  113. if (pretty) stream << Str(4 * indent, Ch(' '));
  114. stream << Ch('}');
  115. }
  116. }
  117. // Verify if ptree does not contain information that cannot be written to json
  118. template<class Ptree>
  119. bool verify_json(const Ptree &pt, int depth)
  120. {
  121. typedef typename Ptree::key_type::value_type Ch;
  122. typedef typename std::basic_string<Ch> Str;
  123. // Root ptree cannot have data
  124. if (depth == 0 && !pt.template get_value<Str>().empty())
  125. return false;
  126. // Ptree cannot have both children and data
  127. if (!pt.template get_value<Str>().empty() && !pt.empty())
  128. return false;
  129. // Check children
  130. typename Ptree::const_iterator it = pt.begin();
  131. for (; it != pt.end(); ++it)
  132. if (!verify_json(it->second, depth + 1))
  133. return false;
  134. // Success
  135. return true;
  136. }
  137. // Write ptree to json stream
  138. template<class Ptree>
  139. void write_json_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
  140. const Ptree &pt,
  141. const std::string &filename,
  142. bool pretty)
  143. {
  144. if (!verify_json(pt, 0))
  145. BOOST_PROPERTY_TREE_THROW(json_parser_error("ptree contains data that cannot be represented in JSON format", filename, 0));
  146. write_json_helper(stream, pt, 0, pretty);
  147. stream << std::endl;
  148. if (!stream.good())
  149. BOOST_PROPERTY_TREE_THROW(json_parser_error("write error", filename, 0));
  150. }
  151. } } }
  152. #endif