-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONDocument.cpp
130 lines (97 loc) · 3.69 KB
/
JSONDocument.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// Created by Christian Diaconu on 23/01/16.
//
#include <iostream>
#include "JSONDocument.h"
JSONDocument::JSONDocument() {
this->buffer = NULL;
}
JSONDocument::~JSONDocument() {
if (this->buffer != NULL)
delete this->buffer;
}
void JSONDocument::addObject(const std::string &key, JSONDocument *object) {
this->object_values[key] = object;
}
JSONDocument *JSONDocument::getObject(const std::string &key) const {
return (this->object_values.at(key));
}
void JSONDocument::deleteObject(const std::string &key) {
this->object_values.erase(key);
}
void JSONDocument::addValue(const std::string &key, const std::string &value) {
this->simple_values[key] = value;
}
std::string JSONDocument::getValue(const std::string &key) const {
return (this->simple_values.at(key));
}
void JSONDocument::deleteValue(std::string key) {
this->object_values.erase(key);
}
void JSONDocument::addArray(const std::string &key, JSONDocument *document) {
this->array_values[key].push_back(document);
}
/* JSON Generator - Sys FXs */
void JSONDocument::bufferGenerator_Values() {
unsigned long int i = 0;
// Lets save simple values !
typedef std::map<std::string, std::string>::iterator simple_IT;
for(simple_IT iterator = this->simple_values.begin(); iterator != this->simple_values.end(); iterator++) {
// Compose element in JSON !
this->buffer->append("\"" + iterator->first + "\":\"" + iterator->second + "\"");
// Add simple-colomuns when needed && checking if there is other to selerialize afterwards.
if (i++ != (this->simple_values.size() - 1) || (this->object_values.size() != 0 || this->array_values.size() != 0))
buffer->append(",");
}
}
void JSONDocument::bufferGenerator_Objects() {
unsigned long int i = 0;
// Lets save object values !
typedef std::map<std::string, JSONDocument *>::iterator object_IT;
for(object_IT iterator = this->object_values.begin(); iterator != this->object_values.end(); iterator++) {
// Compose element in JSON !
this->buffer->append("\"" + iterator->first + "\":" + (iterator->second->serialize()));
// Add simple-colomuns when needed && checking if there is other to selerialize afterwards.
if (i++ != (this->object_values.size() - 1) || this->array_values.size() != 0)
buffer->append(",");
}
}
void JSONDocument::bufferGenerator_Arrays() {
unsigned long int i = 0;
unsigned long int i2 = 0;
// Lets save arrays values !
typedef std::map<std::string, std::vector<JSONDocument *> >::iterator array_IT;
for(array_IT iterator = this->array_values.begin(); iterator != this->array_values.end(); iterator++) {
this->buffer->append("\"" + iterator->first + "\":[");
while (i2 != iterator->second.size())
{
this->buffer->append(iterator->second.at(i2)->serialize());
// Add simple-colomuns when needed
if (i2++ != (iterator->second.size() - 1))
buffer->append(",");
}
this->buffer->append("]");
// Add simple-colomuns when needed
if (i++ != (this->array_values.size() - 1))
buffer->append(",");
}
}
/* JSON Main call */
std::string &JSONDocument::serialize() {
if (buffer == NULL)
buffer = new std::string;
buffer->append("{");
this->bufferGenerator_Values();
this->bufferGenerator_Objects();
this->bufferGenerator_Arrays();
buffer->append("}");
return (*buffer);
}
JSONDocument &JSONDocument::operator=(JSONDocument &document) {
(void)document;
return (*this);
}
JSONDocument::JSONDocument(JSONDocument &document) {
(void)document;
}