Skip to content

Latest commit

 

History

History
157 lines (125 loc) · 4.69 KB

json_reader.md

File metadata and controls

157 lines (125 loc) · 4.69 KB

jsoncons::json_reader

typedef basic_json_reader<char> json_reader

A json_reader can read a sequence of JSON texts from a stream.

json_reader is noncopyable and nonmoveable.

Header

#include <jsoncons/json_reader.hpp>

Constructors

json_reader(std::istream& is,
            json_input_handler& handler,
            parse_error_handler& err_handler)

Constructs a json_reader that is associated with an input stream is of JSON text, a json_input_handler that receives JSON events, and the specified parse_error_handler. You must ensure that the input stream, input handler, and error handler exist as long as does json_reader, as json_reader holds pointers to but does not own these objects.

json_reader(std::istream& is,
            json_input_handler& handler)

Constructs a json_reader that is associated with an input stream is of JSON text, a json_input_handler that receives JSON events, and a default_parse_error_handler. You must ensure that the input stream and input handler exist as long as does json_reader, as json_reader holds pointers to does not own these objects.

Member functions

bool eof() const

Returns true when there are no more JSON texts to be read from the stream, false otherwise

void read()

Reads the next JSON text from the stream and reports JSON events to a json_input_handler, such as a json_decoder. Throws if there are any unconsumed non-whitespace characters left in the input. Throws parse_error if parsing fails.

void read(std::error_code& ec)

Reads the next JSON text from the stream and reports JSON events to a json_input_handler, such as a json_decoder. The error code ec is set if parsing fails or if there are any unconsumed non-whitespace characters left in the input.

void read_next()

Reads the next JSON text from the stream and reports JSON events to a json_input_handler, such as a json_decoder. Throws parse_error if parsing fails.

void read_next(std::error_code& ec)

Reads the next JSON text from the stream and reports JSON events to a json_input_handler, such as a json_decoder. The error code ec is set if parsing fails.

void check_done()

Throws if there are any unconsumed non-whitespace characters in the input. Throws parse_error if there are any unconsumed non-whitespace characters left in the input.

void check_done(std::error_code& ec)

Throws if there are any unconsumed non-whitespace characters in the input. The error code ec is set if there are any unconsumed non-whitespace characters left in the input.

size_t buffer_length() const

void buffer_length(size_t length)

size_t max_nesting_depth() const

By default jsoncons can read a JSON text of arbitrarily large depth.

void max_nesting_depth(size_t depth)

size_t line_number() const

size_t column_number() const

Examples

Parsing JSON text with exceptions

std::string input = R"({"field1"{}})";    
std::istringstream is(input);

json_decoder<json> decoder;
json_reader reader(is,decoder);

try
{
    reader.read();
    json j = decoder.get_result();
}
catch (const parse_error& e)
{
    std::cout << e.what() << std::endl;
}

Output:

Expected name separator ':' at line 1 and column 10

Parsing JSON text with error codes

std::string input = R"({"field1":ru})";    
std::istringstream is(input);

json_decoder<json> decoder;
json_reader reader(is,decoder);

std::error_code ec;
reader.read(ec);

if (!ec)
{
    json j = decoder.get_result();   
}
else
{
    std::cerr << ec.message() 
              << " at line " << reader.line_number() 
              << " and column " << reader.column_number() << std::endl;
}

Output:

Expected value at line 1 and column 11

Reading a sequence of JSON texts from a stream

jsoncons supports reading a sequence of JSON texts, such as shown below (json-texts.json):

{"a":1,"b":2,"c":3}
{"a":4,"b":5,"c":6}
{"a":7,"b":8,"c":9}

This is the code that reads them:

std::ifstream is("json-texts.json");
if (!is.is_open())
{
    throw std::runtime_error("Cannot open file");
}

json_decoder<json> decoder;
json_reader reader(is,decoder);

while (!reader.eof())
{
    reader.read_next();
    if (!reader.eof())
    {
        json val = decoder.get_result();
        std::cout << val << std::endl;
    }
}

Output:

{"a":1,"b":2,"c":3}
{"a":4,"b":5,"c":6}
{"a":7,"b":8,"c":9}