|
3 | 3 | #include <utility> |
4 | 4 | #include <stdio.h> |
5 | 5 | #include <assert.h> |
| 6 | +#include <istream> |
| 7 | +#include <stdexcept> |
6 | 8 |
|
7 | 9 | #if _MSC_VER >= 1400 // VC++ 8.0 |
8 | 10 | #pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. |
@@ -52,6 +54,23 @@ Reader::parse( const std::string &document, |
52 | 54 | return parse( begin, end, root, collectComments ); |
53 | 55 | } |
54 | 56 |
|
| 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 | + |
55 | 74 | bool |
56 | 75 | Reader::parse( const char *beginDoc, const char *endDoc, |
57 | 76 | Value &root, |
@@ -718,4 +737,14 @@ Reader::getFormatedErrorMessages() const |
718 | 737 | } |
719 | 738 |
|
720 | 739 |
|
| 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 | + |
721 | 750 | } // namespace Json |
0 commit comments