Added istream/ostream funcs/operators
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 @@
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 <deque>
# include <stack>
# include <string>
+# include <istream>
namespace Json {
@@ -47,6 +48,12 @@
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 @@
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 <vector>
# include <string>
+# include <ostream>
namespace Json {
@@ -68,7 +69,7 @@
public: // overridden from Writer
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> 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 @@
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 <utility>
#include <stdio.h>
#include <assert.h>
+#include <istream>
+#include <stdexcept>
#if _MSC_VER >= 1400 // VC++ 8.0
#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated.
@@ -52,6 +54,23 @@
return parse( begin, end, root, collectComments );
}
+bool
+Reader::parse( std::istream& sin,
+ Value &root,
+ bool collectComments )
+{
+ //std::istream_iterator<char> begin(sin);
+ //std::istream_iterator<char> 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 @@
}
+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 <utility>
#include <assert.h>
#include <stdio.h>
+#include <ostream>
#if _MSC_VER >= 1400 // VC++ 8.0
#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated.
@@ -475,5 +476,12 @@
return normalized;
}
+std::ostream& operator<<( std::ostream &sout, const Value &root )
+{
+ Json::StyledWriter writer;
+ sout << writer.write(root);
+ return sout;
+}
+
} // namespace Json