8#ifndef META_OCEAN_IO_JSON_PARSER_H
9#define META_OCEAN_IO_JSON_PARSER_H
62 using Array = std::vector<JSONValue>;
67 using ObjectMap = std::unordered_map<std::string, JSONValue>;
158 inline Type type()
const;
164 inline bool isNull()
const;
170 inline bool isBoolean()
const;
176 inline bool isNumber()
const;
182 inline bool isString()
const;
188 inline bool isArray()
const;
194 inline bool isObject()
const;
272 inline bool isValid()
const;
278 explicit inline operator bool()
const;
300 std::variant<std::monostate, std::nullptr_t, bool, double, std::string, UniqueArray, UniqueObjectMap>
data_;
319 SYMBOL_LEFT_BRACE = 0u,
353 explicit JSONScanner(
const std::shared_ptr<std::istream>& stream,
float* progress =
nullptr,
bool* cancel =
nullptr);
362 JSONScanner(
const std::string& filename,
const std::string& buffer,
float* progress =
nullptr,
bool* cancel =
nullptr);
371 JSONScanner(
const std::string& filename, std::string&& buffer,
float* progress =
nullptr,
bool* cancel =
nullptr);
390 static JSONValue parse(
const std::shared_ptr<std::istream>& stream,
const bool strict =
false, std::string* errorMessage =
nullptr);
400 static JSONValue parse(
const std::string& filename,
const std::string& buffer,
const bool strict =
false, std::string* errorMessage =
nullptr);
410 static JSONValue parse(
const std::string& filename, std::string&& buffer,
const bool strict =
false, std::string* errorMessage =
nullptr);
457 return type_ == TYPE_NULL;
462 return type_ == TYPE_BOOLEAN;
467 return type_ == TYPE_NUMBER;
472 return type_ == TYPE_STRING;
477 return type_ == TYPE_ARRAY;
482 return type_ == TYPE_OBJECT;
487 return type_ != TYPE_INVALID;
490inline JSONParser::JSONValue::operator bool()
const
This class implements a JSON scanner that extends the base Scanner class.
Definition JSONParser.h:310
JSONScanner(const std::shared_ptr< std::istream > &stream, float *progress=nullptr, bool *cancel=nullptr)
Creates a new JSON scanner using a stream as input.
JSONScanner(const std::string &filename, const std::string &buffer, float *progress=nullptr, bool *cancel=nullptr)
Creates a new JSON scanner using a file or a memory buffer as input.
JSONScanner(const std::string &filename, std::string &&buffer, float *progress=nullptr, bool *cancel=nullptr)
Creates a new JSON scanner using a file or a memory buffer as input.
void initialize()
Initializes the JSON scanner by registering keywords and symbols.
KeywordId
Definition of JSON keyword IDs.
Definition JSONParser.h:336
@ KEYWORD_FALSE
Keyword 'false'.
Definition JSONParser.h:340
SymbolId
Definition of JSON symbol IDs.
Definition JSONParser.h:317
@ SYMBOL_LEFT_BRACKET
Left bracket '['.
Definition JSONParser.h:323
@ SYMBOL_RIGHT_BRACE
Right brace '}'.
Definition JSONParser.h:321
@ SYMBOL_COLON
Colon ':'.
Definition JSONParser.h:327
@ SYMBOL_RIGHT_BRACKET
Right bracket ']'.
Definition JSONParser.h:325
This class implements a JSON value that can hold different JSON types.
Definition JSONParser.h:35
JSONValue & operator=(JSONValue &&other)=default
Move assignment operator.
bool isNumber() const
Returns whether this value is a number.
Definition JSONParser.h:465
bool isString() const
Returns whether this value is a string.
Definition JSONParser.h:470
const std::string & string() const
Returns the string value.
JSONValue(JSONValue &&other)=default
Move constructor.
const Array * arrayFromObject(const std::string &key) const
Extracts an array value from this object by key.
JSONValue()=default
Creates an invalid JSON value.
JSONValue(double value)
Creates a number JSON value.
const Array & array() const
Returns the array value.
const bool * booleanFromObject(const std::string &key) const
Extracts a boolean value from this object by key.
double number() const
Returns the number value.
JSONValue(const Array &value)
Creates an array JSON value.
bool isNull() const
Returns whether this value is null.
Definition JSONParser.h:455
bool isObject() const
Returns whether this value is an object.
Definition JSONParser.h:480
bool isBoolean() const
Returns whether this value is a boolean.
Definition JSONParser.h:460
const JSONValue * valueFromObject(const std::string &key) const
Extracts a JSONValue from an object by key.
JSONValue(ObjectMap &&value)
Creates an object JSON value.
bool boolean() const
Returns the boolean value.
JSONValue(bool value)
Creates a boolean JSON value.
const ObjectMap & object() const
Returns the object value.
std::variant< std::monostate, std::nullptr_t, bool, double, std::string, UniqueArray, UniqueObjectMap > data_
The value data.
Definition JSONParser.h:300
const ObjectMap * objectFromObject(const std::string &key) const
Extracts an object value from this object by key.
Type type_
The type of this JSON value.
Definition JSONParser.h:297
std::unordered_map< std::string, JSONValue > ObjectMap
Definition of a JSON object (map of string keys to JSONValue).
Definition JSONParser.h:67
std::vector< JSONValue > Array
Definition of a JSON array (vector of JSONValue).
Definition JSONParser.h:62
std::unique_ptr< Array > UniqueArray
Definition of a unique pointer holding an array.
Definition JSONParser.h:74
JSONValue(std::nullptr_t value)
Creates a null JSON value.
JSONValue(Array &&value)
Creates an array JSON value.
const double * numberFromObject(const std::string &key) const
Extracts a number value from this object by key.
JSONValue & operator=(const JSONValue &other)
Copy assignment operator.
JSONValue(std::string &&value)
Creates a string JSON value.
bool isArray() const
Returns whether this value is an array.
Definition JSONParser.h:475
bool isValid() const
Returns whether this value is valid.
Definition JSONParser.h:485
Type
Definition of different JSON value types.
Definition JSONParser.h:42
@ TYPE_BOOLEAN
Boolean value.
Definition JSONParser.h:48
@ TYPE_NUMBER
Number value.
Definition JSONParser.h:50
@ TYPE_NULL
Null value.
Definition JSONParser.h:46
@ TYPE_STRING
String value.
Definition JSONParser.h:52
@ TYPE_ARRAY
Array value.
Definition JSONParser.h:54
JSONValue(const JSONValue &other)
Copy constructor.
JSONValue(const std::string &value)
Creates a string JSON value.
Type type() const
Returns the type of this JSON value.
Definition JSONParser.h:450
const std::string * stringFromObject(const std::string &key) const
Extracts a string value from this object by key.
std::unique_ptr< ObjectMap > UniqueObjectMap
Definition of a unique pointer holding an object.
Definition JSONParser.h:79
JSONValue(const ObjectMap &value)
Creates an object JSON value.
This class implements a JSON parser using the Scanner.
Definition JSONParser.h:28
static JSONValue parse(const std::shared_ptr< std::istream > &stream, const bool strict=false, std::string *errorMessage=nullptr)
Parses JSON from a stream.
static JSONValue parseValue(JSONScanner &scanner, const bool strict, std::string *errorMessage)
Parses a JSON value.
static JSONValue parseArray(JSONScanner &scanner, const bool strict, std::string *errorMessage)
Parses a JSON array.
static JSONValue parse(const std::string &filename, const std::string &buffer, const bool strict=false, std::string *errorMessage=nullptr)
Parses JSON from a file or buffer.
static JSONValue parseObject(JSONScanner &scanner, const bool strict, std::string *errorMessage)
Parses a JSON object.
static JSONValue parse(const std::string &filename, std::string &&buffer, const bool strict=false, std::string *errorMessage=nullptr)
Parses JSON from a file or buffer.
static std::string createErrorMessage(const JSONScanner &scanner, const std::string &message)
Creates an error message with line and column information.
This class implements a simple scanner.
Definition Scanner.h:31
The namespace covering the entire Ocean framework.
Definition Accessor.h:15