JSON for Modern C++  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser Class Reference

syntax analysis

Collaboration diagram for nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser:

Public Member Functions

 parser (const string_t &s)
 constructor for strings More...
 
 parser (std::istream &_is)
 a parser reading from an input stream More...
 
basic_json parse ()
 public parser interface More...
 

Private Member Functions

basic_json parse_internal ()
 the actual parser More...
 
lexer::token_type get_token ()
 get next token from lexer More...
 
void expect (typename lexer::token_type t) const
 

Private Attributes

lexer::token_type last_token = lexer::token_type::uninitialized
 the type of the last read token More...
 
lexer m_lexer
 the lexer More...
 

Detailed Description

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
class nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser

Definition at line 4468 of file json.hpp.

Constructor & Destructor Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::parser ( const string_t s)
inline

Definition at line 4472 of file json.hpp.

4472  : m_lexer(s)
4473  {
4474  // read first token
4475  get_token();
4476  }
lexer m_lexer
the lexer
Definition: json.hpp:4669
lexer::token_type get_token()
get next token from lexer
Definition: json.hpp:4647
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::parser ( std::istream &  _is)
inline

Definition at line 4479 of file json.hpp.

4479  : m_lexer(&_is)
4480  {
4481  // read first token
4482  get_token();
4483  }
lexer m_lexer
the lexer
Definition: json.hpp:4669
lexer::token_type get_token()
get next token from lexer
Definition: json.hpp:4647

Member Function Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::expect ( typename lexer::token_type  t) const
inlineprivate

Definition at line 4653 of file json.hpp.

4654  {
4655  if (t != last_token)
4656  {
4657  std::string error_msg = "parse error - unexpected \'";
4658  error_msg += m_lexer.get_token();
4659  error_msg += "\' (" + lexer::token_type_name(last_token);
4660  error_msg += "); expected " + lexer::token_type_name(t);
4661  throw std::invalid_argument(error_msg);
4662  }
4663  }
lexer m_lexer
the lexer
Definition: json.hpp:4669
string_t get_token() const noexcept
return string representation of last read token
Definition: json.hpp:4288
static std::string token_type_name(token_type t) noexcept
return name of values of type token_type
Definition: json.hpp:3454
lexer::token_type last_token
the type of the last read token
Definition: json.hpp:4667
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
lexer::token_type nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::get_token ( )
inlineprivate

Definition at line 4647 of file json.hpp.

4648  {
4649  last_token = m_lexer.scan();
4650  return last_token;
4651  }
lexer m_lexer
the lexer
Definition: json.hpp:4669
token_type scan() noexcept
Definition: json.hpp:3499
lexer::token_type last_token
the type of the last read token
Definition: json.hpp:4667
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::parse ( )
inline

Definition at line 4486 of file json.hpp.

Referenced by nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parse().

4487  {
4488  basic_json result = parse_internal();
4489 
4491 
4492  return result;
4493  }
void expect(typename lexer::token_type t) const
Definition: json.hpp:4653
basic_json parse_internal()
the actual parser
Definition: json.hpp:4497
indicating the end of the input buffer
basic_json() noexcept=default
create a null object (implicitly)

Here is the caller graph for this function:

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::parse_internal ( )
inlineprivate

Definition at line 4497 of file json.hpp.

References nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::push_back().

4498  {
4499  switch (last_token)
4500  {
4502  {
4503  // explicitly set result to object to cope with {}
4504  basic_json result(value_t::object);
4505 
4506  // read next token
4507  get_token();
4508 
4509  // closing } -> we are done
4511  {
4512  get_token();
4513  return result;
4514  }
4515 
4516  // otherwise: parse key-value pairs
4517  do
4518  {
4519  // ugly, but could be fixed with loop reorganization
4521  {
4522  get_token();
4523  }
4524 
4525  // store key
4527  const auto key = m_lexer.get_string();
4528 
4529  // parse separator (:)
4530  get_token();
4532 
4533  // parse value
4534  get_token();
4535  result[key] = parse_internal();
4536  }
4538 
4539  // closing }
4541  get_token();
4542 
4543  return result;
4544  }
4545 
4547  {
4548  // explicitly set result to object to cope with []
4549  basic_json result(value_t::array);
4550 
4551  // read next token
4552  get_token();
4553 
4554  // closing ] -> we are done
4556  {
4557  get_token();
4558  return result;
4559  }
4560 
4561  // otherwise: parse values
4562  do
4563  {
4564  // ugly, but could be fixed with loop reorganization
4566  {
4567  get_token();
4568  }
4569 
4570  // parse value
4571  result.push_back(parse_internal());
4572  }
4574 
4575  // closing ]
4577  get_token();
4578 
4579  return result;
4580  }
4581 
4583  {
4584  get_token();
4585  return basic_json(nullptr);
4586  }
4587 
4589  {
4590  const auto s = m_lexer.get_string();
4591  get_token();
4592  return basic_json(s);
4593  }
4594 
4596  {
4597  get_token();
4598  return basic_json(true);
4599  }
4600 
4602  {
4603  get_token();
4604  return basic_json(false);
4605  }
4606 
4608  {
4609  auto float_val = m_lexer.get_number();
4610 
4611  // NAN is returned if token could not be translated
4612  // completely
4613  if (std::isnan(float_val))
4614  {
4615  throw std::invalid_argument(std::string("parse error - ") +
4616  m_lexer.get_token() + " is not a number");
4617  }
4618 
4619  get_token();
4620 
4621  // check if conversion loses precision
4622  const auto int_val = static_cast<number_integer_t>(float_val);
4623  if (approx(float_val, static_cast<number_float_t>(int_val)))
4624  {
4625  // we basic_json not lose precision -> return int
4626  return basic_json(int_val);
4627  }
4628  else
4629  {
4630  // we would lose precision -> returnfloat
4631  return basic_json(float_val);
4632  }
4633  }
4634 
4635  default:
4636  {
4637  std::string error_msg = "parse error - unexpected \'";
4638  error_msg += m_lexer.get_token();
4639  error_msg += "\' (";
4640  error_msg += lexer::token_type_name(last_token) + ")";
4641  throw std::invalid_argument(error_msg);
4642  }
4643  }
4644  }
the character for array begin &quot;[&quot;
number_float_t get_number() const
return number value for number tokens
Definition: json.hpp:4436
void expect(typename lexer::token_type t) const
Definition: json.hpp:4653
static bool approx(const T a, const T b)
&quot;equality&quot; comparison for floating point numbers
Definition: json.hpp:2185
lexer m_lexer
the lexer
Definition: json.hpp:4669
lexer::token_type get_token()
get next token from lexer
Definition: json.hpp:4647
basic_json parse_internal()
the actual parser
Definition: json.hpp:4497
the character for object end &quot;}&quot;
string_t get_token() const noexcept
return string representation of last read token
Definition: json.hpp:4288
string_t get_string() const
return string value for string tokens
Definition: json.hpp:4315
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:198
a string - use get_string() for actual value
array (ordered collection of values)
static std::string token_type_name(token_type t) noexcept
return name of values of type token_type
Definition: json.hpp:3454
object (unordered set of name/value pairs)
basic_json() noexcept=default
create a null object (implicitly)
the character for array end &quot;]&quot;
a number - use get_number() for actual value
the character for object begin &quot;{&quot;
lexer::token_type last_token
the type of the last read token
Definition: json.hpp:4667

Here is the call graph for this function:

Member Data Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
lexer::token_type nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::last_token = lexer::token_type::uninitialized
private

Definition at line 4667 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = int64_t, class NumberFloatType = double, template< typename U > class Allocator = std::allocator>
lexer nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberFloatType, Allocator >::parser::m_lexer
private

Definition at line 4669 of file json.hpp.


The documentation for this class was generated from the following file: