Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
JSONSerializers.h
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#pragma once
25
26#include <stddef.h>
27#include <stdint.h>
28
29#include <Objectively/Array.h>
30#include <Objectively/Date.h>
32#include <Objectively/Set.h>
33#include <Objectively/Object.h>
34#include <Objectively/URL.h>
35
46typedef struct JSONProperties JSONProperties;
47typedef struct JSONProperty JSONProperty;
48typedef struct JSONContext JSONContext;
49
59typedef ident (*JSONSerializer)(const JSONProperties *properties,
60 const JSONProperty *property,
61 ident value,
62 ident data,
63 JSONContext *context);
64
74typedef bool (*JSONDeserializer)(const JSONProperties *properties,
75 const JSONProperty *property,
76 const Object *value,
77 ident field,
78 JSONContext *context);
79
84
88 const char *key;
89
93 ptrdiff_t offset;
94
98 size_t size;
99
104
109
115};
116
121
125 const char *name;
126
130 size_t size;
131
136};
137
142#define MakeJSONProperty(Struct, field, serializer_, deserializer_, data_) \
143 (JSONProperty) { \
144 .key = #field, \
145 .offset = (ptrdiff_t) offsetof(Struct, field), \
146 .size = sizeof(((Struct *) NULL)->field), \
147 .serializer = (serializer_), \
148 .deserializer = (deserializer_), \
149 .data = (data_) \
150 }
151
155#define MakeJSONProperties(Struct, ...) \
156 (JSONProperties) { \
157 .name = #Struct, \
158 .size = sizeof(Struct), \
159 .properties = (const JSONProperty[]) { \
160 __VA_ARGS__, \
161 (JSONProperty) { .key = NULL } \
162 } \
163 }
164
165#pragma mark - Standard JSONSerializers
166
172 const JSONProperty *property,
173 ident value,
174 ident data,
175 JSONContext *context);
176
182 const JSONProperty *property,
183 ident value,
184 ident data,
185 JSONContext *context);
186
191 const JSONProperty *property,
192 ident value,
193 ident data,
194 JSONContext *context);
195
200 const JSONProperty *property,
201 ident value,
202 ident data,
203 JSONContext *context);
204
211 const JSONProperty *property,
212 ident value,
213 ident data,
214 JSONContext *context);
215
222 const JSONProperty *property,
223 ident value,
224 ident data,
225 JSONContext *context);
226
231 const JSONProperty *property,
232 ident value,
233 ident data,
234 JSONContext *context);
235
240 const JSONProperty *property,
241 ident value,
242 ident data,
243 JSONContext *context);
244
249 const JSONProperty *property,
250 ident value,
251 ident data,
252 JSONContext *context);
253
259 const JSONProperty *property,
260 ident value,
261 ident data,
262 JSONContext *context);
263
269 const JSONProperty *property,
270 ident value,
271 ident data,
272 JSONContext *context);
273
274#pragma mark - Standard JSONDeserializers
275
282 const JSONProperty *property,
283 const Object *value,
284 ident field,
285 JSONContext *context);
286
293 const JSONProperty *property,
294 const Object *value,
295 ident field,
296 JSONContext *context);
297
303 const JSONProperty *property,
304 const Object *value,
305 ident field,
306 JSONContext *context);
307
313 const JSONProperty *property,
314 const Object *value,
315 ident field,
316 JSONContext *context);
317
325 const JSONProperty *property,
326 const Object *value,
327 ident field,
328 JSONContext *context);
329
337 const JSONProperty *property,
338 const Object *value,
339 ident field,
340 JSONContext *context);
341
347 const JSONProperty *property,
348 const Object *value,
349 ident field,
350 JSONContext *context);
351
357 const JSONProperty *property,
358 const Object *value,
359 ident field,
360 JSONContext *context);
361
367 const JSONProperty *property,
368 const Object *value,
369 ident field,
370 JSONContext *context);
371
378 const JSONProperty *property,
379 const Object *value,
380 ident field,
381 JSONContext *context);
382
389 const JSONProperty *property,
390 const Object *value,
391 ident field,
392 JSONContext *context);
393
394#pragma mark - JSONArrayProperties
395
399#define JSONArrayProperties_NoCount ((ptrdiff_t) -1)
400
404typedef struct {
405
410
414 size_t capacity;
415
420 ptrdiff_t count;
421
423
431#define MakeJSONArrayProperties(properties, capacity, count) \
432 &(JSONArrayProperties){ .properties = (properties), .capacity = (capacity), .count = (count) }
433
434# pragma mark - Object serializers
435
440 const JSONProperty *property,
441 ident value,
442 ident data,
443 JSONContext *context);
444
451 const JSONProperty *property,
452 const Object *value,
453 ident field,
454 JSONContext *context);
455
460 const JSONProperty *property,
461 ident value,
462 ident data,
463 JSONContext *context);
464
471 const JSONProperty *property,
472 const Object *value,
473 ident field,
474 JSONContext *context);
475
481 const JSONProperty *property,
482 ident value,
483 ident data,
484 JSONContext *context);
485
492 const JSONProperty *property,
493 const Object *value,
494 ident field,
495 JSONContext *context);
496
502 const JSONProperty *property,
503 ident value,
504 ident data,
505 JSONContext *context);
506
514 const JSONProperty *property,
515 const Object *value,
516 ident field,
517 JSONContext *context);
518
524 const JSONProperty *property,
525 ident value,
526 ident data,
527 JSONContext *context);
528
535 const JSONProperty *property,
536 const Object *value,
537 ident field,
538 JSONContext *context);
539
545 const JSONProperty *property,
546 ident value,
547 ident data,
548 JSONContext *context);
549
556 const JSONProperty *property,
557 const Object *value,
558 ident field,
559 JSONContext *context);
Arrays.
static Data * data(void)
Definition Data.c:286
Microsecond-precision immutable dates.
Key-value stores.
OBJECTIVELY_EXPORT 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.
OBJECTIVELY_EXPORT 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.
OBJECTIVELY_EXPORT ident JSONSerializeUint32(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a uint32_t field to a JSON number.
OBJECTIVELY_EXPORT 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).
OBJECTIVELY_EXPORT ident JSONSerializeDouble(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a double field to a JSON number.
OBJECTIVELY_EXPORT 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.
OBJECTIVELY_EXPORT ident JSONSerializeString(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a String * field to a JSON string.
OBJECTIVELY_EXPORT 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).
OBJECTIVELY_EXPORT bool JSONDeserializeFloat(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON number into a float field.
OBJECTIVELY_EXPORT 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).
OBJECTIVELY_EXPORT ident JSONSerializeCharacters(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a fixed-size char[] field to a JSON string.
OBJECTIVELY_EXPORT ident JSONSerializeUint64(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a uint64_t field to a JSON number.
OBJECTIVELY_EXPORT bool JSONDeserializeInt32(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON number into an int32_t field.
OBJECTIVELY_EXPORT bool JSONDeserializeUint64(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON number into a uint64_t field.
OBJECTIVELY_EXPORT ident JSONSerializeBoole(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a bool field to a JSON boolean.
bool(* JSONDeserializer)(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON value into a C struct field.
OBJECTIVELY_EXPORT bool JSONDeserializeString(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON string into a String * field.
OBJECTIVELY_EXPORT 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(* JSONSerializer)(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a C struct field to an Objectively Object.
OBJECTIVELY_EXPORT ident JSONSerializeDictionary(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a Dictionary * field to a JSON object.
OBJECTIVELY_EXPORT ident JSONSerializeInt64(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes an int64_t field to a JSON number.
OBJECTIVELY_EXPORT ident JSONSerializeSet(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a Set * field to a JSON array.
OBJECTIVELY_EXPORT bool JSONDeserializeObjectArray(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON array into an Array * field.
OBJECTIVELY_EXPORT bool JSONDeserializeArray(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON array into an inline array field.
OBJECTIVELY_EXPORT ident JSONSerializeStruct(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a nested struct field to a JSON object.
OBJECTIVELY_EXPORT ident JSONSerializeFloat(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes a float field to a JSON number.
OBJECTIVELY_EXPORT bool JSONDeserializeDictionary(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON object into a Dictionary * field.
OBJECTIVELY_EXPORT bool JSONDeserializeDouble(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON number into a double field.
OBJECTIVELY_EXPORT ident JSONSerializeInt32(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes an int32_t field to a JSON number.
OBJECTIVELY_EXPORT bool JSONDeserializeSet(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON array into a Set * field.
OBJECTIVELY_EXPORT bool JSONDeserializeUint32(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON number into a uint32_t field.
OBJECTIVELY_EXPORT bool JSONDeserializeInt64(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON number into an int64_t field.
OBJECTIVELY_EXPORT bool JSONDeserializeBoole(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON boolean into a bool field.
OBJECTIVELY_EXPORT ident JSONSerializeArray(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes an inline array field to a JSON array.
OBJECTIVELY_EXPORT bool JSONDeserializeStruct(const JSONProperties *properties, const JSONProperty *property, const Object *value, ident field, JSONContext *context)
Deserializes a JSON object into a nested struct field.
OBJECTIVELY_EXPORT ident JSONSerializeObjectArray(const JSONProperties *properties, const JSONProperty *property, ident value, ident data, JSONContext *context)
Serializes an Array * field to a JSON array.
Object is the root Class of The Objectively Class hierarchy.
Sets.
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
#define OBJECTIVELY_EXPORT
Definition Types.h:36
Uniform Resource Locators (RFC 3986).
Describes the JSON binding for an inline array field of a C struct.
const JSONProperties * properties
The JSONProperties of the array element type.
ptrdiff_t count
Byte offset of the sibling element-count field, or JSONArrayProperties_NoCount.
size_t capacity
The capacity of the array field (maximum number of elements).
A context for JSON serialization and deserialization.
Definition JSONContext.h:72
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).
const char * name
The struct type name.
Describes the JSON binding strategy for a single field of a C struct.
JSONSerializer serializer
The JSONSerializer, or NULL to omit this field from serialization.
ptrdiff_t offset
The byte offset of the field within the struct.
ident data
Opaque user data passed to the serializer and deserializer.
const char * key
The JSON key name. A NULL key terminates the property list.
size_t size
The byte size of the field within the struct.
JSONDeserializer deserializer
The JSONDeserializer, or NULL to omit this field from deserialization.
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46