22 #ifndef JSONLOADER_H_INCLUDED 23 #define JSONLOADER_H_INCLUDED 25 #include "jsonObject.h" 38 std::string
const& spec);
61 static std::unique_ptr<Node>
loadFromFile(std::string
const& nameFile);
68 static void saveToFile(std::string
const& nameFile,
76 static void saveToFile(std::string
const& nameFile,
77 std::unique_ptr<Node>
const& tree);
119 std::string& strBool);
128 std::string& strNumber);
139 std::string
const& specifier,
151 std::string
const& specifier,
162 std::string
const& specifier);
172 std::string
const& specifier);
182 std::string
const& specifier);
192 std::string
const& specifier);
203 std::string
const& specifier,
220 bool writeSpec =
true);
227 inline static std::ofstream
fout;
232 inline static std::ifstream
fin;
277 template<
typename Targ>
280 std::string
const& specifier,
281 bool mandatory =
false) {
284 for (
const auto&
object : *node) {
285 if (object->getSpecifier() == specifier) {
286 return interpretObject<Targ>(
object.get());
290 "Argument \"" + specifier +
"\" should be specified in " 291 +
"node \"" + node->getSpecifier() +
"\".");
307 template<
typename Targ>
309 std::unique_ptr<Object>
const&
object,
310 std::string
const& specifier,
311 bool mandatory =
false) {
312 return parseArgument<Targ>(convert(
object), specifier, mandatory);
326 static Node* parseNode(
Node* parent,
327 std::string
const& specifier,
328 bool mandatory =
false);
341 static Node* parseNode(std::unique_ptr<Object>
const& parent,
342 std::string
const& specifier,
343 bool mandatory =
false);
353 template<
typename Targ>
356 "Bad type required in JSON::interpretObject for object \"" 358 + (std::string)(
typeid(*object).name()) +
"\" given, \"" 359 + (std::string)(
typeid(
Leaf<Targ>).name()) +
"\" expected.");
360 return static_cast<Leaf<Targ>*
>(object)->getTypedArgument();
363 static Node* convert(
const std::unique_ptr<Object>& node);
static std::string readString()
Reads a string (parameter between "") once the first " has been read.
Definition: jsonLoader.cpp:127
static char getBooleanLeaf(Child &child, std::string const &specifier, char first)
Constructs a Leaf that stores a boolean read in the file.
static char getNumberLeaf(Child &child, std::string const &specifier, char first)
Constructs a Leaf that stores a number read in the file.
static std::ofstream fout
Output stream for the Reader when writing in a file.
Definition: jsonLoader.h:227
static char readBoolean(char first, std::string &strBool)
Reads a boolean, "treu" of "false", into a std::string.
static char readObject(Child &child, std::string const &specifier)
Constructs a Object read in the file.
static char readList(Child &child, std::string const &specifier)
Constructs a List read in the file.
static int readLineNumber()
Definition: jsonLoader.cpp:85
Template class inherited from Object that stores a parameter of a .json file. This class does not hav...
Definition: jsonObject.h:335
static char getStringLeaf(Child &child, std::string const &specifier)
Constructs a Leaf that stores a string read in the file.
static char readNode(Child &child, std::string const &specifier)
Constructs a Node read in the file.
static void readingError(std::string const &error)
Displays an error message when a bad structure is encountered in reading.
void JSONAssert(bool condition, std::string const &spec)
Assertion function for JSON reading / writing.
Definition: jsonLoader.cpp:25
static void writeToFile(Object *object, bool writeSpec=true)
Writes an Object into the file (when writing).
Definition: jsonLoader.cpp:305
static void saveToFile(std::string const &nameFile, Node *tree)
Writes a tree into a file in json format.
Definition: jsonLoader.cpp:62
static void unexpectedEndOfFile()
Raises an error when the end of the file is reached while reading.
Definition: jsonLoader.cpp:111
static char readSeparator()
Reads the next separator ('{', '[', ':', ...) ignoring spaces and line break.
Definition: jsonLoader.cpp:116
static Targ interpretObject(Object *object)
Returns the argument with the correct type from an Object that must be a Leaf.
Definition: jsonLoader.h:354
Inherits from JSON::Object, specialized in JSON Node. A Node owns a vector of Object. The Node's children can either be Leaf or other Node objects. This allows to store the tree structure of a .json file.
Definition: jsonObject.h:97
static std::optional< Targ > parseArgument(std::unique_ptr< Object > const &object, std::string const &specifier, bool mandatory=false)
Allows to get arguments of a node, given the specifier and the type of the desired object...
Definition: jsonLoader.h:308
Abstract object in JSON tree structure. Can be specialized either in Node or in Leaf<T> with a specif...
Definition: jsonObject.h:43
static std::optional< Targ > parseArgument(Node *node, std::string const &specifier, bool mandatory=false)
Allows to get arguments of a node, given the specifier and the type of the desired object...
Definition: jsonLoader.h:278
static int indent
Current level of indentation when writing in a file.
Definition: jsonLoader.h:249
static char readNumber(char first, std::string &strNumber)
Reads a number, double or int, into a std::string.
std::unique_ptr< Object > Child
Type definition for unique_ptr<Object>. Lighten the vector of Object owned by Node objects...
Definition: jsonObject.h:89
static std::unique_ptr< Node > loadFromFile(std::string const &nameFile)
Reads a .json file and returns a tree containing all the JSON structure in ther file.
static std::ifstream fin
Input stream for the Reader when reading in a file.
Definition: jsonLoader.h:232
static std::string currentFileName
Current file name when reading / writing. The only use of this variable is for specifying errors more...
Definition: jsonLoader.h:239
static constexpr const int indentStep
Number of spaces for the indentation when writing in a file.
Definition: jsonLoader.h:244
static void writeToFileWithIndent(std::string const &str)
Writes a string into the file (when writing) with the indentation level indent.
Contains all objects related to JSON reading / writing.
Definition: csldatahandler.h:30
std::string getSpecifier() const
Definition: jsonObject.h:62