Class Poco::JSON::Parser
Author: Applied Informatics Software Engineering GmbH and Contributors

# Poco::JSON

# class Parser

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

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

## Destructor

### ~Parser

virtual ~Parser();

Destroys JSON 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.

### setAllowNullByte

void setAllowNullByte(
bool nullByte
);

Allow or disallow null byte in strings.

By default, null byte is allowed.

### setDepth

void setDepth(
std::size_t depth
);

Sets the allowed JSON depth.

### setHandler

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

Set the Handler.