VL
JSONLoader.h
Go to the documentation of this file.
1// This is a custom visitor for rapidjson::Value.
2// The interface described by the link below:
3// https://internal.dunescience.org/doxygen/classrapidjson_1_1Handler.html
4
5#include <cstdint>
6#include <vector>
7#include <list>
8#include <string>
9#include <memory>
10#include <unordered_map>
11#include "vl_fwd.h"
12
13namespace vl
14{
16 {
17 struct ContainerInfo;
18
19 public:
20 JSONLoader(vl::Object& object) : mObject(object) {}
21 ContainerInfo* GetCurrentContainer();
22 ContainerInfo* PushContainer(vl::Var* ptr, const std::string& name);
23 void PopContainer();
24
25 // rapidjson interface
26 typedef char Ch;
27 typedef size_t SizeType;
28 bool Null();
29 bool Bool(bool b);
30 bool Int(int i);
31 bool Uint(unsigned i);
32 bool Int64(int64_t i);
33 bool Uint64(uint64_t i);
34 bool Double(double d);
36 bool RawNumber(const Ch* str, SizeType length, bool copy);
37 bool String(const Ch* str, SizeType length, bool copy);
38 bool StartObject();
39 bool Key(const Ch* str, SizeType length, bool copy);
40 bool EndObject(SizeType memberCount);
41 bool StartArray();
42 bool EndArray(SizeType elementCount);
43
44 protected:
45 bool AddVar(const vl::VarPtr& ptr);
46 // 3 following functions: Memorize current object or a list as a container for the next parsed elements
47 void PushNewList(ContainerInfo* parentContainer, const std::string& listName);
48 void PushNewObject(ContainerInfo* c, const std::string& objectName);
49 ContainerInfo* PushNewContainer(
50 bool isObject
51 , bool isList
52 , ContainerInfo* parentConainer
53 , const std::string& newContainerName
54 );
55 // Store references to prototypes which types have not been parsed yet
56 void StoreUnresolvedRef(vl::Object& ref);
57 // Link eventually parsed type objects to the prototypes
58 void ResolveRefs();
59
60 private:
61 struct ContainerInfo
62 {
63 ContainerInfo(vl::Var& containerInstance, const std::string& containerName)
64 : var(containerInstance)
65 , name(containerName)
66 {}
67 vl::Var& var;
68 std::string name;
69 };
70 private:
71 vl::Object& mObject;
72 std::vector<ContainerInfo> mStack;
73 std::string mCurrentKey;
74 bool mCurrentProto = false;
75 std::list<vl::Object*> mUnresolvedRefs;
76 std::unordered_map<std::string, vl::Object*> mTypeRefs;
77 };
78}
Definition: JSONLoader.h:16
bool Null()
Definition: JSONLoader.cpp:126
bool Int(int i)
Definition: JSONLoader.cpp:141
char Ch
Definition: JSONLoader.h:26
bool StartArray()
Definition: JSONLoader.cpp:247
ContainerInfo * PushNewContainer(bool isObject, bool isList, ContainerInfo *parentConainer, const std::string &newContainerName)
Definition: JSONLoader.cpp:76
size_t SizeType
Definition: JSONLoader.h:27
bool Uint64(uint64_t i)
Definition: JSONLoader.cpp:166
bool StartObject()
Definition: JSONLoader.cpp:207
ContainerInfo * GetCurrentContainer()
Definition: JSONLoader.cpp:6
bool RawNumber(const Ch *str, SizeType length, bool copy)
enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length)
Definition: JSONLoader.cpp:184
bool Key(const Ch *str, SizeType length, bool copy)
Definition: JSONLoader.cpp:223
void PushNewObject(ContainerInfo *c, const std::string &objectName)
Definition: JSONLoader.cpp:61
bool EndArray(SizeType elementCount)
Definition: JSONLoader.cpp:260
bool AddVar(const vl::VarPtr &ptr)
Definition: JSONLoader.cpp:26
bool Uint(unsigned i)
Definition: JSONLoader.cpp:148
void PopContainer()
Definition: JSONLoader.cpp:19
bool String(const Ch *str, SizeType length, bool copy)
Definition: JSONLoader.cpp:191
bool EndObject(SizeType memberCount)
Definition: JSONLoader.cpp:239
ContainerInfo * PushContainer(vl::Var *ptr, const std::string &name)
Definition: JSONLoader.cpp:14
bool Double(double d)
Definition: JSONLoader.cpp:175
bool Bool(bool b)
Definition: JSONLoader.cpp:134
void StoreUnresolvedRef(vl::Object &ref)
Definition: JSONLoader.cpp:103
void PushNewList(ContainerInfo *parentContainer, const std::string &listName)
Definition: JSONLoader.cpp:56
JSONLoader(vl::Object &object)
Definition: JSONLoader.h:20
bool Int64(int64_t i)
Definition: JSONLoader.cpp:157
void ResolveRefs()
Definition: JSONLoader.cpp:108
Definition: JSONConverter.h:8