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#include "TypeResolver.h"
13
14namespace vl
15{
17 {
18 struct ContainerInfo;
19
20 public:
21 JSONLoader(vl::Object& object, const TypeResolver& typeResolver = TypeResolver());
22 ContainerInfo* GetCurrentContainer();
23 ContainerInfo* PushContainer(vl::Var* ptr, const std::string& name);
24 void PopContainer();
25
26 // rapidjson interface
27 typedef char Ch;
28 typedef size_t SizeType;
29 bool Null();
30 bool Bool(bool b);
31 bool Int(int i);
32 bool Uint(unsigned i);
33 bool Int64(int64_t i);
34 bool Uint64(uint64_t i);
35 bool Double(double d);
37 bool RawNumber(const Ch* str, SizeType length, bool copy);
38 bool String(const Ch* str, SizeType length, bool copy);
39 bool StartObject();
40 bool Key(const Ch* str, SizeType length, bool copy);
41 bool EndObject(SizeType memberCount);
42 bool StartArray();
43 bool EndArray(SizeType elementCount);
44
45 protected:
46 bool AddVar(const vl::VarPtr& ptr);
47 // 3 following functions: Memorize current object or a list as a container for the next parsed elements
48 void PushNewList(ContainerInfo* parentContainer, const std::string& listName);
49 void PushNewObject(ContainerInfo* c, const std::string& objectName);
50 ContainerInfo* PushNewContainer(
51 bool isObject
52 , bool isList
53 , ContainerInfo* parentConainer
54 , const std::string& newContainerName
55 );
56 // Store references to prototypes which types have not been parsed yet
58 // Link eventually parsed type objects to the prototypes
59 void ResolveRefs();
60
61 private:
62 struct ContainerInfo
63 {
64 ContainerInfo(vl::Var& containerInstance, const std::string& containerName)
65 : var(containerInstance)
66 , name(containerName)
67 {}
68 vl::Var& var;
69 std::string name;
70 };
71 private:
72 vl::Object& mObject;
73 std::vector<ContainerInfo> mStack;
74 std::string mCurrentKey;
75 bool mKeyProcessed = true;
76 bool mCurrentProto = false;
77 std::list<vl::Object*> mUnresolvedRefs;
78 std::unordered_map<std::string, vl::Object*> mTypeRefs;
79 TypeResolver mTypeResolver;
80 };
81}
Definition: TypeResolver.h:7
Definition: vl.h:31
Definition: JSONLoader.h:17
bool Null()
Definition: JSONLoader.cpp:175
bool Int(int i)
Definition: JSONLoader.cpp:190
char Ch
Definition: JSONLoader.h:27
bool StartArray()
Definition: JSONLoader.cpp:301
ContainerInfo * PushNewContainer(bool isObject, bool isList, ContainerInfo *parentConainer, const std::string &newContainerName)
Definition: JSONLoader.cpp:96
size_t SizeType
Definition: JSONLoader.h:28
bool Uint64(uint64_t i)
Definition: JSONLoader.cpp:215
bool StartObject()
Definition: JSONLoader.cpp:256
ContainerInfo * GetCurrentContainer()
Definition: JSONLoader.cpp:15
bool RawNumber(const Ch *str, SizeType length, bool copy)
enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length)
Definition: JSONLoader.cpp:233
bool Key(const Ch *str, SizeType length, bool copy)
Definition: JSONLoader.cpp:275
void PushNewObject(ContainerInfo *c, const std::string &objectName)
Definition: JSONLoader.cpp:81
bool EndArray(SizeType elementCount)
Definition: JSONLoader.cpp:314
bool AddVar(const vl::VarPtr &ptr)
Definition: JSONLoader.cpp:35
bool Uint(unsigned i)
Definition: JSONLoader.cpp:197
void PopContainer()
Definition: JSONLoader.cpp:28
bool String(const Ch *str, SizeType length, bool copy)
Definition: JSONLoader.cpp:240
JSONLoader(vl::Object &object, const TypeResolver &typeResolver=TypeResolver())
Definition: JSONLoader.cpp:10
bool EndObject(SizeType memberCount)
Definition: JSONLoader.cpp:292
ContainerInfo * PushContainer(vl::Var *ptr, const std::string &name)
Definition: JSONLoader.cpp:23
bool Double(double d)
Definition: JSONLoader.cpp:224
bool Bool(bool b)
Definition: JSONLoader.cpp:183
void StoreUnresolvedRef(vl::Object &ref)
Definition: JSONLoader.cpp:122
void PushNewList(ContainerInfo *parentContainer, const std::string &listName)
Definition: JSONLoader.cpp:76
bool Int64(int64_t i)
Definition: JSONLoader.cpp:206
void ResolveRefs()
Definition: JSONLoader.cpp:127
Definition: vl.h:173
Definition: JSONConverter.h:9
std::shared_ptr< Var > VarPtr
Definition: vl_fwd.h:41