Library: JSON
Package: JSON
Header: Poco/JSON/Parser.h
Description
A parser for reading RFC 4627 compliant JSON from strings or streams.
Simple usage example:
std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }"; Parser parser; Var result = parser.parse(json); // ... use result (see next example) parser.reset(); std::ostringstream ostr; PrintHandler::Ptr pHandler = new PrintHandler(ostr); parser.setHandler(pHandler); parser.parse(json); // ostr.str() == json
The result of parsing a valid JSON document will be either an Object or an Array. Therefore the result of parse() is a Poco::Dynamic::Var containing a Poco::SharedPtr to an Object or Array instance.
Example:
std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }"; Parser parser; Var result = parser.parse(json); Object::Ptr pObject = result.extract<Object::Ptr>(); std::string name = pObject->getValue<std::string>("name"); Array::Ptr pChildren = pObject->getArray("children");
Inheritance
Direct Base Classes: ParserImpl
All Base Classes: ParserImpl
Member Summary
Member Functions: asVar, getAllowComments, getAllowNullByte, getDepth, getHandler, parse, reset, result, setAllowComments, setAllowNullByte, setDepth, setHandler
Inherited Functions: asVarImpl, getAllowCommentsImpl, getAllowNullByteImpl, getDepthImpl, getHandlerImpl, parseImpl, resetImpl, resultImpl, setAllowCommentsImpl, setAllowNullByteImpl, setDepthImpl, setHandlerImpl
Constructors
Parser
Parser(
const Handler::Ptr & pHandler = new ParseHandler
);
Destructor
~Parser
virtual ~Parser();
Member Functions
asVar
Dynamic::Var asVar() const;
Returns the result of parsing;
getAllowComments
bool getAllowComments() const;
Returns true if comments are allowed, false otherwise.
By default, comments are not allowed.
getAllowNullByte
bool getAllowNullByte() const;
Returns true if null byte is allowed, false otherwise.
By default, null bytes are allowed.
getDepth
std::size_t getDepth() const;
Returns the allowed JSON depth.
getHandler
const Handler::Ptr & getHandler();
Returns the Handler.
parse
Dynamic::Var parse(
const std::string & json
);
Parses JSON from a string.
parse
Dynamic::Var parse(
std::istream & in
);
Parses JSON from an input stream.
reset
void reset();
Resets the parser.
result
Dynamic::Var result() const;
Returns the result of parsing as Dynamic::Var;
setAllowComments
void setAllowComments(
bool comments
);
Allow or disallow comments. By default, comments are not allowed.
If set to true, comments will be filtered out of the input data before passing the JSON on to the parser. This will impact performance, especially when reading from a std::istream.
setAllowNullByte
void setAllowNullByte(
bool nullByte
);
Allow or disallow null byte in strings.
By default, null byte is allowed (true).
If set to false, an additional check for "\u0000" will be performed before passing the JSON on to the parser. This will impact performance, especially when reading from a std::istream.
setDepth
void setDepth(
std::size_t depth
);
Sets the allowed JSON depth.
Default maximum depth is 128. Setting this value too high may result in a stack overflow when parsing a (malicious) JSON document.
setHandler
void setHandler(
const Handler::Ptr & pHandler
);
Set the Handler.