-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonLite.h
72 lines (63 loc) · 2.08 KB
/
JsonLite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <string>
#include <vector>
/* JsonLite: Lightweight JSON serializer for C++. No complex dependencies. */
namespace JsonLite
{
// Quick C++ 'reflection' base class, determine class type at runtime for pointer down casting.
class JsonElement
{
friend class JsonLiteSerializer;
private:
std::vector<JsonElement*> children;
std::string name;
JsonElement* parent;
public:
enum Type
{
JsonObject,
JsonArray,
JsonString,
JsonInt,
JsonFloat,
JsonBool,
JsonNull
};
Type type;
JsonElement(const std::string& name, Type type, JsonElement* parent) : children(), name(name), type(type), parent(parent) {}
};
template <typename T>
class JsonValue : public JsonElement
{
public:
T value;
JsonValue(const std::string& name, const T& value, JsonElement::Type type, JsonElement* parent) : JsonElement(name, type, parent), value(value) {}
};
class JsonLiteSerializer
{
private:
JsonElement* root;
std::string buffer;
std::vector<JsonElement*> objectList;
void AddTabs(int depth);
JsonElement* AddElement(JsonElement* parent, const std::string& name, JsonElement::Type type);
template<class T, typename V>
JsonElement* AddValue(JsonElement* parent, const std::string& name, const V& value, JsonElement::Type type);
void SanitizeInput(const std::string& input);
void ToString(JsonElement* elem, int depth, bool prettyPrint);
public:
JsonLiteSerializer();
~JsonLiteSerializer();
JsonElement* AddArray(JsonElement* parent, const std::string& name);
JsonElement* AddObject(JsonElement* parent, const std::string& name);
void AddBoolean(JsonElement* parent, const std::string& name, bool value);
void AddFloat(JsonElement* parent, const std::string& name, float value);
void AddInteger(JsonElement* parent, const std::string& name, int value);
void AddNull(JsonElement* parent, const std::string& name);
void AddString(JsonElement* parent, const std::string& name, const std::string& value);
void Clear();
JsonElement* GetParent(JsonElement* elem);
JsonElement* GetRoot();
std::string ToString(bool prettyPrint = false);
};
}