Skip to content

Commit 56a1d6c

Browse files
committed
Added istream/ostream funcs/operators
1 parent 2370789 commit 56a1d6c

5 files changed

Lines changed: 82 additions & 1 deletion

File tree

doc/jsoncpp.dox

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ root["indent"]["use_space"] = getCurrentIndentUseSpace();
6565
Json::StyledWriter writer;
6666
// Make a new JSON document for the configuration. Preserve original comments.
6767
std::string outputConfig = writer.write( root );
68+
69+
// You can also use streams. This will put the contents of any JSON
70+
// stream at a particular sub-value, if you'd like.
71+
std::cin >> root["subtree"];
72+
73+
// And you can write to a stream, using the StyledWriter automatically.
74+
std::cout << root;
6875
\endcode
6976

7077
\section _plinks Build instructions

include/json/reader.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# include <deque>
77
# include <stack>
88
# include <string>
9+
# include <istream>
910

1011
namespace Json {
1112

@@ -47,6 +48,12 @@ namespace Json {
4748
Value &root,
4849
bool collectComments = true );
4950

51+
/// \brief Parse from input stream.
52+
/// \see Json::operator>>(std::istream&, Json::Value&).
53+
bool parse( std::istream&,
54+
Value &root,
55+
bool collectComments = true );
56+
5057
/** \brief Returns a user friendly string that list errors in the parsed document.
5158
* \return Formatted error message with the list of errors with their location in
5259
* the parsed document. An empty string is returned if no error occurred
@@ -144,6 +151,31 @@ namespace Json {
144151
bool collectComments_;
145152
};
146153

154+
/** \brief Read from 'sin' into 'root'.
155+
156+
Always keep comments from the input JSON.
157+
158+
This can be used to read a file into a particular sub-object.
159+
For example:
160+
\code
161+
Json::Value root;
162+
cin >> root["dir"]["file"];
163+
cout << root;
164+
\endcode
165+
Result:
166+
\verbatim
167+
{
168+
"dir": {
169+
"file": {
170+
// The input stream JSON would be nested here.
171+
}
172+
}
173+
}
174+
\endverbatim
175+
\throw std::exception on parse error.
176+
\see Json::operator<<()
177+
*/
178+
std::istream& operator>>( std::istream&, Value& );
147179

148180
} // namespace Json
149181

include/json/writer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# include "value.h"
55
# include <vector>
66
# include <string>
7+
# include <ostream>
78

89
namespace Json {
910

@@ -68,7 +69,7 @@ namespace Json {
6869
public: // overridden from Writer
6970
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
7071
* \param root Value to serialize.
71-
* \return String containing the JSON document that represent the root value.
72+
* \return String containing the JSON document that represents the root value.
7273
*/
7374
virtual std::string write( const Value &root );
7475

@@ -102,6 +103,10 @@ namespace Json {
102103
std::string JSON_API valueToString( bool value );
103104
std::string JSON_API valueToQuotedString( const char *value );
104105

106+
/// \brief Output using the StyledWriter.
107+
/// \see Json::operator>>()
108+
std::ostream& operator<<( std::ostream&, const Value &root );
109+
105110
} // namespace Json
106111

107112

src/lib_json/json_reader.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <utility>
44
#include <stdio.h>
55
#include <assert.h>
6+
#include <istream>
7+
#include <stdexcept>
68

79
#if _MSC_VER >= 1400 // VC++ 8.0
810
#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated.
@@ -52,6 +54,23 @@ Reader::parse( const std::string &document,
5254
return parse( begin, end, root, collectComments );
5355
}
5456

57+
bool
58+
Reader::parse( std::istream& sin,
59+
Value &root,
60+
bool collectComments )
61+
{
62+
//std::istream_iterator<char> begin(sin);
63+
//std::istream_iterator<char> end;
64+
// Those would allow streamed input from a file, if parse() were a
65+
// template function.
66+
67+
// Since std::string is reference-counted, this at least does not
68+
// create an extra copy.
69+
std::string doc;
70+
std::getline(sin, doc, (char)EOF);
71+
return parse( doc, root, collectComments );
72+
}
73+
5574
bool
5675
Reader::parse( const char *beginDoc, const char *endDoc,
5776
Value &root,
@@ -718,4 +737,14 @@ Reader::getFormatedErrorMessages() const
718737
}
719738

720739

740+
std::istream& operator>>( std::istream &sin, Value &root )
741+
{
742+
Json::Reader reader;
743+
bool ok = reader.parse(sin, root, true);
744+
//JSON_ASSERT( ok );
745+
if (!ok) throw std::runtime_error(reader.getFormatedErrorMessages());
746+
return sin;
747+
}
748+
749+
721750
} // namespace Json

src/lib_json/json_writer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <utility>
33
#include <assert.h>
44
#include <stdio.h>
5+
#include <ostream>
56

67
#if _MSC_VER >= 1400 // VC++ 8.0
78
#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated.
@@ -475,5 +476,12 @@ StyledWriter::normalizeEOL( const std::string &text )
475476
return normalized;
476477
}
477478

479+
std::ostream& operator<<( std::ostream &sout, const Value &root )
480+
{
481+
Json::StyledWriter writer;
482+
sout << writer.write(root);
483+
return sout;
484+
}
485+
478486

479487
} // namespace Json

0 commit comments

Comments
 (0)