Added istream/ostream funcs/operators

This commit is contained in:
Christopher Dunn
2007-03-23 09:57:01 +00:00
parent 2370789d67
commit 56a1d6cbf5
5 changed files with 82 additions and 1 deletions

View File

@@ -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 @@ 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,
@@ -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

View File

@@ -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 @@ 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