-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonWriter.c
More file actions
283 lines (242 loc) · 6.03 KB
/
JsonWriter.c
File metadata and controls
283 lines (242 loc) · 6.03 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* systcs_JSON_Builder.c
*
* Created on: Aug 9, 2013
* Author: Nehchal J.
* Email: nehchalj@gmail.com
* Edited on : Dec 5, 2013
* Revision Notes : Nov 20, 2013 | Changes made to the floatToStr()
* : Dec 3, 2013 | Optimized the code for computation
* : Dec 5, 2013 | Created an object JsonWriter separate
* from json string - to align with general practices
* for making a library.
*/
#include "stdio.h"
#include "string.h"
#include "JsonWriter.h"
//*****************************************************************************
// Converts an integer to a string. Max possible num of digits = 10
//*****************************************************************************
char* intToStr(int integer, char *str)
{
char cTemp[11];
int i = 0,j =0, temp;
str[0] = '\0';
if (integer == 0)
strcpy(str,"0");
else
{
if (integer < 0){
str[j++] = '-';
integer*= -1; //make the integer positive
}
while(integer>0){
temp = integer%10;
cTemp[i++] = temp + 48;
integer/=10;
}
while(i>0) //reverse the string
str[j++] = cTemp[--i];
str[j] = '\0';
}
return str;
}
//*****************************************************************************
// Converts a the mantissa part of a float number to string. Precision value
// should be less than 4.
//*****************************************************************************
char* mantissaToStr(float floatNum, int precision, char* str)
{
int i = 0, temp, j;
int len;
int intNum;
char strTemp[6];
str[0]='\0';
if (!( precision > 0 && precision <=4)) //check the limits of precision
return strcat(str, "Error");
for (i=0; i<precision; i++)
floatNum*=10.0;
intNum = (int) floatNum;
/* Conver integer into reversed string */
for ( i=0, len=0; intNum>0; i++){
temp = intNum%10;
strTemp[i] = temp + 48;
intNum/=10;
len++;
}
/* Add zeroes if length is less than precision */
for (i=0; i < precision - len; i++)
str[i] = '0';
/* Append strTemp to str after appending */
for (j=len-1; j>=0; j--, i++)
str[i] = strTemp[j];
str[i]='\0';
return str;
}
/* ======= init =======
* Initializes the JsonWriter
*/
void JsonWriter_init(tJsonWriter *jsonWriter, char *buf){
jsonWriter->len = 0;
jsonWriter->outBuf = buf;
}
//*****************************************************************************
// Converts a float to a string. Supports max 6 digits after the decimal point
//*****************************************************************************
char* floatToStr(float floatNum, int precision, char *str)
{
int i;
int integral;
float mantissa;
char sIntegral[11];
char mantissaStr[11];
str[0]='\0';
if(floatNum != 0){
if (floatNum < 0){
strcat(str,"-");
floatNum*= -1;
}
integral = (int) floatNum;
mantissa = (floatNum - integral);
strcat(str, intToStr(integral,sIntegral));
strcat(str,(const char*)".");
strcat(str, mantissaToStr(mantissa, precision, mantissaStr));
//else
//strcat(str,".0");
}
else{
str[0] = '0'; str[1] = '.';
/* append trailing zeroes according to precision */
for (i=2; i < precision + 2; i++)
str[i]='0';
str[i]='\0';
}
return str;
}
//Begins encoding a new array.
void JsonWriter_beginArray(tJsonWriter* obj)
{
obj->outBuf[obj->len++] = '[';
return;
}
//Begins encoding a new object.
void JsonWriter_beginObject(tJsonWriter* obj)
{
//objectOpen = 1;
obj->outBuf[obj->len++] = '{';
return;
}
//Ends encoding the current array.
void JsonWriter_endArray(tJsonWriter* obj)
{
obj->outBuf[obj->len++] = ']';
obj->outBuf[obj->len] = '\0';
return;
}
//Ends encoding the current object.
void JsonWriter_endObject(tJsonWriter* obj)
{
obj->outBuf[obj->len++] = '}';
obj->outBuf[obj->len] = '\0';
return;
}
//Encodes the name.
void JsonWriter_name(tJsonWriter* obj, char* sName)
{
int i;
obj->outBuf[obj->len++] = '\"';
for (i=0; sName[i]!='\0'; i++)
obj->outBuf[obj->len++] = sName[i];
obj->outBuf[obj->len++] = '\"';
obj->outBuf[obj->len++] = ':';
return;
}
//Encodes null.
void JsonWriter_nullValue(tJsonWriter* obj)
{
obj->outBuf[obj->len++] = 'n';
obj->outBuf[obj->len++] = 'u';
obj->outBuf[obj->len++] = 'l';
obj->outBuf[obj->len++] = 'l';
return;
}
void JsonWriter_floatAsStringValue(tJsonWriter* obj, float fValue, int precision)
{
char tempStr[21];
int i;
obj->outBuf[obj->len++] = '"';
floatToStr(fValue, precision, tempStr);
for (i=0; tempStr[i]!='\0'; i++)
obj->outBuf[obj->len++] = tempStr[i];
obj->outBuf[obj->len++] = '"';
return;
}
//Encodes a float value
void JsonWriter_floatValue(tJsonWriter* obj, float fValue, int precision)
{
char tempStr[21];
int i;
floatToStr(fValue, precision, tempStr);
for (i=0; tempStr[i]!='\0'; i++)
obj->outBuf[obj->len++] = tempStr[i];
return;
}
//Encodes a string value
void JsonWriter_stringValue(tJsonWriter* obj, char* sValue)
{
int i;
obj->outBuf[obj->len++] = '\"';
for (i=0; sValue[i]!='\0'; i++)
obj->outBuf[obj->len++] = sValue[i];
obj->outBuf[obj->len++] = '\"';
return;
}
//Encodes a integer value
void JsonWriter_integerValue(tJsonWriter* obj, int iValue)
{
char tempStr[11];
int i;
intToStr(iValue, tempStr);
for (i=0; tempStr[i]!='\0'; i++)
obj->outBuf[obj->len++] = tempStr[i];
return;
}
//Encodes an object value
void JsonWriter_objectValue(tJsonWriter* obj, char* oValue)
{
int i;
for (i=0; oValue[i]!='\0'; i++)
obj->outBuf[obj->len++] = oValue[i];
return;
}
//Encodes an array value
void JsonWriter_arrayValue(tJsonWriter* obj, char* aValue)
{
int i;
for (i=0; aValue[i]!='\0'; i++)
obj->outBuf[obj->len++] = aValue[i];
return;
}
void JsonWriter_boolValue(tJsonWriter* obj, boolean bValue)
{
if (bValue == TRUE){
obj->outBuf[obj->len++] = 't';
obj->outBuf[obj->len++] = 'r';
obj->outBuf[obj->len++] = 'u';
obj->outBuf[obj->len++] = 'e';
}
else{
obj->outBuf[obj->len++] = 'f';
obj->outBuf[obj->len++] = 'a';
obj->outBuf[obj->len++] = 'l';
obj->outBuf[obj->len++] = 's';
obj->outBuf[obj->len++] = 'e';
}
return;
}
//to indicate next pair/value will be added
void JsonWriter_next(tJsonWriter* obj)
{
obj->outBuf[obj->len++] = ',';
return;
}