Chris@16: // ---------------------------------------------------------------------------- Chris@16: // Copyright (C) 2002-2006 Marcin Kalicinski Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see www.boost.org Chris@16: // ---------------------------------------------------------------------------- Chris@16: #ifndef BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_WRITE_HPP_INCLUDED Chris@16: #define BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_WRITE_HPP_INCLUDED Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace property_tree { namespace json_parser Chris@16: { Chris@16: Chris@16: // Create necessary escape sequences from illegal characters Chris@16: template Chris@16: std::basic_string create_escapes(const std::basic_string &s) Chris@16: { Chris@16: std::basic_string result; Chris@16: typename std::basic_string::const_iterator b = s.begin(); Chris@16: typename std::basic_string::const_iterator e = s.end(); Chris@16: while (b != e) Chris@16: { Chris@101: typedef typename make_unsigned::type UCh; Chris@101: UCh c(*b); Chris@16: // This assumes an ASCII superset. But so does everything in PTree. Chris@16: // We escape everything outside ASCII, because this code can't Chris@16: // handle high unicode characters. Chris@101: if (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) || Chris@101: (c >= 0x30 && c <= 0x5B) || (c >= 0x5D && c <= 0xFF)) Chris@16: result += *b; Chris@16: else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b'); Chris@16: else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f'); Chris@16: else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n'); Chris@16: else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r'); Chris@101: else if (*b == Ch('\t')) result += Ch('\\'), result += Ch('t'); Chris@16: else if (*b == Ch('/')) result += Ch('\\'), result += Ch('/'); Chris@16: else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"'); Chris@16: else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\'); Chris@16: else Chris@16: { Chris@16: const char *hexdigits = "0123456789ABCDEF"; Chris@16: unsigned long u = (std::min)(static_cast( Chris@16: static_cast(*b)), Chris@16: 0xFFFFul); Chris@16: int d1 = u / 4096; u -= d1 * 4096; Chris@16: int d2 = u / 256; u -= d2 * 256; Chris@16: int d3 = u / 16; u -= d3 * 16; Chris@16: int d4 = u; Chris@16: result += Ch('\\'); result += Ch('u'); Chris@16: result += Ch(hexdigits[d1]); result += Ch(hexdigits[d2]); Chris@16: result += Ch(hexdigits[d3]); result += Ch(hexdigits[d4]); Chris@16: } Chris@16: ++b; Chris@16: } Chris@16: return result; Chris@16: } Chris@16: Chris@16: template Chris@16: void write_json_helper(std::basic_ostream &stream, Chris@16: const Ptree &pt, Chris@16: int indent, bool pretty) Chris@16: { Chris@16: Chris@16: typedef typename Ptree::key_type::value_type Ch; Chris@16: typedef typename std::basic_string Str; Chris@16: Chris@16: // Value or object or array Chris@16: if (indent > 0 && pt.empty()) Chris@16: { Chris@16: // Write value Chris@16: Str data = create_escapes(pt.template get_value()); Chris@16: stream << Ch('"') << data << Ch('"'); Chris@16: Chris@16: } Chris@16: else if (indent > 0 && pt.count(Str()) == pt.size()) Chris@16: { Chris@16: // Write array Chris@16: stream << Ch('['); Chris@16: if (pretty) stream << Ch('\n'); Chris@16: typename Ptree::const_iterator it = pt.begin(); Chris@16: for (; it != pt.end(); ++it) Chris@16: { Chris@16: if (pretty) stream << Str(4 * (indent + 1), Ch(' ')); Chris@16: write_json_helper(stream, it->second, indent + 1, pretty); Chris@16: if (boost::next(it) != pt.end()) Chris@16: stream << Ch(','); Chris@16: if (pretty) stream << Ch('\n'); Chris@16: } Chris@101: if (pretty) stream << Str(4 * indent, Ch(' ')); Chris@101: stream << Ch(']'); Chris@16: Chris@16: } Chris@16: else Chris@16: { Chris@16: // Write object Chris@16: stream << Ch('{'); Chris@16: if (pretty) stream << Ch('\n'); Chris@16: typename Ptree::const_iterator it = pt.begin(); Chris@16: for (; it != pt.end(); ++it) Chris@16: { Chris@16: if (pretty) stream << Str(4 * (indent + 1), Ch(' ')); Chris@16: stream << Ch('"') << create_escapes(it->first) << Ch('"') << Ch(':'); Chris@101: if (pretty) stream << Ch(' '); Chris@16: write_json_helper(stream, it->second, indent + 1, pretty); Chris@16: if (boost::next(it) != pt.end()) Chris@16: stream << Ch(','); Chris@16: if (pretty) stream << Ch('\n'); Chris@16: } Chris@16: if (pretty) stream << Str(4 * indent, Ch(' ')); Chris@16: stream << Ch('}'); Chris@16: } Chris@16: Chris@16: } Chris@16: Chris@16: // Verify if ptree does not contain information that cannot be written to json Chris@16: template Chris@16: bool verify_json(const Ptree &pt, int depth) Chris@16: { Chris@16: Chris@16: typedef typename Ptree::key_type::value_type Ch; Chris@16: typedef typename std::basic_string Str; Chris@16: Chris@16: // Root ptree cannot have data Chris@16: if (depth == 0 && !pt.template get_value().empty()) Chris@16: return false; Chris@16: Chris@16: // Ptree cannot have both children and data Chris@16: if (!pt.template get_value().empty() && !pt.empty()) Chris@16: return false; Chris@16: Chris@16: // Check children Chris@16: typename Ptree::const_iterator it = pt.begin(); Chris@16: for (; it != pt.end(); ++it) Chris@16: if (!verify_json(it->second, depth + 1)) Chris@16: return false; Chris@16: Chris@16: // Success Chris@16: return true; Chris@16: Chris@16: } Chris@16: Chris@16: // Write ptree to json stream Chris@16: template Chris@16: void write_json_internal(std::basic_ostream &stream, Chris@16: const Ptree &pt, Chris@16: const std::string &filename, Chris@16: bool pretty) Chris@16: { Chris@16: if (!verify_json(pt, 0)) Chris@16: BOOST_PROPERTY_TREE_THROW(json_parser_error("ptree contains data that cannot be represented in JSON format", filename, 0)); Chris@16: write_json_helper(stream, pt, 0, pretty); Chris@16: stream << std::endl; Chris@16: if (!stream.good()) Chris@16: BOOST_PROPERTY_TREE_THROW(json_parser_error("write error", filename, 0)); Chris@16: } Chris@16: Chris@16: } } } Chris@16: Chris@16: #endif