2 * \defgroup Subst Variable substitution type stuff
3 * \ingroup CitadelConfig
5 * Copyright (c) 1987-2011 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
34 #include "libcitadel.h"
52 void DeleteJSONValue(void *vJsonValue)
54 JsonValue *Val = (JsonValue*) vJsonValue;
55 FreeStrBuf(&Val->Name);
56 FreeStrBuf(&Val->Value);
57 DeleteHash(&Val->SubValues);
61 JsonValue *NewJsonObject(const char *Key, long keylen)
65 Ret = (JsonValue*) malloc(sizeof(JsonValue));
66 memset(Ret, 0, sizeof(JsonValue));
67 Ret->Type = JSON_OBJECT;
69 Ret->Name = NewStrBufPlain(Key, keylen);
70 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;
82 Ret->Name = NewStrBufPlain(Key, keylen);
83 Ret->SubValues = NewHash(1, Flathash);
88 JsonValue *NewJsonNumber(const char *Key, long keylen, long Number)
92 Ret = (JsonValue*) malloc(sizeof(JsonValue));
93 memset(Ret, 0, sizeof(JsonValue));
96 Ret->Name = NewStrBufPlain(Key, keylen);
97 Ret->Value = NewStrBufPlain(NULL, 64);
98 StrBufPrintf(Ret->Value, "%ld", Number);
104 JsonValue *NewJsonBigNumber(const char *Key, long keylen, double Number)
108 Ret = (JsonValue*) malloc(sizeof(JsonValue));
109 memset(Ret, 0, sizeof(JsonValue));
110 Ret->Type = JSON_NUM;
112 Ret->Name = NewStrBufPlain(Key, keylen);
113 Ret->Value = NewStrBufPlain(NULL, 128);
114 StrBufPrintf(Ret->Value, "%f", Number);
118 JsonValue *NewJsonString(const char *Key, long keylen, StrBuf *CopyMe)
122 Ret = (JsonValue*) malloc(sizeof(JsonValue));
123 memset(Ret, 0, sizeof(JsonValue));
124 Ret->Type = JSON_STRING;
126 Ret->Name = NewStrBufPlain(Key, keylen);
127 Ret->Value = NewStrBufDup(CopyMe);
131 JsonValue *NewJsonPlainString(const char *Key, long keylen, const char *CopyMe, long len)
135 Ret = (JsonValue*) malloc(sizeof(JsonValue));
136 memset(Ret, 0, sizeof(JsonValue));
137 Ret->Type = JSON_STRING;
139 Ret->Name = NewStrBufPlain(Key, keylen);
140 Ret->Value = NewStrBufPlain(CopyMe, len);
144 JsonValue *NewJsonNull(const char *Key, long keylen)
148 Ret = (JsonValue*) malloc(sizeof(JsonValue));
149 memset(Ret, 0, sizeof(JsonValue));
150 Ret->Type = JSON_NULL;
152 Ret->Name = NewStrBufPlain(Key, keylen);
153 Ret->Value = NewStrBufPlain(HKEY("nulll"));
157 JsonValue *NewJsonBool(const char *Key, long keylen, int value)
161 Ret = (JsonValue*) malloc(sizeof(JsonValue));
162 memset(Ret, 0, sizeof(JsonValue));
163 Ret->Type = JSON_BOOL;
165 Ret->Name = NewStrBufPlain(Key, keylen);
167 Ret->Value = NewStrBufPlain(HKEY("true"));
169 Ret->Value = NewStrBufPlain(HKEY("false"));
173 void JsonArrayAppend(JsonValue *Array, JsonValue *Val)
176 if (Array->Type != JSON_ARRAY)
177 return; /* todo assert! */
179 n = GetCount(Array->SubValues);
180 Put(Array->SubValues, (const char*) &n, sizeof(n), Val, DeleteJSONValue);
183 void JsonObjectAppend(JsonValue *Array, JsonValue *Val)
185 if ((Array->Type != JSON_OBJECT) || (Val->Name == NULL))
186 return; /* todo assert! */
188 Put(Array->SubValues, SKEY(Val->Name), Val, DeleteJSONValue);
195 void SerializeJson(StrBuf *Target, JsonValue *Val, int FreeVal)
197 void *vValue, *vPrevious;
206 StrBufAppendBufPlain(Target, HKEY("\""), 0);
207 StrECMAEscAppend(Target, Val->Value, NULL);
208 StrBufAppendBufPlain(Target, HKEY("\""), 0);
211 StrBufAppendBuf(Target, Val->Value, 0);
214 StrBufAppendBuf(Target, Val->Value, 0);
217 StrBufAppendBuf(Target, Val->Value, 0);
221 StrBufAppendBufPlain(Target, HKEY("["), 0);
222 It = GetNewHashPos(Val->SubValues, 0);
223 while (GetNextHashPos(Val->SubValues,
227 if (vPrevious != NULL)
228 StrBufAppendBufPlain(Target, HKEY(","), 0);
230 SubVal = (JsonValue*) vValue;
231 SerializeJson(Target, SubVal, 0);
234 StrBufAppendBufPlain(Target, HKEY("]"), 0);
239 StrBufAppendBufPlain(Target, HKEY("{"), 0);
240 It = GetNewHashPos(Val->SubValues, 0);
241 while (GetNextHashPos(Val->SubValues,
245 SubVal = (JsonValue*) vValue;
247 if (vPrevious != NULL) {
248 StrBufAppendBufPlain(Target, HKEY(","), 0);
250 StrBufAppendBufPlain(Target, HKEY("\""), 0);
251 StrBufAppendBuf(Target, SubVal->Name, 0);
252 StrBufAppendBufPlain(Target, HKEY("\":"), 0);
254 SerializeJson(Target, SubVal, 0);
257 StrBufAppendBufPlain(Target, HKEY("}"), 0);
262 DeleteJSONValue(Val);