forked from argandas/ardubson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ardubsonObject.cpp
229 lines (207 loc) · 6.06 KB
/
ardubsonObject.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
ardubson.h - Library for the BSON (Binary-JSON) format.
Created by Hugo Arganda (argandas), April 6, 2016.
Released into the public domain.
*/
#include "ardubson.h"
// Constructor /////////////////////////////////////////////////////////////////
BSONObject::BSONObject(char* data)
{
int size = *(uint32_t *) data;
if ((size >= 0) && (size <= BSON_BUFF_SIZE))
{
memcpy(_objData, data, size);
}
}
// Public Methods //////////////////////////////////////////////////////////////
char* BSONObject::rawData(void)
{
return (char *) &_objData;
}
int32_t BSONObject::len(void)
{
return *(int32_t *) &_objData;
}
BSONElement BSONObject::getField(const char *fieldName)
{
char* e_data = NULL;
int e_len = 0;
BSONElement be;
uint32_t len = *(uint32_t *) &_objData;
uint32_t off = sizeof(uint32_t);
while ((off + 1) < len)
{
// Get next element data type
char type = *(char *) &_objData[off];
e_data = (char *) &_objData[off];
off++;
e_len = 1;
// Check data type range
if ((type > (char) BSON_MINKEY) && (type < (char) BSON_MAXKEY))
{
// Get element key
char *key = (char *) &_objData[off];
off += strlen(key) + 1;
e_len += strlen(key) + 1;
// Check key
if (strcmp(fieldName, key) == 0)
{
// Save value
if (type == (char) BSON_TYPE_STRING)
{
// Get string size
uint32_t sz = *(uint32_t *) &_objData[off];
off += sizeof(uint32_t);
e_len += sizeof(uint32_t);
// Get string value
char *val = (char *) &_objData[off];
off += sz;
e_len += sz;
break;
}
else if (type == (char) BSON_TYPE_INT32)
{
// Get value
int32_t val = *(int32_t *) &_objData[off];
off += sizeof(int32_t);
e_len += sizeof(int32_t);
break;
}
else if (type == (char) BSON_TYPE_INT64)
{
// Get value
int64_t val = *(int64_t *) &_objData[off];
off += sizeof(int64_t);
e_len += sizeof(int64_t);
break;
}
else if (type == (char) BSON_TYPE_BOOLEAN)
{
// Get value
char val = *(char *) &_objData[off];
off += sizeof(char);
e_len += sizeof(char);
break;
}
// EOO
if ((uint32_t) BSON_EOO == _objData[off])
break;
}
else
{
// Ignore incoming data
}
// break;
}
else
{
// Invalid type
break;
}
}
return be.Fill(e_data, e_len);
}
bool BSONObject::appendJSON(const char* data)
{
int len = strlen((char *) &_jsonStr);
if ((len + strlen(data)) < JSON_MAX_SIZE)
{
strcat((char *) &_jsonStr, data);
}
}
char* BSONObject::jsonString(void)
{
/* Clear buffer */
memset(_jsonStr, NULL, JSON_MAX_SIZE);
appendJSON("{");
char *data = (char *) &_objData;
bool first = true;
uint32_t len = *(uint32_t *) data;
data += sizeof(uint32_t);
while ((data - (char *) &_objData) < len)
{
// Get next element data type
char type = *data;
data += sizeof(char);
// Check data type range
if ((type > (char) BSON_MINKEY) && (type < (char) BSON_MAXKEY))
{
// Add trailing comma
if (!first)
{
appendJSON(",");
}
// Get element key
char *key = data;
data += strlen(key) + 1;
appendJSON("\"");
appendJSON(key);
appendJSON("\":");
// Switch according to data type
switch (type)
{
case BSON_TYPE_STRING:
{
// Get string size
uint32_t sz = *(uint32_t *) data;
data += sizeof(uint32_t);
appendJSON("\"");
appendJSON(data);
appendJSON("\"");
data += sz;
break;
}
case BSON_TYPE_INT32:
{
int32_t val = *(int32_t *) data;
data += sizeof(int32_t);
/* TODO: Fix int32 data type being truncated to int by using itoa() */
char buff[8];
itoa(val, buff, 10);
appendJSON(buff);
break;
}
case BSON_TYPE_INT64:
{
int64_t val = *(int64_t *) data;
data += sizeof(int64_t);
/* TODO: Fix int64 data type being truncated to int by using itoa() */
char buff[8];
itoa(val, buff, 10);
appendJSON(buff);
break;
}
case BSON_TYPE_BOOLEAN:
{
char val = *(char *) data;
data += sizeof(char);
if (val == 0x1)
{
appendJSON("true");
}
else
{
appendJSON("false");
}
break;
}
default:
appendJSON("NaN");
}
// EOO
if (BSON_EOO == *data)
break;
}
else
{
// Invalid type
appendJSON("NaT");
break;
}
// At least one element has been added
first = false;
}
appendJSON("}");
return (char *) &_jsonStr;
}
// Private Methods //////////////////////////////////////////////////////////////