millijson
Lightweight JSON parser for C++
Loading...
Searching...
No Matches
millijson Namespace Reference

A lightweight header-only JSON parser. More...

Classes

class  Array
 JSON array. More...
 
class  Base
 Virtual base class for all JSON types. More...
 
class  Boolean
 JSON boolean. More...
 
struct  DefaultProvisioner
 Default methods to provision representations of JSON types. More...
 
struct  FileReadOptions
 Options for parse_file() and validate_file(). More...
 
class  Nothing
 JSON null. More...
 
class  Number
 JSON number. More...
 
class  NumberAsString
 JSON number as a string. More...
 
class  Object
 JSON object. More...
 
struct  ParseOptions
 Options for parse(). More...
 
class  String
 JSON string. More...
 

Enumerations

enum  Type {
  NUMBER , NUMBER_AS_STRING , STRING , BOOLEAN ,
  NOTHING , ARRAY , OBJECT
}
 

Functions

template<class Provisioner_ = DefaultProvisioner, class Input_ >
std::shared_ptr< typename DefaultProvisioner::Baseparse (Input_ &input, const ParseOptions &options)
 
template<class Input_ >
Type validate (Input_ &input, const ParseOptions &options)
 
template<class Provisioner_ = DefaultProvisioner>
std::shared_ptr< typename Provisioner_::Base > parse_string (const char *ptr, std::size_t len, const ParseOptions &options)
 
Type validate_string (const char *ptr, std::size_t len, const ParseOptions &options)
 
template<class Provisioner_ = DefaultProvisioner>
std::shared_ptr< Baseparse_file (const char *path, const FileReadOptions &options)
 
Type validate_file (const char *path, const FileReadOptions &options)
 

Detailed Description

A lightweight header-only JSON parser.

Enumeration Type Documentation

◆ Type

All known JSON types. NUMBER_AS_STRING indicates a JSON number that is represented as its input string.

Function Documentation

◆ parse()

template<class Provisioner_ = DefaultProvisioner, class Input_ >
std::shared_ptr< typename DefaultProvisioner::Base > millijson::parse ( Input_ & input,
const ParseOptions & options )

Parse a stream of input bytes for a JSON value, based on the specification at https://json.org.

No consideration is given to floating-point overflow for arbitrarily large numbers. On systems that support IEEE754 arithmetic, overflow will manifest as infinities in Number, otherwise it is undefined behavior. If overflow is undesirable, consider setting ParseOptions::number_as_string to manually control conversion after parsing.

Template Parameters
Provisioner_Class that provide methods for provisioning each JSON type, see DefaultProvisioner for an example. All types should be subclasses of the provisioner's base class (which may but is not required to be Base).
Input_Class of the source of input bytes. This should satisfy the byteme::PerByteInterface interface with the following methods:
  • char get() const, which extracts a char from the input source without advancing the position on the byte stream.
  • bool valid() const, to determine whether an input char can be get() from the input.
  • bool advance(), to advance the input stream and return valid() at the new position.
  • unsigned long long position() const, for the current position relative to the start of the byte stream.
Parameters
inputA source of input bytes, usually from a JSON-formatted file or string.
optionsFurther options for parsing.
Returns
A pointer to a JSON value.

◆ parse_file()

template<class Provisioner_ = DefaultProvisioner>
std::shared_ptr< Base > millijson::parse_file ( const char * path,
const FileReadOptions & options )

Parse a file containing a JSON value using parse().

Parameters
[in]pathPointer to an array containing a path to a JSON file.
optionsFurther options.
Returns
A pointer to a JSON value.

◆ parse_string()

template<class Provisioner_ = DefaultProvisioner>
std::shared_ptr< typename Provisioner_::Base > millijson::parse_string ( const char * ptr,
std::size_t len,
const ParseOptions & options )
inline

Parse a string containing a JSON value using parse().

Template Parameters
Provisioner_Class that provide methods for provisioning each JSON type, see DefaultProvisioner for an example. All types should be subclasses of the provisioner's base class (which may but is not required to be Base).
Parameters
[in]ptrPointer to an array containing a JSON string.
lenLength of the array.
optionsFurther options for parsing.
Returns
A pointer to a JSON value.

◆ validate()

template<class Input_ >
Type millijson::validate ( Input_ & input,
const ParseOptions & options )

Check that a string contains a valid JSON value. This follows the same logic as parse() but is more memory-efficient.

Template Parameters
Input_Any class that supplies input characters, see parse() for details.
Parameters
inputA source of input bytes, usually from a JSON-formatted file or string.
optionsFurther options for parsing.
Returns
The type of the JSON variable stored in input. If the JSON string is invalid, an error is raised.

◆ validate_file()

Type millijson::validate_file ( const char * path,
const FileReadOptions & options )
inline

Check that a file contains a valid JSON value using validate().

Parameters
[in]pathPointer to an array containing a path to a JSON file.
optionsFurther options.
Returns
The type of the JSON variable stored in the file. If the JSON file is invalid, an error is raised.

◆ validate_string()

Type millijson::validate_string ( const char * ptr,
std::size_t len,
const ParseOptions & options )
inline

Check that a string contains a valid JSON value using validate().

Parameters
[in]ptrPointer to an array containing a JSON string.
lenLength of the array.
optionsFurther options for parsing.
Returns
The type of the JSON variable stored in the string. If the JSON string is invalid, an error is raised.