class Poco::JSON::Parser

Overview

A parser for reading RFC 4627 compliant JSON from strings or streams. Moreā€¦

#include <Parser.h>

class Parser
{
public:
    // typedefs

    typedef std::char_traits<char> CharTraits;
    typedef CharTraits::int_type CharIntType;

    // enums

    enum Actions;
    enum Classes;
    enum JSONType;
    enum Modes;
    enum States;

    // classes

    template <typename IT>
    class Source;

    // fields

    static const std::size_t JSON_PARSE_BUFFER_SIZE;
    static const std::size_t JSON_PARSER_STACK_SIZE;
    static const int JSON_UNLIMITED_DEPTH;

    // construction

    Parser(
        const Handler::Ptr& pHandler = new ParseHandler,
        std::size_t bufSize = JSON_PARSE_BUFFER_SIZE
        );

    // methods

    void
    reset();

    void
    setAllowComments(bool comments);

    bool
    getAllowComments() const;

    void
    setAllowNullByte(bool nullByte);

    bool
    getAllowNullByte() const;

    void
    setDepth(std::size_t depth);

    std::size_t
    getDepth() const;

    Dynamic::Var
    parse(const std::string& json);

    Dynamic::Var
    parse(std::istream& in);

    void
    setHandler(const Handler::Ptr& pHandler);

    const Handler::Ptr&
    getHandler();

    Dynamic::Var
    asVar() const;

    Dynamic::Var
    result() const;
};

Detailed Documentation

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 object = result.extract<Object::Ptr>();
std::string name = object.getValue<std::string>("name");
Array::Ptr children = object.getArray("children");

Construction

Parser(
    const Handler::Ptr& pHandler = new ParseHandler,
    std::size_t bufSize = JSON_PARSE_BUFFER_SIZE
    )

Creates JSON Parser, using the given Handler and buffer size.

Methods

void
reset()

Resets the parser.

void
setAllowComments(bool comments)

Allow or disallow comments. By default, comments are not allowed.

bool
getAllowComments() const

Returns true if comments are allowed, false otherwise.

By default, comments are not allowed.

void
setAllowNullByte(bool nullByte)

Allow or disallow null byte in strings.

By default, null byte is allowed.

bool
getAllowNullByte() const

Returns true if null byte is allowed, false otherwise.

By default, null bytes are allowed.

void
setDepth(std::size_t depth)

Sets the allowed JSON depth.

std::size_t
getDepth() const

Returns the allowed JSON depth.

Dynamic::Var
parse(const std::string& json)

Parses JSON from a string.

Dynamic::Var
parse(std::istream& in)

Parses JSON from an input stream.

void
setHandler(const Handler::Ptr& pHandler)

Set the Handler.

const Handler::Ptr&
getHandler()

Returns the Handler.

Dynamic::Var
asVar() const

Returns the result of parsing;.

Dynamic::Var
result() const

Returns the result of parsing as Dynamic::Var;.