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; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
30 #include "libcitadel.h"
48 void DeleteJSONValue(void *vJsonValue)
50 JsonValue *Val = (JsonValue*) vJsonValue;
51 FreeStrBuf(&Val->Name);
52 FreeStrBuf(&Val->Value);
53 DeleteHash(&Val->SubValues);
58 JsonValue *NewJsonObject(const char *Key, long keylen)
62 Ret = (JsonValue*) malloc(sizeof(JsonValue));
63 memset(Ret, 0, sizeof(JsonValue));
64 Ret->Type = JSON_OBJECT;
67 Ret->Name = NewStrBufPlain(Key, keylen);
69 Ret->SubValues = NewHash(1, NULL);
74 JsonValue *NewJsonArray(const char *Key, long keylen)
78 Ret = (JsonValue*) malloc(sizeof(JsonValue));
79 memset(Ret, 0, sizeof(JsonValue));
80 Ret->Type = JSON_ARRAY;
83 Ret->Name = NewStrBufPlain(Key, keylen);
85 Ret->SubValues = NewHash(1, lFlathash);
90 JsonValue *NewJsonNumber(const char *Key, long keylen, long Number)
94 Ret = (JsonValue*) malloc(sizeof(JsonValue));
95 memset(Ret, 0, sizeof(JsonValue));
99 Ret->Name = NewStrBufPlain(Key, keylen);
101 Ret->Value = NewStrBufPlain(NULL, 64);
102 StrBufPrintf(Ret->Value, "%ld", Number);
107 JsonValue *NewJsonBigNumber(const char *Key, long keylen, double Number)
111 Ret = (JsonValue*) malloc(sizeof(JsonValue));
112 memset(Ret, 0, sizeof(JsonValue));
113 Ret->Type = JSON_NUM;
116 Ret->Name = NewStrBufPlain(Key, keylen);
118 Ret->Value = NewStrBufPlain(NULL, 128);
119 StrBufPrintf(Ret->Value, "%f", Number);
124 JsonValue *NewJsonString(const char *Key, long keylen, StrBuf *CopyMe, int copy_or_smash)
128 Ret = (JsonValue*) malloc(sizeof(JsonValue));
129 memset(Ret, 0, sizeof(JsonValue));
130 Ret->Type = JSON_STRING;
133 Ret->Name = NewStrBufPlain(Key, keylen);
135 if (copy_or_smash == NEWJSONSTRING_COPYBUF)
137 Ret->Value = NewStrBufDup(CopyMe);
139 else if (copy_or_smash == NEWJSONSTRING_SMASHBUF)
145 Ret->Value = NULL; // error condition
151 JsonValue *NewJsonPlainString(const char *Key, long keylen, const char *CopyMe, long len)
155 Ret = (JsonValue*) malloc(sizeof(JsonValue));
156 memset(Ret, 0, sizeof(JsonValue));
157 Ret->Type = JSON_STRING;
160 Ret->Name = NewStrBufPlain(Key, keylen);
162 Ret->Value = NewStrBufPlain(CopyMe, len);
167 JsonValue *NewJsonNull(const char *Key, long keylen)
171 Ret = (JsonValue*) malloc(sizeof(JsonValue));
172 memset(Ret, 0, sizeof(JsonValue));
173 Ret->Type = JSON_NULL;
176 Ret->Name = NewStrBufPlain(Key, keylen);
178 Ret->Value = NewStrBufPlain(HKEY("nulll"));
183 JsonValue *NewJsonBool(const char *Key, long keylen, int value)
187 Ret = (JsonValue*) malloc(sizeof(JsonValue));
188 memset(Ret, 0, sizeof(JsonValue));
189 Ret->Type = JSON_BOOL;
192 Ret->Name = NewStrBufPlain(Key, keylen);
195 Ret->Value = NewStrBufPlain(HKEY("true"));
199 Ret->Value = NewStrBufPlain(HKEY("false"));
205 void JsonArrayAppend(JsonValue *Array, JsonValue *Val)
208 if (Array->Type != JSON_ARRAY)
213 n = GetCount(Array->SubValues);
214 Put(Array->SubValues, LKEY(n), Val, DeleteJSONValue);
218 void JsonObjectAppend(JsonValue *Array, JsonValue *Val)
220 if ((Array->Type != JSON_OBJECT) || (Val->Name == NULL))
224 Put(Array->SubValues, SKEY(Val->Name), Val, DeleteJSONValue);
228 void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal)
230 void *vValue, *vPrevious;
240 StrBufAppendBufPlain(Target, HKEY("\""), 0);
241 StrECMAEscAppend(Target, Val->Value, NULL);
242 StrBufAppendBufPlain(Target, HKEY("\""), 0);
245 StrBufAppendBuf(Target, Val->Value, 0);
248 StrBufAppendBuf(Target, Val->Value, 0);
251 StrBufAppendBuf(Target, Val->Value, 0);
255 StrBufAppendBufPlain(Target, HKEY("["), 0);
256 It = GetNewHashPos(Val->SubValues, 0);
257 while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue))
259 if (vPrevious != NULL)
261 StrBufAppendBufPlain(Target, HKEY(","), 0);
263 SubVal = (JsonValue*) vValue;
264 SerializeJson(Target, SubVal, 0);
267 StrBufAppendBufPlain(Target, HKEY("]"), 0);
272 StrBufAppendBufPlain(Target, HKEY("{"), 0);
273 It = GetNewHashPos(Val->SubValues, 0);
274 while (GetNextHashPos(Val->SubValues, It, &keylen, &Key, &vValue))
276 SubVal = (JsonValue*) vValue;
278 if (vPrevious != NULL)
280 StrBufAppendBufPlain(Target, HKEY(","), 0);
282 StrBufAppendBufPlain(Target, HKEY("\""), 0);
283 StrBufAppendBuf(Target, SubVal->Name, 0);
284 StrBufAppendBufPlain(Target, HKEY("\":"), 0);
286 SerializeJson(Target, SubVal, 0);
289 StrBufAppendBufPlain(Target, HKEY("}"), 0);
295 DeleteJSONValue(Val);