Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
jsonLoader.h
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
22 #ifndef JSONLOADER_H_INCLUDED
23 #define JSONLOADER_H_INCLUDED
24 
25 #include "jsonObject.h"
26 #include <fstream>
27 #include <optional>
28 
29 namespace JSON {
30 
37 void JSONAssert(bool condition,
38  std::string const& spec);
39 
46 class Reader {
47 
48  public:
49 
50  Reader() = delete;
51 
52  public:
53 
61  static std::unique_ptr<Node> loadFromFile(std::string const& nameFile);
62 
68  static void saveToFile(std::string const& nameFile,
69  Node* tree);
70 
76  static void saveToFile(std::string const& nameFile,
77  std::unique_ptr<Node> const& tree);
78 
79  private:
80 
84  static int readLineNumber();
85 
91  static void readingError(std::string const& error);
92 
96  static void unexpectedEndOfFile();
97 
103  static char readSeparator();
104 
110  static std::string readString();
111 
118  static char readBoolean(char first,
119  std::string& strBool);
120 
127  static char readNumber(char first,
128  std::string& strNumber);
129 
138  static char getBooleanLeaf(Child& child,
139  std::string const& specifier,
140  char first);
141 
150  static char getNumberLeaf(Child& child,
151  std::string const& specifier,
152  char first);
153 
161  static char getStringLeaf(Child& child,
162  std::string const& specifier);
163 
171  static char readNode(Child& child,
172  std::string const& specifier);
173 
181  static char readList(Child& child,
182  std::string const& specifier);
183 
191  static char readObject(Child& child,
192  std::string const& specifier);
193 
202  static char readObject(Child& child,
203  std::string const& specifier,
204  char first);
205 
211  static void writeToFileWithIndent(std::string const& str);
212 
219  static void writeToFile(Object* object,
220  bool writeSpec = true);
221 
222  private:
223 
227  inline static std::ofstream fout;
228 
232  inline static std::ifstream fin;
233 
239  inline static std::string currentFileName;
240 
244  constexpr static const int indentStep = 2;
245 
249  inline static int indent;
250 };
251 
257 class Parser {
258 
259  public:
260 
261  Parser() = delete;
262 
263  public:
264 
277  template<typename Targ>
278  static std::optional<Targ> parseArgument(
279  Node* node,
280  std::string const& specifier,
281  bool mandatory = false) {
282  if (not node)
283  return std::nullopt;
284  for (const auto& object : *node) {
285  if (object->getSpecifier() == specifier) {
286  return interpretObject<Targ>(object.get());
287  }
288  }
289  JSONAssert(not mandatory,
290  "Argument \"" + specifier + "\" should be specified in "
291  + "node \"" + node->getSpecifier() + "\".");
292  return std::nullopt;
293  }
294 
307  template<typename Targ>
308  static std::optional<Targ> parseArgument(
309  std::unique_ptr<Object> const& object,
310  std::string const& specifier,
311  bool mandatory = false) {
312  return parseArgument<Targ>(convert(object), specifier, mandatory);
313  }
314 
326  static Node* parseNode(Node* parent,
327  std::string const& specifier,
328  bool mandatory = false);
329 
341  static Node* parseNode(std::unique_ptr<Object> const& parent,
342  std::string const& specifier,
343  bool mandatory = false);
344 
353  template<typename Targ>
354  static Targ interpretObject(Object* object) {
355  JSONAssert(typeid(*object) == typeid(Leaf<Targ>),
356  "Bad type required in JSON::interpretObject for object \""
357  + object->getSpecifier() + "\": \""
358  + (std::string)(typeid(*object).name()) + "\" given, \""
359  + (std::string)(typeid(Leaf<Targ>).name()) + "\" expected.");
360  return static_cast<Leaf<Targ>*>(object)->getTypedArgument();
361  }
362 
363  static Node* convert(const std::unique_ptr<Object>& node);
364 };
365 
366 } // End of namespace JSON
367 
368 
369 #endif
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.
Class for static purpose only (i.e. not constructible) that handles reading .json files...
Definition: jsonLoader.h:46
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 (&#39;{&#39;, &#39;[&#39;, &#39;:&#39;, ...) 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&#39;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.
Class for static purpose only (i.e. not constructible) that gather data from a tree (JSON::Node objec...
Definition: jsonLoader.h:257
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