Skip to content

Commit

Permalink
Added istream/ostream funcs/operators
Browse files Browse the repository at this point in the history
  • Loading branch information
cdunn2001 committed Mar 23, 2007
1 parent 2370789 commit 56a1d6c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
7 changes: 7 additions & 0 deletions doc/jsoncpp.dox
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# include <deque>
# include <stack>
# include <string>
# include <istream>

namespace Json {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion include/json/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# include "value.h"
# include <vector>
# include <string>
# include <ostream>

namespace Json {

Expand Down Expand Up @@ -68,7 +69,7 @@ namespace Json {
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 );

Expand Down Expand Up @@ -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


Expand Down
29 changes: 29 additions & 0 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<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,
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit 56a1d6c

Please sign in to comment.