|
Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
|
JSON implementation of the IParser interface. More...
#include <JsonParser.h>


Public Member Functions | |
| std::shared_ptr< DBNode > | parse (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< DBNode > | readFromFile (const std::string &filename) const override |
| Reads a JSON file and parses it into a DBNode tree. | |
| std::shared_ptr< DBNode > | parseObject (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< DBNode > | parseArray (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. | |
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.
|
overridevirtual |
Parses a JSON document from an in-memory string.
The top-level JSON value is expected to be an object ({ ... }).
| input | JSON text buffer. |
| std::runtime_error | on malformed JSON or conversion errors. |
Implements IParser.
Definition at line 3 of file JsonParser.cpp.
| 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 ']'.
| input | JSON string. |
| index | Reference to the current position; advanced as parsing proceeds. |
| std::runtime_error | on invalid syntax. |
Definition at line 57 of file JsonParser.cpp.
| 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:
| input | JSON string. |
| index | Reference to the current position; advanced past the end of the numeric token. |
| std::runtime_error | if the token cannot be converted to double. |
Definition at line 74 of file JsonParser.cpp.
| 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:
| input | JSON string. |
| index | Reference to the current position in the string; advanced past the closing '}' on success. |
| std::runtime_error | on invalid syntax. |
Definition at line 9 of file JsonParser.cpp.
| 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.
| input | JSON string. |
| index | Reference to the current position; on return, points just after the closing ". |
| std::runtime_error | if the expected quotes are missing. |
Definition at line 31 of file JsonParser.cpp.
| DBNode::Value JSONParser::parseValue | ( | const std::string & | input, |
| size_t & | index | ||
| ) | const |
Parses any JSON value at the current position.
Supported value types:
The result is converted into a DBNode::Value:
| input | JSON string. |
| index | Reference to the current position; advanced past the parsed value on success. |
| std::runtime_error | if the value is invalid or unsupported. |
Definition at line 43 of file JsonParser.cpp.
|
overridevirtual |
Reads a JSON file and parses it into a DBNode tree.
The entire file content is loaded into memory and passed to parse().
| filename | Source file name. |
| std::runtime_error | if the file cannot be opened or parsing fails. |
Implements IParser.
Definition at line 113 of file JsonParser.cpp.
| 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.
| input | JSON string. |
| index | Reference to the current position; updated in place. |
Definition at line 99 of file JsonParser.cpp.
|
overridevirtual |
Serializes a DBNode tree to a JSON file.
Internally uses DBNode::printJSONToStream() to produce a JSON-like representation and writes it to filename.
| filename | Target file name. |
| root | Root DBNode to serialize. |
| std::runtime_error | if the file cannot be opened or a write error occurs. |
Implements IParser.
Definition at line 103 of file JsonParser.cpp.