src/cx/json.h

changeset 1547
12da0654e4a9
parent 1543
7b66371d6da3
equal deleted inserted replaced
1546:c8dd35f3ea53 1547:12da0654e4a9
39 #include "common.h" 39 #include "common.h"
40 #include "allocator.h" 40 #include "allocator.h"
41 #include "string.h" 41 #include "string.h"
42 #include "buffer.h" 42 #include "buffer.h"
43 #include "array_list.h" 43 #include "array_list.h"
44 #include "map.h"
44 45
45 #include <string.h> 46 #include <string.h>
46 47
47 #ifdef __cplusplus 48 #ifdef __cplusplus
48 extern "C" { 49 extern "C" {
186 /** 187 /**
187 * Type alias for the JSON array struct. 188 * Type alias for the JSON array struct.
188 */ 189 */
189 typedef struct cx_json_array_s CxJsonArray; 190 typedef struct cx_json_array_s CxJsonArray;
190 /** 191 /**
191 * Type alias for the JSON object struct. 192 * Type alias for the map representing a JSON object.
192 */ 193 * The map contains pointers of type @c CxJsonValue.
193 typedef struct cx_json_object_s CxJsonObject; 194 */
195 typedef CxMap* CxJsonObject;
194 /** 196 /**
195 * Type alias for a JSON string. 197 * Type alias for a JSON string.
196 */ 198 */
197 typedef struct cx_mutstr_s CxJsonString; 199 typedef struct cx_mutstr_s CxJsonString;
198 /** 200 /**
207 * Type alias for a JSON literal. 209 * Type alias for a JSON literal.
208 */ 210 */
209 typedef enum cx_json_literal CxJsonLiteral; 211 typedef enum cx_json_literal CxJsonLiteral;
210 212
211 /** 213 /**
212 * Type alias for a key/value pair in a JSON object.
213 */
214 typedef struct cx_json_obj_value_s CxJsonObjValue;
215
216 /**
217 * JSON array structure. 214 * JSON array structure.
218 */ 215 */
219 struct cx_json_array_s { 216 struct cx_json_array_s {
220 /** 217 /**
221 * The array data. 218 * The array data.
222 */ 219 */
223 CX_ARRAY_DECLARE(CxJsonValue*, array); 220 CX_ARRAY_DECLARE(CxJsonValue*, array);
224 };
225
226 /**
227 * JSON object structure.
228 */
229 struct cx_json_object_s {
230 /**
231 * The key/value entries.
232 */
233 CX_ARRAY_DECLARE(CxJsonObjValue, values);
234 /**
235 * The original indices to reconstruct the order in which the members were added.
236 */
237 size_t *indices;
238 };
239
240 /**
241 * Structure for a key/value entry in a JSON object.
242 */
243 struct cx_json_obj_value_s {
244 /**
245 * The key (or name in JSON terminology) of the value.
246 */
247 cxmutstr name;
248 /**
249 * The value.
250 */
251 CxJsonValue *value;
252 }; 221 };
253 222
254 /** 223 /**
255 * Structure for a JSON value. 224 * Structure for a JSON value.
256 */ 225 */
347 * Never access this value manually. 316 * Never access this value manually.
348 */ 317 */
349 CxJsonValue *parsed; 318 CxJsonValue *parsed;
350 319
351 /** 320 /**
352 * A pointer to an intermediate state of a currently parsed object member. 321 * The name of a not yet completely parsed object member.
353 * 322 *
354 * Never access this value manually. 323 * Never access this value manually.
355 */ 324 */
356 CxJsonObjValue uncompleted_member; 325 cxmutstr uncompleted_member_name;
357 326
358 /** 327 /**
359 * State stack. 328 * State stack.
360 */ 329 */
361 CX_ARRAY_DECLARE_SIZED(int, states, unsigned); 330 CX_ARRAY_DECLARE_SIZED(int, states, unsigned);
436 struct cx_json_writer_s { 405 struct cx_json_writer_s {
437 /** 406 /**
438 * Set true to enable pretty output. 407 * Set true to enable pretty output.
439 */ 408 */
440 bool pretty; 409 bool pretty;
441 /**
442 * Set false to output the members in the order in which they were added.
443 */
444 bool sort_members;
445 /** 410 /**
446 * The maximum number of fractional digits in a number value. 411 * The maximum number of fractional digits in a number value.
447 * The default value is 6 and values larger than 15 are reduced to 15. 412 * The default value is 6 and values larger than 15 are reduced to 15.
448 * Note that the actual number of digits may be lower, depending on the concrete number. 413 * Note that the actual number of digits may be lower, depending on the concrete number.
449 */ 414 */
1275 * @return the size of the object, i.e., the number of key/value pairs 1240 * @return the size of the object, i.e., the number of key/value pairs
1276 * @see cxJsonIsObject() 1241 * @see cxJsonIsObject()
1277 */ 1242 */
1278 cx_attr_nonnull 1243 cx_attr_nonnull
1279 CX_INLINE size_t cxJsonObjSize(const CxJsonValue *value) { 1244 CX_INLINE size_t cxJsonObjSize(const CxJsonValue *value) {
1280 return value->object.values_size; 1245 return cxCollectionSize(value->object);
1281 } 1246 }
1282 1247
1283 /** 1248 /**
1284 * Returns an iterator over the JSON object members. 1249 * Returns a map iterator over the JSON object members.
1285 * 1250 *
1286 * The iterator yields values of type @c CxJsonObjValue* which 1251 * The iterator yields values of type @c CxMapEntry* which
1287 * contain the name and value of the member. 1252 * contain the name and the @c CxJsonObjValue* of the member.
1288 * 1253 *
1289 * If the @p value is not a JSON object, the behavior is undefined. 1254 * If the @p value is not a JSON object, the behavior is undefined.
1290 * 1255 *
1291 * @param value the JSON value 1256 * @param value the JSON value
1292 * @return an iterator over the object members 1257 * @return an iterator over the object members
1293 * @see cxJsonIsObject() 1258 * @see cxJsonIsObject()
1294 */ 1259 */
1295 cx_attr_nonnull cx_attr_nodiscard 1260 cx_attr_nonnull cx_attr_nodiscard
1296 CX_EXPORT CxIterator cxJsonObjIter(const CxJsonValue *value); 1261 CX_EXPORT CxMapIterator cxJsonObjIter(const CxJsonValue *value);
1297 1262
1298 /** 1263 /**
1299 * Internal function, do not use. 1264 * Internal function, do not use.
1300 * @param value the JSON object 1265 * @param value the JSON object
1301 * @param name the key to look up 1266 * @param name the key to look up

mercurial