From 56a1d6cbf5cd462dc18dd16e406f9303bc03c9bf Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Fri, 23 Mar 2007 09:57:01 +0000 Subject: [PATCH] Added istream/ostream funcs/operators --- doc/jsoncpp.dox | 7 +++++++ include/json/reader.h | 32 ++++++++++++++++++++++++++++++++ include/json/writer.h | 7 ++++++- src/lib_json/json_reader.cpp | 29 +++++++++++++++++++++++++++++ src/lib_json/json_writer.cpp | 8 ++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) diff --git a/doc/jsoncpp.dox b/doc/jsoncpp.dox index 5463463..3667fa6 100644 --- a/doc/jsoncpp.dox +++ b/doc/jsoncpp.dox @@ -65,6 +65,13 @@ root["indent"]["use_space"] = getCurrentIndentUseSpace(); Json::StyledWriter writer; // Make a new JSON document for the configuration. Preserve original comments. std::string outputConfig = writer.write( root ); + +// You can also use streams. This will put the contents of any JSON +// stream at a particular sub-value, if you'd like. +std::cin >> root["subtree"]; + +// And you can write to a stream, using the StyledWriter automatically. +std::cout << root; \endcode \section _plinks Build instructions diff --git a/include/json/reader.h b/include/json/reader.h index 60594d9..cb76fab 100644 --- a/include/json/reader.h +++ b/include/json/reader.h @@ -6,6 +6,7 @@ # include # include # include +# include namespace Json { @@ -47,6 +48,12 @@ namespace Json { Value &root, bool collectComments = true ); + /// \brief Parse from input stream. + /// \see Json::operator>>(std::istream&, Json::Value&). + bool parse( std::istream&, + Value &root, + bool collectComments = true ); + /** \brief Returns a user friendly string that list errors in the parsed document. * \return Formatted error message with the list of errors with their location in * the parsed document. An empty string is returned if no error occurred @@ -144,6 +151,31 @@ namespace Json { bool collectComments_; }; + /** \brief Read from 'sin' into 'root'. + + Always keep comments from the input JSON. + + This can be used to read a file into a particular sub-object. + For example: + \code + Json::Value root; + cin >> root["dir"]["file"]; + cout << root; + \endcode + Result: + \verbatim + { + "dir": { + "file": { + // The input stream JSON would be nested here. + } + } + } + \endverbatim + \throw std::exception on parse error. + \see Json::operator<<() + */ + std::istream& operator>>( std::istream&, Value& ); } // namespace Json diff --git a/include/json/writer.h b/include/json/writer.h index 94582d9..1312dbc 100644 --- a/include/json/writer.h +++ b/include/json/writer.h @@ -4,6 +4,7 @@ # include "value.h" # include # include +# include namespace Json { @@ -68,7 +69,7 @@ namespace Json { public: // overridden from Writer /** \brief Serialize a Value in JSON format. * \param root Value to serialize. - * \return String containing the JSON document that represent the root value. + * \return String containing the JSON document that represents the root value. */ virtual std::string write( const Value &root ); @@ -102,6 +103,10 @@ namespace Json { std::string JSON_API valueToString( bool value ); std::string JSON_API valueToQuotedString( const char *value ); + /// \brief Output using the StyledWriter. + /// \see Json::operator>>() + std::ostream& operator<<( std::ostream&, const Value &root ); + } // namespace Json diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index fa3c5eb..91b07c6 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #if _MSC_VER >= 1400 // VC++ 8.0 #pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. @@ -52,6 +54,23 @@ Reader::parse( const std::string &document, return parse( begin, end, root, collectComments ); } +bool +Reader::parse( std::istream& sin, + Value &root, + bool collectComments ) +{ + //std::istream_iterator begin(sin); + //std::istream_iterator end; + // Those would allow streamed input from a file, if parse() were a + // template function. + + // Since std::string is reference-counted, this at least does not + // create an extra copy. + std::string doc; + std::getline(sin, doc, (char)EOF); + return parse( doc, root, collectComments ); +} + bool Reader::parse( const char *beginDoc, const char *endDoc, Value &root, @@ -718,4 +737,14 @@ Reader::getFormatedErrorMessages() const } +std::istream& operator>>( std::istream &sin, Value &root ) +{ + Json::Reader reader; + bool ok = reader.parse(sin, root, true); + //JSON_ASSERT( ok ); + if (!ok) throw std::runtime_error(reader.getFormatedErrorMessages()); + return sin; +} + + } // namespace Json diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 063a66f..5f000df 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #if _MSC_VER >= 1400 // VC++ 8.0 #pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. @@ -475,5 +476,12 @@ StyledWriter::normalizeEOL( const std::string &text ) return normalized; } +std::ostream& operator<<( std::ostream &sout, const Value &root ) +{ + Json::StyledWriter writer; + sout << writer.write(root); + return sout; +} + } // namespace Json