Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
Static Public Member Functions | Static Private Member Functions | Static Private Attributes
JSON::Reader Class Reference

Class for static purpose only (i.e. not constructible) that handles reading .json files. Once a file read, it can also rewrite its data in a file. However this functionnality is not used for now. More...

#include <jsonLoader.h>

Static Public Member Functions

static std::unique_ptr< NodeloadFromFile (std::string const &nameFile)
 Reads a .json file and returns a tree containing all the JSON structure in ther file. More...
 
static void saveToFile (std::string const &nameFile, Node *tree)
 Writes a tree into a file in json format. More...
 
static void saveToFile (std::string const &nameFile, std::unique_ptr< Node > const &tree)
 Writes a tree into a file in json format. More...
 
static std::unique_ptr< NodeloadFromFile (std::string const &nameFile)
 Reads a .json file and returns a tree containing all the JSON structure in ther file. More...
 
static void saveToFile (std::string const &nameFile, Node *tree)
 Writes a tree into a file in json format. More...
 
static void saveToFile (std::string const &nameFile, std::unique_ptr< Node > const &tree)
 Writes a tree into a file in json format. More...
 

Static Private Member Functions

static int readLineNumber ()
 
static void readingError (std::string const &error)
 Displays an error message when a bad structure is encountered in reading. More...
 
static void unexpectedEndOfFile ()
 Raises an error when the end of the file is reached while reading.
 
static char readSeparator ()
 Reads the next separator ('{', '[', ':', ...) ignoring spaces and line break. More...
 
static std::string readString ()
 Reads a string (parameter between "") once the first " has been read. More...
 
static char readBoolean (char first, std::string &strBool)
 Reads a boolean, "treu" of "false", into a std::string. More...
 
static char readNumber (char first, std::string &strNumber)
 Reads a number, double or int, into a std::string. More...
 
static char getBooleanLeaf (Child &child, std::string const &specifier, char first)
 Constructs a Leaf that stores a boolean read in the file. More...
 
static char getNumberLeaf (Child &child, std::string const &specifier, char first)
 Constructs a Leaf that stores a number read in the file. More...
 
static char getStringLeaf (Child &child, std::string const &specifier)
 Constructs a Leaf that stores a string read in the file. More...
 
static char readNode (Child &child, std::string const &specifier)
 Constructs a Node read in the file. More...
 
static char readList (Child &child, std::string const &specifier)
 Constructs a List read in the file. More...
 
static char readObject (Child &child, std::string const &specifier)
 Constructs a Object read in the file. More...
 
static char readObject (Child &child, std::string const &specifier, char first)
 Constructs a Object read in the file. More...
 
static void writeToFileWithIndent (std::string const &str)
 Writes a string into the file (when writing) with the indentation level indent. More...
 
static void writeToFile (Object *object, bool writeSpec=true)
 Writes an Object into the file (when writing). More...
 
static int readLineNumber ()
 
static void readingError (std::string const &error)
 Displays an error message when a bad structure is encountered in reading. More...
 
static void unexpectedEndOfFile ()
 Raises an error when the end of the file is reached while reading.
 
static char readSeparator ()
 Reads the next separator ('{', '[', ':', ...) ignoring spaces and line break. More...
 
static std::string readString ()
 Reads a string (parameter between "") once the first " has been read. More...
 
static char readBoolean (char first, std::string &strBool)
 Reads a boolean, "treu" of "false", into a std::string. More...
 
static char readNumber (char first, std::string &strNumber)
 Reads a number, double or int, into a std::string. More...
 
static char getBooleanLeaf (Child &child, std::string const &specifier, char first)
 Constructs a Leaf that stores a boolean read in the file. More...
 
static char getNumberLeaf (Child &child, std::string const &specifier, char first)
 Constructs a Leaf that stores a number read in the file. More...
 
static char getStringLeaf (Child &child, std::string const &specifier)
 Constructs a Leaf that stores a string read in the file. More...
 
static char readNode (Child &child, std::string const &specifier)
 Constructs a Node read in the file. More...
 
static char readList (Child &child, std::string const &specifier)
 Constructs a List read in the file. More...
 
static char readObject (Child &child, std::string const &specifier)
 Constructs a Object read in the file. More...
 
static char readObject (Child &child, std::string const &specifier, char first)
 Constructs a Object read in the file. More...
 
static void writeToFileWithIndent (std::string const &str)
 Writes a string into the file (when writing) with the indentation level indent. More...
 
static void writeToFile (Object *object, bool writeSpec=true)
 Writes an Object into the file (when writing). More...
 

Static Private Attributes

static std::ofstream fout
 Output stream for the Reader when writing in a file.
 
static std::ifstream fin
 Input stream for the Reader when reading in a file.
 
static std::string currentFileName
 Current file name when reading / writing. The only use of this variable is for specifying errors more clearly if there is multiple files.
 
static constexpr const int indentStep = 2
 Number of spaces for the indentation when writing in a file.
 
static int indent
 Current level of indentation when writing in a file.
 

Detailed Description

Class for static purpose only (i.e. not constructible) that handles reading .json files. Once a file read, it can also rewrite its data in a file. However this functionnality is not used for now.

Member Function Documentation

◆ getBooleanLeaf() [1/2]

static char JSON::Reader::getBooleanLeaf ( Child child,
std::string const &  specifier,
char  first 
)
staticprivate

Constructs a Leaf that stores a boolean read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Leaf created.
specifierThe specifier of the Leaf.
firstThe first character of the boolean read by the caller.
Returns
The separator encountered after the boolean.

◆ getBooleanLeaf() [2/2]

static char JSON::Reader::getBooleanLeaf ( Child child,
std::string const &  specifier,
char  first 
)
staticprivate

Constructs a Leaf that stores a boolean read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Leaf created.
specifierThe specifier of the Leaf.
firstThe first character of the boolean read by the caller.
Returns
The separator encountered after the boolean.

◆ getNumberLeaf() [1/2]

static char JSON::Reader::getNumberLeaf ( Child child,
std::string const &  specifier,
char  first 
)
staticprivate

Constructs a Leaf that stores a number read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Leaf created.
specifierThe specifier of the Leaf.
firstThe first character of the number read by the caller.
Returns
The separator encountered after the number.

◆ getNumberLeaf() [2/2]

static char JSON::Reader::getNumberLeaf ( Child child,
std::string const &  specifier,
char  first 
)
staticprivate

Constructs a Leaf that stores a number read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Leaf created.
specifierThe specifier of the Leaf.
firstThe first character of the number read by the caller.
Returns
The separator encountered after the number.

◆ getStringLeaf() [1/2]

static char JSON::Reader::getStringLeaf ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a Leaf that stores a string read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Leaf created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the string.

◆ getStringLeaf() [2/2]

static char JSON::Reader::getStringLeaf ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a Leaf that stores a string read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Leaf created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the string.

◆ loadFromFile() [1/2]

static std::unique_ptr<Node> JSON::Reader::loadFromFile ( std::string const &  nameFile)
static

Reads a .json file and returns a tree containing all the JSON structure in ther file.

Parameters
nameFileName of the file.
Returns
A std::unique_ptr to the root Node of the tree (transfers ownership to the caller).

◆ loadFromFile() [2/2]

static std::unique_ptr<Node> JSON::Reader::loadFromFile ( std::string const &  nameFile)
static

Reads a .json file and returns a tree containing all the JSON structure in ther file.

Parameters
nameFileName of the file.
Returns
A std::unique_ptr to the root Node of the tree (transfers ownership to the caller).

◆ readBoolean() [1/2]

static char JSON::Reader::readBoolean ( char  first,
std::string &  strBool 
)
staticprivate

Reads a boolean, "treu" of "false", into a std::string.

Parameters
firstFirst character of the boolean (read by the caller).
strNumberString that is modified to store the boolean.
Returns
The separator encountered after the number.

◆ readBoolean() [2/2]

static char JSON::Reader::readBoolean ( char  first,
std::string &  strBool 
)
staticprivate

Reads a boolean, "treu" of "false", into a std::string.

Parameters
firstFirst character of the boolean (read by the caller).
strNumberString that is modified to store the boolean.
Returns
The separator encountered after the number.

◆ readingError() [1/2]

static void JSON::Reader::readingError ( std::string const &  error)
staticprivate

Displays an error message when a bad structure is encountered in reading.

Parameters
errorSpecification of the error.

◆ readingError() [2/2]

static void JSON::Reader::readingError ( std::string const &  error)
staticprivate

Displays an error message when a bad structure is encountered in reading.

Parameters
errorSpecification of the error.

◆ readLineNumber() [1/2]

int JSON::Reader::readLineNumber ( )
staticprivate
Returns
The current line when reading in a file. Useful to spot errors.

◆ readLineNumber() [2/2]

static int JSON::Reader::readLineNumber ( )
staticprivate
Returns
The current line when reading in a file. Useful to spot errors.

◆ readList() [1/2]

static char JSON::Reader::readList ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a List read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the List created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the List.

◆ readList() [2/2]

static char JSON::Reader::readList ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a List read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the List created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the List.

◆ readNode() [1/2]

static char JSON::Reader::readNode ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a Node read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Node created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the Node.

◆ readNode() [2/2]

static char JSON::Reader::readNode ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a Node read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Node created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the Node.

◆ readNumber() [1/2]

static char JSON::Reader::readNumber ( char  first,
std::string &  strNumber 
)
staticprivate

Reads a number, double or int, into a std::string.

Parameters
firstFirst character of the number (read by the caller).
strNumberString that is modified to store the number.
Returns
The separator encountered after the number.

◆ readNumber() [2/2]

static char JSON::Reader::readNumber ( char  first,
std::string &  strNumber 
)
staticprivate

Reads a number, double or int, into a std::string.

Parameters
firstFirst character of the number (read by the caller).
strNumberString that is modified to store the number.
Returns
The separator encountered after the number.

◆ readObject() [1/4]

static char JSON::Reader::readObject ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a Object read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Object created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the Object.

◆ readObject() [2/4]

static char JSON::Reader::readObject ( Child child,
std::string const &  specifier 
)
staticprivate

Constructs a Object read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Object created.
specifierThe specifier of the Leaf.
Returns
The separator encountered after the Object.

◆ readObject() [3/4]

static char JSON::Reader::readObject ( Child child,
std::string const &  specifier,
char  first 
)
staticprivate

Constructs a Object read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Object created.
specifierThe specifier of the Leaf.
firstThe first character of the Object read by the caller.
Returns
The separator encountered after the Object.

◆ readObject() [4/4]

static char JSON::Reader::readObject ( Child child,
std::string const &  specifier,
char  first 
)
staticprivate

Constructs a Object read in the file.

Parameters
childunique_ptr to an Object modified during the call to point to the Object created.
specifierThe specifier of the Leaf.
firstThe first character of the Object read by the caller.
Returns
The separator encountered after the Object.

◆ readSeparator() [1/2]

char JSON::Reader::readSeparator ( )
staticprivate

Reads the next separator ('{', '[', ':', ...) ignoring spaces and line break.

Returns
The separator char.

◆ readSeparator() [2/2]

static char JSON::Reader::readSeparator ( )
staticprivate

Reads the next separator ('{', '[', ':', ...) ignoring spaces and line break.

Returns
The separator char.

◆ readString() [1/2]

string JSON::Reader::readString ( )
staticprivate

Reads a string (parameter between "") once the first " has been read.

Returns
The string between "".

◆ readString() [2/2]

static std::string JSON::Reader::readString ( )
staticprivate

Reads a string (parameter between "") once the first " has been read.

Returns
The string between "".

◆ saveToFile() [1/4]

static void JSON::Reader::saveToFile ( std::string const &  nameFile,
Node tree 
)
static

Writes a tree into a file in json format.

Parameters
nameFileName of the file.
treeA Node*, root Node of the tree.

◆ saveToFile() [2/4]

void JSON::Reader::saveToFile ( std::string const &  nameFile,
Node tree 
)
static

Writes a tree into a file in json format.

Parameters
nameFileName of the file.
treeA Node*, root Node of the tree.

◆ saveToFile() [3/4]

static void JSON::Reader::saveToFile ( std::string const &  nameFile,
std::unique_ptr< Node > const &  tree 
)
static

Writes a tree into a file in json format.

Parameters
nameFileName of the file.
treeA std::unique_ptr<Node>, root Node of the tree.

◆ saveToFile() [4/4]

static void JSON::Reader::saveToFile ( std::string const &  nameFile,
std::unique_ptr< Node > const &  tree 
)
static

Writes a tree into a file in json format.

Parameters
nameFileName of the file.
treeA std::unique_ptr<Node>, root Node of the tree.

◆ writeToFile() [1/2]

void JSON::Reader::writeToFile ( Object object,
bool  writeSpec = true 
)
staticprivate

Writes an Object into the file (when writing).

Parameters
objectPointer to the Object to write.
writeSpecBoolean specifying if the specifier must be written or not (must not be written for elements of a List).

◆ writeToFile() [2/2]

static void JSON::Reader::writeToFile ( Object object,
bool  writeSpec = true 
)
staticprivate

Writes an Object into the file (when writing).

Parameters
objectPointer to the Object to write.
writeSpecBoolean specifying if the specifier must be written or not (must not be written for elements of a List).

◆ writeToFileWithIndent() [1/2]

static void JSON::Reader::writeToFileWithIndent ( std::string const &  str)
staticprivate

Writes a string into the file (when writing) with the indentation level indent.

Parameters
strString to write in the file.

◆ writeToFileWithIndent() [2/2]

static void JSON::Reader::writeToFileWithIndent ( std::string const &  str)
staticprivate

Writes a string into the file (when writing) with the indentation level indent.

Parameters
strString to write in the file.

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