|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r59739 - in trunk/boost/property_tree: . detail
From: sebastian.redl_at_[hidden]
Date: 2010-02-17 13:55:12
Author: cornedbee
Date: 2010-02-17 13:55:11 EST (Wed, 17 Feb 2010)
New Revision: 59739
URL: http://svn.boost.org/trac/boost/changeset/59739
Log:
Give the JSON writer the ability to NOT pretty-print, but instead produce compact code. Fixes bug 3828. But I still have to write test cases for the JSON writer; there aren't ANY!
Text files modified:
trunk/boost/property_tree/detail/json_parser_write.hpp | 46 ++++++++++++++++++++-------------------
trunk/boost/property_tree/json_parser.hpp | 14 ++++++++---
2 files changed, 34 insertions(+), 26 deletions(-)
Modified: trunk/boost/property_tree/detail/json_parser_write.hpp
==============================================================================
--- trunk/boost/property_tree/detail/json_parser_write.hpp (original)
+++ trunk/boost/property_tree/detail/json_parser_write.hpp 2010-02-17 13:55:11 EST (Wed, 17 Feb 2010)
@@ -60,17 +60,16 @@
template<class Ptree>
void write_json_helper(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
- const Ptree &pt,
- int indent)
+ const Ptree &pt,
+ int indent, bool pretty)
{
typedef typename Ptree::key_type::value_type Ch;
typedef typename std::basic_string<Ch> Str;
-
+
// Value or object or array
if (indent > 0 && pt.empty())
{
-
// Write value
Str data = create_escapes(pt.template get_value<Str>());
stream << Ch('"') << data << Ch('"');
@@ -78,42 +77,44 @@
}
else if (indent > 0 && pt.count(Str()) == pt.size())
{
-
// Write array
- stream << Ch('[') << Ch('\n');
+ stream << Ch('[');
+ if (pretty) stream << Ch('\n');
typename Ptree::const_iterator it = pt.begin();
for (; it != pt.end(); ++it)
{
- stream << Str(4 * (indent + 1), Ch(' '));
- write_json_helper(stream, it->second, indent + 1);
+ if (pretty) stream << Str(4 * (indent + 1), Ch(' '));
+ write_json_helper(stream, it->second, indent + 1, pretty);
if (boost::next(it) != pt.end())
stream << Ch(',');
- stream << Ch('\n');
+ if (pretty) stream << Ch('\n');
}
stream << Str(4 * indent, Ch(' ')) << Ch(']');
}
else
{
-
// Write object
- stream << Ch('{') << Ch('\n');
+ stream << Ch('{');
+ if (pretty) stream << Ch('\n');
typename Ptree::const_iterator it = pt.begin();
for (; it != pt.end(); ++it)
{
- stream << Str(4 * (indent + 1), Ch(' '));
+ if (pretty) stream << Str(4 * (indent + 1), Ch(' '));
stream << Ch('"') << create_escapes(it->first) << Ch('"') << Ch(':');
- if (it->second.empty())
- stream << Ch(' ');
- else
- stream << Ch('\n') << Str(4 * (indent + 1), Ch(' '));
- write_json_helper(stream, it->second, indent + 1);
+ if (pretty) {
+ if (it->second.empty())
+ stream << Ch(' ');
+ else
+ stream << Ch('\n') << Str(4 * (indent + 1), Ch(' '));
+ }
+ write_json_helper(stream, it->second, indent + 1, pretty);
if (boost::next(it) != pt.end())
stream << Ch(',');
- stream << Ch('\n');
+ if (pretty) stream << Ch('\n');
}
- stream << Str(4 * indent, Ch(' ')) << Ch('}');
-
+ if (pretty) stream << Str(4 * indent, Ch(' '));
+ stream << Ch('}');
}
}
@@ -149,11 +150,12 @@
template<class Ptree>
void write_json_internal(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
const Ptree &pt,
- const std::string &filename)
+ const std::string &filename,
+ bool pretty)
{
if (!verify_json(pt, 0))
BOOST_PROPERTY_TREE_THROW(json_parser_error("ptree contains data that cannot be represented in JSON format", filename, 0));
- write_json_helper(stream, pt, 0);
+ write_json_helper(stream, pt, 0, pretty);
stream << std::endl;
if (!stream.good())
BOOST_PROPERTY_TREE_THROW(json_parser_error("write error", filename, 0));
Modified: trunk/boost/property_tree/json_parser.hpp
==============================================================================
--- trunk/boost/property_tree/json_parser.hpp (original)
+++ trunk/boost/property_tree/json_parser.hpp 2010-02-17 13:55:11 EST (Wed, 17 Feb 2010)
@@ -85,14 +85,17 @@
* @param stream The stream to which to write the JSON representation of the
* property tree.
* @param pt The property tree to tranlsate to JSON and output.
+ * @param pretty Whether to pretty-print. Defaults to true for backward
+ * compatibility.
*/
template<class Ptree>
void write_json(std::basic_ostream<
typename Ptree::key_type::value_type
> &stream,
- const Ptree &pt)
+ const Ptree &pt,
+ bool pretty = true)
{
- write_json_internal(stream, pt, std::string());
+ write_json_internal(stream, pt, std::string(), pretty);
}
/**
@@ -106,11 +109,14 @@
* representation of the property tree.
* @param pt The property tree to translate to JSON and output.
* @param loc The locale to use when writing out to the output file.
+ * @param pretty Whether to pretty-print. Defaults to true and last place
+ * for backward compatibility.
*/
template<class Ptree>
void write_json(const std::string &filename,
const Ptree &pt,
- const std::locale &loc = std::locale())
+ const std::locale &loc = std::locale(),
+ bool pretty = true)
{
std::basic_ofstream<typename Ptree::key_type::value_type>
stream(filename.c_str());
@@ -118,7 +124,7 @@
BOOST_PROPERTY_TREE_THROW(json_parser_error(
"cannot open file", filename, 0));
stream.imbue(loc);
- write_json_internal(stream, pt, filename);
+ write_json_internal(stream, pt, filename, pretty);
}
} } }
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk