src/cx/json.h

changeset 1009
7650e722437e
parent 1007
81b2986d2b04
child 1012
21884374edbb
equal deleted inserted replaced
1008:3b69f025f083 1009:7650e722437e
292 292
293 cx_attr_nonnull 293 cx_attr_nonnull
294 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value); 294 CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value);
295 295
296 cx_attr_nonnull 296 cx_attr_nonnull
297 static inline bool cxJsonIsObject(CxJsonValue *value) { 297 static inline bool cxJsonIsObject(const CxJsonValue *value) {
298 return value->type == CX_JSON_OBJECT; 298 return value->type == CX_JSON_OBJECT;
299 } 299 }
300 300
301 cx_attr_nonnull 301 cx_attr_nonnull
302 static inline bool cxJsonIsArray(CxJsonValue *value) { 302 static inline bool cxJsonIsArray(const CxJsonValue *value) {
303 return value->type == CX_JSON_ARRAY; 303 return value->type == CX_JSON_ARRAY;
304 } 304 }
305 305
306 cx_attr_nonnull 306 cx_attr_nonnull
307 static inline bool cxJsonIsString(CxJsonValue *value) { 307 static inline bool cxJsonIsString(const CxJsonValue *value) {
308 return value->type == CX_JSON_STRING; 308 return value->type == CX_JSON_STRING;
309 } 309 }
310 310
311 cx_attr_nonnull 311 cx_attr_nonnull
312 static inline bool cxJsonIsNumber(CxJsonValue *value) { 312 static inline bool cxJsonIsNumber(const CxJsonValue *value) {
313 return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER; 313 return value->type == CX_JSON_NUMBER || value->type == CX_JSON_INTEGER;
314 } 314 }
315 315
316 cx_attr_nonnull 316 cx_attr_nonnull
317 static inline bool cxJsonIsInteger(CxJsonValue *value) { 317 static inline bool cxJsonIsInteger(const CxJsonValue *value) {
318 return value->type == CX_JSON_INTEGER; 318 return value->type == CX_JSON_INTEGER;
319 } 319 }
320 320
321 cx_attr_nonnull 321 cx_attr_nonnull
322 static inline bool cxJsonIsLiteral(CxJsonValue *value) { 322 static inline bool cxJsonIsLiteral(const CxJsonValue *value) {
323 return value->type == CX_JSON_LITERAL; 323 return value->type == CX_JSON_LITERAL;
324 } 324 }
325 325
326 cx_attr_nonnull 326 cx_attr_nonnull
327 static inline bool cxJsonIsBool(CxJsonValue *value) { 327 static inline bool cxJsonIsBool(const CxJsonValue *value) {
328 return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL; 328 return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL;
329 } 329 }
330 330
331 cx_attr_nonnull 331 cx_attr_nonnull
332 static inline bool cxJsonIsTrue(CxJsonValue *value) { 332 static inline bool cxJsonIsTrue(const CxJsonValue *value) {
333 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE; 333 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE;
334 } 334 }
335 335
336 cx_attr_nonnull 336 cx_attr_nonnull
337 static inline bool cxJsonIsFalse(CxJsonValue *value) { 337 static inline bool cxJsonIsFalse(const CxJsonValue *value) {
338 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE; 338 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE;
339 } 339 }
340 340
341 cx_attr_nonnull 341 cx_attr_nonnull
342 static inline bool cxJsonIsNull(CxJsonValue *value) { 342 static inline bool cxJsonIsNull(const CxJsonValue *value) {
343 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL; 343 return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL;
344 } 344 }
345 345
346 cx_attr_nonnull 346 cx_attr_nonnull
347 cx_attr_returns_nonnull 347 cx_attr_returns_nonnull
348 static inline char *cxJsonAsString(CxJsonValue *value) { 348 static inline char *cxJsonAsString(const CxJsonValue *value) {
349 return value->value.string.ptr; 349 return value->value.string.ptr;
350 } 350 }
351 351
352 cx_attr_nonnull 352 cx_attr_nonnull
353 static inline cxstring cxJsonAsCxString(CxJsonValue *value) { 353 static inline cxstring cxJsonAsCxString(const CxJsonValue *value) {
354 return cx_strcast(value->value.string); 354 return cx_strcast(value->value.string);
355 } 355 }
356 356
357 cx_attr_nonnull 357 cx_attr_nonnull
358 static inline cxmutstr cxJsonAsCxMutStr(CxJsonValue *value) { 358 static inline cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value) {
359 return value->value.string; 359 return value->value.string;
360 } 360 }
361 361
362 cx_attr_nonnull 362 cx_attr_nonnull
363 static inline double cxJsonAsDouble(CxJsonValue *value) { 363 static inline double cxJsonAsDouble(const CxJsonValue *value) {
364 if (value->type == CX_JSON_INTEGER) { 364 if (value->type == CX_JSON_INTEGER) {
365 return (double) value->value.integer; 365 return (double) value->value.integer;
366 } else { 366 } else {
367 return value->value.number; 367 return value->value.number;
368 } 368 }
369 } 369 }
370 370
371 cx_attr_nonnull 371 cx_attr_nonnull
372 static inline int64_t cxJsonAsInteger(CxJsonValue *value) { 372 static inline int64_t cxJsonAsInteger(const CxJsonValue *value) {
373 if (value->type == CX_JSON_INTEGER) { 373 if (value->type == CX_JSON_INTEGER) {
374 return value->value.integer; 374 return value->value.integer;
375 } else { 375 } else {
376 return (int64_t) value->value.number; 376 return (int64_t) value->value.number;
377 } 377 }
378 } 378 }
379 379
380 cx_attr_nonnull 380 cx_attr_nonnull
381 static inline bool cxJsonAsBool(CxJsonValue *value) { 381 static inline bool cxJsonAsBool(const CxJsonValue *value) {
382 return value->value.literal == CX_JSON_TRUE; 382 return value->value.literal == CX_JSON_TRUE;
383 } 383 }
384 384
385 cx_attr_nonnull 385 cx_attr_nonnull
386 static inline size_t cxJsonArrSize(CxJsonValue *value) { 386 static inline size_t cxJsonArrSize(const CxJsonValue *value) {
387 return value->value.array.array_size; 387 return value->value.array.array_size;
388 } 388 }
389 389
390 cx_attr_nonnull 390 cx_attr_nonnull
391 cx_attr_returns_nonnull 391 cx_attr_returns_nonnull
392 CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index); 392 CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index);
393 393
394 // TODO: add cxJsonArrIter() 394 // TODO: add cxJsonArrIter()
395 395
396 // TODO: implement cxJsonObjGet as a _Generic with support for cxstring 396 // TODO: implement cxJsonObjGet as a _Generic with support for cxstring
397 cx_attr_nonnull 397 cx_attr_nonnull
398 cx_attr_returns_nonnull 398 cx_attr_returns_nonnull
399 cx_attr_cstr_arg(2) 399 cx_attr_cstr_arg(2)
400 CxJsonValue *cxJsonObjGet(CxJsonValue *value, const char* name); 400 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char* name);
401 401
402 #ifdef __cplusplus 402 #ifdef __cplusplus
403 } 403 }
404 #endif 404 #endif
405 405

mercurial