Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
JSONSerializers.c File Reference
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "Boole.h"
#include "Date.h"
#include "DateFormatter.h"
#include "JSONContext.h"
#include "Array.h"
#include "Dictionary.h"
#include "Set.h"
#include "Null.h"
#include "Number.h"
#include "URL.h"

Go to the source code of this file.

Functions

bool JSONDeserializeArray (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON array into an inline array field.
 
bool JSONDeserializeBoole (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON boolean into a bool field.
 
bool JSONDeserializeCharacters (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON string into a fixed-size char[] field.
 
bool JSONDeserializeCString (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON string into a heap-allocated char * field.
 
bool JSONDeserializeDate (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON string into a Date * field (ISO 8601 by default).
 
bool JSONDeserializeDictionary (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON object into a Dictionary * field.
 
bool JSONDeserializeDouble (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON number into a double field.
 
bool JSONDeserializeFloat (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON number into a float field.
 
bool JSONDeserializeInt32 (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON number into an int32_t field.
 
bool JSONDeserializeInt64 (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON number into an int64_t field.
 
bool JSONDeserializeObjectArray (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON array into an Array * field.
 
bool JSONDeserializeSet (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON array into a Set * field.
 
bool JSONDeserializeString (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON string into a String * field.
 
bool JSONDeserializeStruct (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON object into a nested struct field.
 
bool JSONDeserializeUint32 (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON number into a uint32_t field.
 
bool JSONDeserializeUint64 (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON number into a uint64_t field.
 
bool JSONDeserializeURL (const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
 Deserializes a JSON string into a URL * field via initWithString.
 
ident JSONSerializeArray (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes an inline array field to a JSON array.
 
ident JSONSerializeBoole (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a bool field to a JSON boolean.
 
ident JSONSerializeCharacters (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a fixed-size char[] field to a JSON string.
 
ident JSONSerializeCString (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a char * heap string field to a JSON string.
 
ident JSONSerializeDate (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a Date * field to a JSON string (ISO 8601 by default).
 
ident JSONSerializeDictionary (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a Dictionary * field to a JSON object.
 
ident JSONSerializeDouble (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a double field to a JSON number.
 
ident JSONSerializeFloat (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a float field to a JSON number.
 
ident JSONSerializeInt32 (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes an int32_t field to a JSON number.
 
ident JSONSerializeInt64 (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes an int64_t field to a JSON number.
 
ident JSONSerializeObjectArray (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes an Array * field to a JSON array.
 
ident JSONSerializeSet (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a Set * field to a JSON array.
 
ident JSONSerializeString (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a String * field to a JSON string.
 
ident JSONSerializeStruct (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a nested struct field to a JSON object.
 
ident JSONSerializeUint32 (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a uint32_t field to a JSON number.
 
ident JSONSerializeUint64 (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a uint64_t field to a JSON number.
 
ident JSONSerializeURL (const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
 Serializes a URL * field to a JSON string (the URL's string form).
 

Function Documentation

◆ JSONDeserializeArray()

bool JSONDeserializeArray ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON array into an inline array field.

property->data must point to a JSONArrayProperties.

Returns
false if the JSON value is not an Array.

Definition at line 501 of file JSONSerializers.c.

505 {
506
507 (void) properties;
508
509 const JSONArrayProperties *array = property->data;
510 if (!array || !array->properties || !field) {
511 return false;
512 }
513
514 uint8_t *instance = (uint8_t *) field - property->offset;
516 *(size_t *) (instance + array->count) = 0;
517 }
518
519 memset(field, 0, array->properties->size * array->capacity);
520
521 if (!$(value, isKindOfClass, _Array())) {
522 return false;
523 }
524
525 const size_t n = $(context, structsFromArray, array->properties,
526 cast(Array, value), field, array->capacity);
527
529 *(size_t *) (instance + array->count) = n;
530 }
531
532 return true;
533}
Class * _Array(void)
Definition Array.c:760
static Array * array(void)
Definition Array.c:234
#define cast(type, obj)
Safely cast obj to type.
Definition Class.h:182
static size_t structsFromArray(JSONContext *self, const JSONProperties *properties, const Array *array, ident instances, size_t count)
Deserializes a JSON Array into an array of C structs.
#define JSONArrayProperties_NoCount
Sentinel for JSONArrayProperties::count indicating no sibling count field.
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Array.h:72
Describes the JSON binding for an inline array field of a C struct.
ptrdiff_t offset
The byte offset of the field within the struct.

◆ JSONDeserializeBoole()

bool JSONDeserializeBoole ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON boolean into a bool field.

Returns
false if the JSON value is not a Boole.

Definition at line 454 of file JSONSerializers.c.

458 {
459
460 (void) properties;
461 (void) property;
462 (void) context;
463
464 bool *dest = field;
465 if (!dest) {
466 return false;
467 }
468
469 *dest = false;
470
471 if (!$(value, isKindOfClass, _Boole())) {
472 return false;
473 }
474
475 *dest = cast(Boole, value)->value;
476 return true;
477}
Class * _Boole(void)
Definition Boole.c:125
A wrapper for placing boolean primitives into collections, etc.
Definition Boole.h:41

◆ JSONDeserializeCharacters()

bool JSONDeserializeCharacters ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON string into a fixed-size char[] field.

property->data must carry the field size via JSONFieldSize.

Returns
false if the JSON value is not a String.

Definition at line 251 of file JSONSerializers.c.

255 {
256
257 (void) properties;
258 (void) context;
259
260 char *dest = field;
261 const size_t size = property->size;
262
263 if (!dest || !size) {
264 return false;
265 }
266
267 memset(dest, 0, size);
268
269 if (!$(value, isKindOfClass, _String())) {
270 return false;
271 }
272
273 strncpy(dest, cast(String, value)->chars, size - 1);
274 dest[size - 1] = '\0';
275 return true;
276}
Class * _String(void)
Definition String.c:999
UTF-8 strings.
Definition String.h:69

◆ JSONDeserializeCString()

bool JSONDeserializeCString ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON string into a heap-allocated char * field.

The existing pointer is freed before replacement.

Returns
false if the JSON value is not a String.

Definition at line 278 of file JSONSerializers.c.

282 {
283
284 (void) properties;
285 (void) property;
286 (void) context;
287
288 char **dest = field;
289 if (!dest) {
290 return false;
291 }
292
293 free(*dest);
294 *dest = NULL;
295
296 if (!$(value, isKindOfClass, _String())) {
297 return false;
298 }
299
300 *dest = strdup(cast(String, value)->chars);
301 return true;
302}

◆ JSONDeserializeDate()

bool JSONDeserializeDate ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON string into a Date * field (ISO 8601 by default).

Pass a const char * format string as property->data to override the format.

Returns
false if the JSON value is not a String or cannot be parsed.

Definition at line 632 of file JSONSerializers.c.

636 {
637
638 (void) properties;
639 (void) context;
640
641 Date **dest = field;
642 release(*dest);
643 *dest = NULL;
644
645 if (!$(value, isKindOfClass, _String())) {
646 return false;
647 }
648
649 const char *fmt = property->data ? (const char *) property->data : DATEFORMAT_ISO8601;
650 DateFormatter *formatter = $(alloc(DateFormatter), initWithFormat, fmt);
651 *dest = $(formatter, dateFromString, cast(String, value));
652 release(formatter);
653 return *dest != NULL;
654}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static Date * dateFromString(const DateFormatter *self, const String *string)
static DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
#define DATEFORMAT_ISO8601
ISO8601 date format.
Date formatting and parsing.
Microsecond-precision immutable dates.
Definition Date.h:74
ident data
Opaque user data passed to the serializer and deserializer.

◆ JSONDeserializeDictionary()

bool JSONDeserializeDictionary ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON object into a Dictionary * field.

The existing Dictionary is released before replacement.

Returns
false if the JSON value is not a Dictionary.

Definition at line 753 of file JSONSerializers.c.

757 {
758
759 (void) properties;
760 (void) property;
761 (void) context;
762
763 Dictionary **dest = field;
764 release(*dest);
765 *dest = NULL;
766
767 if (!$(value, isKindOfClass, _Dictionary())) {
768 return false;
769 }
770
771 *dest = $(alloc(Dictionary), init);
772 $(*(Dictionary **) dest, addEntriesFromDictionary, cast(Dictionary, value));
773 return true;
774}
static Array * init(Array *self)
Definition Array.c:420
Class * _Dictionary(void)
Definition Dictionary.c:762
static void addEntriesFromDictionary(Dictionary *self, const Dictionary *dictionary)
Definition Dictionary.c:175
Key-value stores.
Definition Dictionary.h:60

◆ JSONDeserializeDouble()

bool JSONDeserializeDouble ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON number into a double field.

Returns
false if the JSON value is not a Number.

Definition at line 429 of file JSONSerializers.c.

433 {
434
435 (void) properties;
436 (void) property;
437 (void) context;
438
439 double *dest = field;
440 if (!dest) {
441 return false;
442 }
443
444 *dest = 0.0;
445
446 if (!$(value, isKindOfClass, _Number())) {
447 return false;
448 }
449
450 *dest = cast(Number, value)->value;
451 return true;
452}
Class * _Number(void)
Definition Number.c:198
A wrapper for placing numeric primitives into collections, etc.
Definition Number.h:41

◆ JSONDeserializeFloat()

bool JSONDeserializeFloat ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON number into a float field.

Returns
false if the JSON value is not a Number.

Definition at line 404 of file JSONSerializers.c.

408 {
409
410 (void) properties;
411 (void) property;
412 (void) context;
413
414 float *dest = field;
415 if (!dest) {
416 return false;
417 }
418
419 *dest = 0.f;
420
421 if (!$(value, isKindOfClass, _Number())) {
422 return false;
423 }
424
425 *dest = (float) cast(Number, value)->value;
426 return true;
427}

◆ JSONDeserializeInt32()

bool JSONDeserializeInt32 ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON number into an int32_t field.

Returns
false if the JSON value is not a Number.

Definition at line 304 of file JSONSerializers.c.

308 {
309
310 (void) properties;
311 (void) property;
312 (void) context;
313
314 int32_t *dest = field;
315 if (!dest) {
316 return false;
317 }
318
319 *dest = 0;
320
321 if (!$(value, isKindOfClass, _Number())) {
322 return false;
323 }
324
325 *dest = (int32_t) cast(Number, value)->value;
326 return true;
327}

◆ JSONDeserializeInt64()

bool JSONDeserializeInt64 ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON number into an int64_t field.

JSON numbers are IEEE 754 doubles (53-bit mantissa). Values beyond 2^53 will lose precision on round-trip.

Returns
false if the JSON value is not a Number.

Definition at line 354 of file JSONSerializers.c.

358 {
359
360 (void) properties;
361 (void) property;
362 (void) context;
363
364 int64_t *dest = field;
365 if (!dest) {
366 return false;
367 }
368
369 *dest = 0;
370
371 if (!$(value, isKindOfClass, _Number())) {
372 return false;
373 }
374
375 *dest = (int64_t) cast(Number, value)->value;
376 return true;
377}

◆ JSONDeserializeObjectArray()

bool JSONDeserializeObjectArray ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON array into an Array * field.

The existing Array is released before replacement. Elements are retained as their parsed Objectively types (String, Number, Boole, etc.).

Returns
false if the JSON value is not an Array.

Definition at line 671 of file JSONSerializers.c.

675 {
676
677 (void) properties;
678 (void) property;
679 (void) context;
680
681 Array **dest = field;
682 release(*dest);
683 *dest = NULL;
684
685 if (!$(value, isKindOfClass, _Array())) {
686 return false;
687 }
688
689 const Array *src = cast(Array, value);
690 *dest = $(alloc(Array), initWithCapacity, src->count);
691 $(*(Array **) dest, addObjectsFromArray, src);
692 return true;
693}
static Array * initWithCapacity(Array *self, size_t capacity)
Definition Array.c:453
static void addObjectsFromArray(Array *self, const Array *array)
Definition Array.c:221

◆ JSONDeserializeSet()

bool JSONDeserializeSet ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON array into a Set * field.

The existing Set is released before replacement.

Returns
false if the JSON value is not an Array.

Definition at line 714 of file JSONSerializers.c.

718 {
719
720 (void) properties;
721 (void) property;
722 (void) context;
723
724 Set **dest = field;
725 release(*dest);
726 *dest = NULL;
727
728 if (!$(value, isKindOfClass, _Array())) {
729 return false;
730 }
731
732 const Array *src = cast(Array, value);
733 *dest = $(alloc(Set), initWithCapacity, src->count);
734 $(*(Set **) dest, addObjectsFromArray, src);
735 return true;
736}
Sets.
Definition Set.h:55

◆ JSONDeserializeString()

bool JSONDeserializeString ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON string into a String * field.

The existing String is released before replacement.

Returns
false if the JSON value is not a String.

Definition at line 552 of file JSONSerializers.c.

556 {
557
558 (void) properties;
559 (void) property;
560 (void) context;
561
562 String **dest = field;
563 release(*dest);
564 *dest = NULL;
565
566 if (!$(value, isKindOfClass, _String())) {
567 return false;
568 }
569
570 *dest = retain((String *) value);
571 return true;
572}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210

◆ JSONDeserializeStruct()

bool JSONDeserializeStruct ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON object into a nested struct field.

property->data must point to a JSONProperties describing the nested type.

Returns
false if the JSON value is not a Dictionary.

Definition at line 479 of file JSONSerializers.c.

483 {
484
485 (void) properties;
486
487 const JSONProperties *child = property->data;
488 if (!child || !child->properties || !field) {
489 return false;
490 }
491
492 memset(field, 0, child->size);
493
494 if (!$(value, isKindOfClass, _Dictionary())) {
495 return false;
496 }
497
498 return $(context, structFromDictionary, child, cast(Dictionary, value), field);
499}
static bool structFromDictionary(JSONContext *self, const JSONProperties *properties, const Dictionary *dictionary, ident instance)
Deserializes a Dictionary into a C struct.
Describes the JSON binding for all fields of a C struct.
const JSONProperty * properties
The NULL-terminated JSONProperty array.
size_t size
The struct size, i.e. sizeof(Struct).

◆ JSONDeserializeUint32()

bool JSONDeserializeUint32 ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON number into a uint32_t field.

Returns
false if the JSON value is not a Number.

Definition at line 329 of file JSONSerializers.c.

333 {
334
335 (void) properties;
336 (void) property;
337 (void) context;
338
339 uint32_t *dest = field;
340 if (!dest) {
341 return false;
342 }
343
344 *dest = 0;
345
346 if (!$(value, isKindOfClass, _Number())) {
347 return false;
348 }
349
350 *dest = (uint32_t) cast(Number, value)->value;
351 return true;
352}

◆ JSONDeserializeUint64()

bool JSONDeserializeUint64 ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON number into a uint64_t field.

JSON numbers are IEEE 754 doubles (53-bit mantissa). Values beyond 2^53 will lose precision on round-trip.

Returns
false if the JSON value is not a Number.

Definition at line 379 of file JSONSerializers.c.

383 {
384
385 (void) properties;
386 (void) property;
387 (void) context;
388
389 uint64_t *dest = field;
390 if (!dest) {
391 return false;
392 }
393
394 *dest = 0;
395
396 if (!$(value, isKindOfClass, _Number())) {
397 return false;
398 }
399
400 *dest = (uint64_t) cast(Number, value)->value;
401 return true;
402}

◆ JSONDeserializeURL()

bool JSONDeserializeURL ( const JSONProperties properties,
const JSONProperty property,
const Object value,
ident  field,
JSONContext context 
)

Deserializes a JSON string into a URL * field via initWithString.

The existing URL is released before replacement.

Returns
false if the JSON value is not a String.

Definition at line 589 of file JSONSerializers.c.

593 {
594
595 (void) properties;
596 (void) property;
597 (void) context;
598
599 URL **dest = field;
600 release(*dest);
601 *dest = NULL;
602
603 if (!$(value, isKindOfClass, _String())) {
604 return false;
605 }
606
607 *dest = $(alloc(URL), initWithString, cast(String, value));
608 return *dest != NULL;
609}
static String * initWithString(String *self, const String *string)
Definition String.c:716
Uniform Resource Locators (RFC 3986).
Definition URL.h:44

◆ JSONSerializeArray()

ident JSONSerializeArray ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes an inline array field to a JSON array.

property->data must point to a JSONArrayProperties.

Definition at line 223 of file JSONSerializers.c.

227 {
228
229 (void) properties;
230 (void) property;
231
233 if (!array || !array->properties || !value) {
234 return NULL;
235 }
236
237 Array *out = $(alloc(Array), init);
238
239 for (size_t i = 0; i < array->capacity; i++) {
240 const ident instance = (uint8_t *) value + i * array->properties->size;
241 Dictionary *dict = $(context, dictionaryFromStruct, array->properties, instance);
242 $(out, addObject, dict);
243 release(dict);
244 }
245
246 return (ident) out;
247}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
static Data * data(void)
Definition Data.c:286
static Dictionary * dictionaryFromStruct(JSONContext *self, const JSONProperties *properties, const ident instance)
Serializes a C struct instance to a Dictionary.
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49

◆ JSONSerializeBoole()

ident JSONSerializeBoole ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a bool field to a JSON boolean.

Definition at line 188 of file JSONSerializers.c.

192 {
193
194 (void) properties;
195 (void) property;
196 (void) data;
197 (void) context;
198
199 if (!value) {
200 return NULL;
201 }
202
203 return retain(*(const bool *) value ? $$(Boole, True) : $$(Boole, False));
204}
static Boole * True(void)
Definition Boole.c:77
static Boole * False(void)
Definition Boole.c:59

◆ JSONSerializeCharacters()

ident JSONSerializeCharacters ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a fixed-size char[] field to a JSON string.

Omits the key (returns NULL) when the field is empty.

Definition at line 42 of file JSONSerializers.c.

46 {
47
48 (void) properties;
49 (void) property;
50 (void) data;
51 (void) context;
52
53 const char *s = value;
54 if (s && s[0]) {
55 return (ident) $$(String, stringWithCharacters, s);
56 }
57
58 return NULL;
59}
static String * stringWithCharacters(const char *chars)
Definition String.c:349

◆ JSONSerializeCString()

ident JSONSerializeCString ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a char * heap string field to a JSON string.

Omits the key (returns NULL) when the pointer is NULL.

Definition at line 61 of file JSONSerializers.c.

65 {
66
67 (void) properties;
68 (void) property;
69 (void) data;
70 (void) context;
71
72 const char *const *s = value;
73 if (s && *s) {
74 return (ident) $$(String, stringWithCharacters, *s);
75 }
76
77 return NULL;
78}

◆ JSONSerializeDate()

ident JSONSerializeDate ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a Date * field to a JSON string (ISO 8601 by default).

Pass a const char * format string as property->data to override the format.

Definition at line 611 of file JSONSerializers.c.

615 {
616
617 (void) properties;
618 (void) context;
619
620 Date **date = value;
621 if (!*date) {
622 return NULL;
623 }
624
625 const char *fmt = data ? (const char *) data : DATEFORMAT_ISO8601;
626 DateFormatter *formatter = $(alloc(DateFormatter), initWithFormat, fmt);
627 String *string = $(formatter, stringFromDate, *date);
628 release(formatter);
629 return string;
630}
static Date * date(void)
Definition Date.c:98
static String * stringFromDate(const DateFormatter *self, const Date *date)
static String * string(void)
Definition String.c:905

◆ JSONSerializeDictionary()

ident JSONSerializeDictionary ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a Dictionary * field to a JSON object.

Values are written as-is; each must be a JSON-compatible Objectively type.

Definition at line 738 of file JSONSerializers.c.

742 {
743
744 (void) properties;
745 (void) property;
746 (void) data;
747 (void) context;
748
749 Dictionary **dict = value;
750 return *dict ? retain(*dict) : NULL;
751}

◆ JSONSerializeDouble()

ident JSONSerializeDouble ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a double field to a JSON number.

Definition at line 170 of file JSONSerializers.c.

174 {
175
176 (void) properties;
177 (void) property;
178 (void) data;
179 (void) context;
180
181 if (!value) {
182 return NULL;
183 }
184
185 return (ident) $$(Number, numberWithValue, *(const double *) value);
186}
static Number * numberWithValue(double value)
Definition Number.c:159

◆ JSONSerializeFloat()

ident JSONSerializeFloat ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a float field to a JSON number.

Definition at line 152 of file JSONSerializers.c.

156 {
157
158 (void) properties;
159 (void) property;
160 (void) data;
161 (void) context;
162
163 if (!value) {
164 return NULL;
165 }
166
167 return (ident) $$(Number, numberWithValue, (double) *(const float *) value);
168}

◆ JSONSerializeInt32()

ident JSONSerializeInt32 ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes an int32_t field to a JSON number.

Definition at line 80 of file JSONSerializers.c.

84 {
85
86 (void) properties;
87 (void) property;
88 (void) data;
89 (void) context;
90
91 if (!value) {
92 return NULL;
93 }
94
95 return (ident) $$(Number, numberWithValue, (double) *(const int32_t *) value);
96}

◆ JSONSerializeInt64()

ident JSONSerializeInt64 ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes an int64_t field to a JSON number.

JSON numbers are IEEE 754 doubles (53-bit mantissa). Values beyond 2^53 will lose precision on round-trip.

Definition at line 116 of file JSONSerializers.c.

120 {
121
122 (void) properties;
123 (void) property;
124 (void) data;
125 (void) context;
126
127 if (!value) {
128 return NULL;
129 }
130
131 return (ident) $$(Number, numberWithValue, (double) *(const int64_t *) value);
132}

◆ JSONSerializeObjectArray()

ident JSONSerializeObjectArray ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes an Array * field to a JSON array.

Elements are written as-is; each must be a JSON-compatible Objectively type.

Definition at line 656 of file JSONSerializers.c.

660 {
661
662 (void) properties;
663 (void) property;
664 (void) data;
665 (void) context;
666
667 Array **arr = value;
668 return *arr ? retain(*arr) : NULL;
669}

◆ JSONSerializeSet()

ident JSONSerializeSet ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a Set * field to a JSON array.

Elements are written as-is; each must be a JSON-compatible Objectively type.

Definition at line 695 of file JSONSerializers.c.

699 {
700
701 (void) properties;
702 (void) property;
703 (void) data;
704 (void) context;
705
706 Set **set = value;
707 if (!*set) {
708 return NULL;
709 }
710
711 return (ident) $(cast(Set, *set), allObjects);
712}
static Array * allObjects(const Dictionary *self)
Definition Dictionary.c:213
static Set * set(void)
Definition Set.c:572

◆ JSONSerializeString()

ident JSONSerializeString ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a String * field to a JSON string.

Definition at line 537 of file JSONSerializers.c.

541 {
542
543 (void) properties;
544 (void) property;
545 (void) data;
546 (void) context;
547
548 String **s = value;
549 return *s ? retain(*s) : NULL;
550}

◆ JSONSerializeStruct()

ident JSONSerializeStruct ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a nested struct field to a JSON object.

property->data must point to a JSONProperties describing the nested type.

Definition at line 206 of file JSONSerializers.c.

210 {
211
212 (void) properties;
213 (void) property;
214
215 const JSONProperties *child = data;
216 if (!child || !child->properties || !value) {
217 return NULL;
218 }
219
220 return (ident) $(context, dictionaryFromStruct, child, value);
221}

◆ JSONSerializeUint32()

ident JSONSerializeUint32 ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a uint32_t field to a JSON number.

Definition at line 98 of file JSONSerializers.c.

102 {
103
104 (void) properties;
105 (void) property;
106 (void) data;
107 (void) context;
108
109 if (!value) {
110 return NULL;
111 }
112
113 return (ident) $$(Number, numberWithValue, (double) *(const uint32_t *) value);
114}

◆ JSONSerializeUint64()

ident JSONSerializeUint64 ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a uint64_t field to a JSON number.

JSON numbers are IEEE 754 doubles (53-bit mantissa). Values beyond 2^53 will lose precision on round-trip.

Definition at line 134 of file JSONSerializers.c.

138 {
139
140 (void) properties;
141 (void) property;
142 (void) data;
143 (void) context;
144
145 if (!value) {
146 return NULL;
147 }
148
149 return (ident) $$(Number, numberWithValue, (double) *(const uint64_t *) value);
150}

◆ JSONSerializeURL()

ident JSONSerializeURL ( const JSONProperties properties,
const JSONProperty property,
ident  value,
ident  data,
JSONContext context 
)

Serializes a URL * field to a JSON string (the URL's string form).

Definition at line 574 of file JSONSerializers.c.

578 {
579
580 (void) properties;
581 (void) property;
582 (void) data;
583 (void) context;
584
585 URL **url = value;
586 return *url ? retain((*url)->urlString) : NULL;
587}