Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
JSONParser Class Reference

JSON implementation of the IParser interface. More...

#include <JsonParser.h>

Inheritance diagram for JSONParser:
Collaboration diagram for JSONParser:

Public Member Functions

std::shared_ptr< DBNodeparse (const std::string &input) const override
 Parses a JSON document from an in-memory string.
 
void writeToFile (const std::string &filename, const std::shared_ptr< DBNode > &root) const override
 Serializes a DBNode tree to a JSON file.
 
std::shared_ptr< DBNodereadFromFile (const std::string &filename) const override
 Reads a JSON file and parses it into a DBNode tree.
 
std::shared_ptr< DBNodeparseObject (const std::string &input, size_t &index) const
 Parses a JSON object starting at the given index.
 
std::string parseString (const std::string &input, size_t &index) const
 Parses a JSON string literal.
 
DBNode::Value parseValue (const std::string &input, size_t &index) const
 Parses any JSON value at the current position.
 
double parseNumber (const std::string &input, size_t &index) const
 Parses a JSON number (integer or floating point).
 
void skipWhitespace (const std::string &input, size_t &index) const
 Skips whitespace characters in the input string.
 
std::shared_ptr< DBNodeparseArray (const std::string &input, size_t &index) const
 Parses a JSON array starting at the current position.
 
- Public Member Functions inherited from IParser
virtual ~IParser ()=default
 Virtual destructor to allow proper cleanup via base pointer.
 

Detailed Description

JSON implementation of the IParser interface.

JSONParser can:

The parser is intentionally simple and does not support the full JSON standard (e.g. no null literal, no escape sequences in strings).

Definition at line 42 of file JsonParser.h.

Member Function Documentation

◆ parse()

std::shared_ptr< DBNode > JSONParser::parse ( const std::string &  input) const
overridevirtual

Parses a JSON document from an in-memory string.

The top-level JSON value is expected to be an object ({ ... }).

Parameters
inputJSON text buffer.
Returns
Shared pointer to the root DBNode representing the JSON object.
Exceptions
std::runtime_erroron malformed JSON or conversion errors.

Implements IParser.

Definition at line 3 of file JsonParser.cpp.

◆ parseArray()

std::shared_ptr< DBNode > JSONParser::parseArray ( const std::string &  input,
size_t &  index 
) const

Parses a JSON array starting at the current position.

Expects input[index] to point to '['. Elements are parsed using parseValue() and stored in a DBNode with numeric string keys: 0, 1, 2, ...

On return, index is advanced past the closing ']'.

Parameters
inputJSON string.
indexReference to the current position; advanced as parsing proceeds.
Returns
Shared pointer to the DBNode representing the array.
Exceptions
std::runtime_erroron invalid syntax.

Definition at line 57 of file JsonParser.cpp.

◆ parseNumber()

double JSONParser::parseNumber ( const std::string &  input,
size_t &  index 
) const

Parses a JSON number (integer or floating point).

Accepts the typical JSON number syntax:

  • optional minus sign,
  • digits,
  • optional decimal part,
  • optional exponent (e/E with optional sign).
Parameters
inputJSON string.
indexReference to the current position; advanced past the end of the numeric token.
Returns
Parsed double value.
Exceptions
std::runtime_errorif the token cannot be converted to double.

Definition at line 74 of file JsonParser.cpp.

◆ parseObject()

std::shared_ptr< DBNode > JSONParser::parseObject ( const std::string &  input,
size_t &  index 
) const

Parses a JSON object starting at the given index.

Expects input[index] to point to a '{', and consumes the entire object, returning a DBNode where each key becomes a child entry.

Example JSON: { "a": 1, "b": true }

becomes a DBNode with:

  • key "a" -> 1
  • key "b" -> true
Parameters
inputJSON string.
indexReference to the current position in the string; advanced past the closing '}' on success.
Returns
Shared pointer to the DBNode representing the object.
Exceptions
std::runtime_erroron invalid syntax.

Definition at line 9 of file JsonParser.cpp.

◆ parseString()

std::string JSONParser::parseString ( const std::string &  input,
size_t &  index 
) const

Parses a JSON string literal.

Expects input[index] to point to the opening double quote ("</tt>). Reads until the next unescaped <tt>". Escape sequences are not currently interpreted; characters are copied verbatim.

Parameters
inputJSON string.
indexReference to the current position; on return, points just after the closing ".
Returns
The extracted string value (without quotes).
Exceptions
std::runtime_errorif the expected quotes are missing.

Definition at line 31 of file JsonParser.cpp.

◆ parseValue()

DBNode::Value JSONParser::parseValue ( const std::string &  input,
size_t &  index 
) const

Parses any JSON value at the current position.

Supported value types:

  • object: { ... }
  • array: [ ... ]
  • string: "..."
  • number: integer or floating-point
  • boolean: true / false

The result is converted into a DBNode::Value:

  • objects and arrays become DBNode subtrees,
  • scalars become int/double/bool/string stored in the variant.
Parameters
inputJSON string.
indexReference to the current position; advanced past the parsed value on success.
Returns
Parsed DBNode::Value.
Exceptions
std::runtime_errorif the value is invalid or unsupported.

Definition at line 43 of file JsonParser.cpp.

◆ readFromFile()

std::shared_ptr< DBNode > JSONParser::readFromFile ( const std::string &  filename) const
overridevirtual

Reads a JSON file and parses it into a DBNode tree.

The entire file content is loaded into memory and passed to parse().

Parameters
filenameSource file name.
Returns
Shared pointer to the root DBNode representing the JSON document.
Exceptions
std::runtime_errorif the file cannot be opened or parsing fails.

Implements IParser.

Definition at line 113 of file JsonParser.cpp.

◆ skipWhitespace()

void JSONParser::skipWhitespace ( const std::string &  input,
size_t &  index 
) const

Skips whitespace characters in the input string.

Advances index while input[index] is a whitespace character as reported by std::isspace.

Parameters
inputJSON string.
indexReference to the current position; updated in place.

Definition at line 99 of file JsonParser.cpp.

◆ writeToFile()

void JSONParser::writeToFile ( const std::string &  filename,
const std::shared_ptr< DBNode > &  root 
) const
overridevirtual

Serializes a DBNode tree to a JSON file.

Internally uses DBNode::printJSONToStream() to produce a JSON-like representation and writes it to filename.

Parameters
filenameTarget file name.
rootRoot DBNode to serialize.
Exceptions
std::runtime_errorif the file cannot be opened or a write error occurs.

Implements IParser.

Definition at line 103 of file JsonParser.cpp.


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