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)
128 Ret = (JsonValue*) malloc(sizeof(JsonValue));
129 memset(Ret, 0, sizeof(JsonValue));
130 Ret->Type = JSON_STRING;
133 Ret->Name = NewStrBufPlain(Key, keylen);
135 Ret->Value = NewStrBufDup(CopyMe);
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);