2 * JSON data type and serializer for Citadel
4 * Copyright (c) 1987-2018 by the citadel.org team
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
11 #include <sys/types.h>
19 #include "libcitadel.h"
37 void DeleteJSONValue(void *vJsonValue)
39 JsonValue *Val = (JsonValue*) vJsonValue;
40 FreeStrBuf(&Val->Name);
41 FreeStrBuf(&Val->Value);
42 DeleteHash(&Val->SubValues);
47 JsonValue *NewJsonObject(const char *Key, long keylen)
51 Ret = (JsonValue*) malloc(sizeof(JsonValue));
52 memset(Ret, 0, sizeof(JsonValue));
53 Ret->Type = JSON_OBJECT;
56 Ret->Name = NewStrBufPlain(Key, keylen);
58 Ret->SubValues = NewHash(1, NULL);
63 JsonValue *NewJsonArray(const char *Key, long keylen)
67 Ret = (JsonValue*) malloc(sizeof(JsonValue));
68 memset(Ret, 0, sizeof(JsonValue));
69 Ret->Type = JSON_ARRAY;
72 Ret->Name = NewStrBufPlain(Key, keylen);
74 Ret->SubValues = NewHash(1, lFlathash);
79 JsonValue *NewJsonNumber(const char *Key, long keylen, long Number)
83 Ret = (JsonValue*) malloc(sizeof(JsonValue));
84 memset(Ret, 0, sizeof(JsonValue));
88 Ret->Name = NewStrBufPlain(Key, keylen);
90 Ret->Value = NewStrBufPlain(NULL, 64);
91 StrBufPrintf(Ret->Value, "%ld", Number);
96 JsonValue *NewJsonBigNumber(const char *Key, long keylen, double Number)
100 Ret = (JsonValue*) malloc(sizeof(JsonValue));
101 memset(Ret, 0, sizeof(JsonValue));
102 Ret->Type = JSON_NUM;
105 Ret->Name = NewStrBufPlain(Key, keylen);
107 Ret->Value = NewStrBufPlain(NULL, 128);
108 StrBufPrintf(Ret->Value, "%f", Number);
113 JsonValue *NewJsonString(const char *Key, long keylen, StrBuf *CopyMe, int copy_or_smash)
117 Ret = (JsonValue*) malloc(sizeof(JsonValue));
118 memset(Ret, 0, sizeof(JsonValue));
119 Ret->Type = JSON_STRING;
122 Ret->Name = NewStrBufPlain(Key, keylen);
124 if (copy_or_smash == NEWJSONSTRING_COPYBUF)
126 Ret->Value = NewStrBufDup(CopyMe);
128 else if (copy_or_smash == NEWJSONSTRING_SMASHBUF)
134 Ret->Value = NULL; // error condition
140 JsonValue *NewJsonPlainString(const char *Key, long keylen, const char *CopyMe, long len)
144 Ret = (JsonValue*) malloc(sizeof(JsonValue));
145 memset(Ret, 0, sizeof(JsonValue));
146 Ret->Type = JSON_STRING;
149 Ret->Name = NewStrBufPlain(Key, keylen);
151 Ret->Value = NewStrBufPlain(CopyMe, len);
156 JsonValue *NewJsonNull(const char *Key, long keylen)
160 Ret = (JsonValue*) malloc(sizeof(JsonValue));
161 memset(Ret, 0, sizeof(JsonValue));
162 Ret->Type = JSON_NULL;
165 Ret->Name = NewStrBufPlain(Key, keylen);
167 Ret->Value = NewStrBufPlain(HKEY("nulll"));
172 JsonValue *NewJsonBool(const char *Key, long keylen, int value)
176 Ret = (JsonValue*) malloc(sizeof(JsonValue));
177 memset(Ret, 0, sizeof(JsonValue));
178 Ret->Type = JSON_BOOL;
181 Ret->Name = NewStrBufPlain(Key, keylen);
184 Ret->Value = NewStrBufPlain(HKEY("true"));
188 Ret->Value = NewStrBufPlain(HKEY("false"));
194 void JsonArrayAppend(JsonValue *Array, JsonValue *Val)
197 if (Array->Type != JSON_ARRAY)
202 n = GetCount(Array->SubValues);
203 Put(Array->SubValues, LKEY(n), Val, DeleteJSONValue);
207 void JsonObjectAppend(JsonValue *Array, JsonValue *Val)
209 if ((Array->Type != JSON_OBJECT) || (Val->Name == NULL))
213 Put(Array->SubValues, SKEY(Val->Name), Val, DeleteJSONValue);
217 void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal)
219 void *vValue, *vPrevious;
229 StrBufAppendBufPlain(Target, HKEY("\""), 0);
230 StrECMAEscAppend(Target, Val->Value, NULL);
231 StrBufAppendBufPlain(Target, HKEY("\""), 0);
234 StrBufAppendBuf(Target, Val->Value, 0);
237 StrBufAppendBuf(Target, Val->Value, 0);
240 StrBufAppendBuf(Target, Val->Value, 0);
244 StrBufAppendBufPlain(Target, HKEY("["), 0);
245 It = GetNewHashPos(Val->SubValues, 0);
246 while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue))
248 if (vPrevious != NULL)
250 StrBufAppendBufPlain(Target, HKEY(","), 0);
252 SubVal = (JsonValue*) vValue;
253 SerializeJson(Target, SubVal, 0);
256 StrBufAppendBufPlain(Target, HKEY("]"), 0);
261 StrBufAppendBufPlain(Target, HKEY("{"), 0);
262 It = GetNewHashPos(Val->SubValues, 0);
263 while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue))
265 SubVal = (JsonValue*) vValue;
267 if (vPrevious != NULL)
269 StrBufAppendBufPlain(Target, HKEY(","), 0);
271 StrBufAppendBufPlain(Target, HKEY("\""), 0);
272 StrBufAppendBuf(Target, SubVal->Name, 0);
273 StrBufAppendBufPlain(Target, HKEY("\":"), 0);
275 SerializeJson(Target, SubVal, 0);
278 StrBufAppendBufPlain(Target, HKEY("}"), 0);
284 DeleteJSONValue(Val);