mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2024-12-12 10:03:51 +01:00
Added istream/ostream funcs/operators
This commit is contained in:
parent
2370789d67
commit
56a1d6cbf5
@ -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
|
||||
|
@ -6,6 +6,7 @@
|
||||
# include <deque>
|
||||
# include <stack>
|
||||
# include <string>
|
||||
# include <istream>
|
||||
|
||||
namespace Json {
|
||||
|
||||
@ -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
|
||||
@ -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
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
# include "value.h"
|
||||
# include <vector>
|
||||
# include <string>
|
||||
# include <ostream>
|
||||
|
||||
namespace Json {
|
||||
|
||||
@ -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 );
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user