# 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");

## Member Summary

Member Functions: asVar, getAllowComments, getAllowNullByte, getDepth, getHandler, parse, reset, result, setAllowComments, setAllowNullByte, setDepth, setHandler

## Types

### CharIntType

typedef CharTraits::int_type CharIntType;

### CharTraits

typedef std::char_traits < char > CharTraits;

## Enumerations

### Actions

CB = - 10

CE = - 11

FA = - 12

TR = - 13

NU = - 14

DE = - 15

DF = - 16

SB = - 17

MX = - 18

ZX = - 19

IX = - 20

EX = - 21

UC = - 22

### Classes

C_SPACE

C_WHITE

C_LCURB

C_RCURB

C_LSQRB

C_RSQRB

C_COLON

C_COMMA

C_QUOTE

C_BACKS

C_SLASH

C_PLUS

C_MINUS

C_POINT

C_ZERO

C_DIGIT

C_LOW_A

C_LOW_B

C_LOW_C

C_LOW_D

C_LOW_E

C_LOW_F

C_LOW_L

C_LOW_N

C_LOW_R

C_LOW_S

C_LOW_T

C_LOW_U

C_ABCDF

C_E

C_ETC

C_STAR

NR_CLASSES

### JSONType

JSON_T_NONE = 0

JSON_T_INTEGER

JSON_T_FLOAT

JSON_T_NULL

JSON_T_TRUE

JSON_T_FALSE

JSON_T_STRING

JSON_T_MAX

### Modes

Modes that can be pushed on the _pStack.

MODE_ARRAY = 1

MODE_DONE = 2

MODE_KEY = 3

MODE_OBJECT = 4

### States

State codes

GO

OK

OB

KE

CO

VA

AR

ST

EC

U1

U2

U3

U4

MI

ZE

IT

FR

E1

E2

E3

T1

T2

T3

F1

F2

F3

F4

N1

N2

N3

C1

C2

C3

FX

D1

D2

NR_STATES

## 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.

## Variables

### JSON_PARSER_STACK_SIZE

static const std::size_t JSON_PARSER_STACK_SIZE = 128;

### JSON_PARSE_BUFFER_SIZE

static const std::size_t JSON_PARSE_BUFFER_SIZE = 4096;

### JSON_UNLIMITED_DEPTH

static const int JSON_UNLIMITED_DEPTH = - 1;