Objectively
Object oriented framework for 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
145#define MakeJSONPropertyWithKey(Struct, field, key_, serializer_, deserializer_, data_) \
146 (JSONProperty) { \
147 .key = (key_), \
148 .offset = (ptrdiff_t) offsetof(Struct, field), \
149 .size = sizeof(((Struct *) NULL)->field), \
150 .serializer = (serializer_), \
151 .deserializer = (deserializer_), \
152 .data = (data_) \
153 }
154
160#define MakeJSONProperty(Struct, field, serializer_, deserializer_, data_) \
161 MakeJSONPropertyWithKey(Struct, field, #field, serializer_, deserializer_, data_)
162
166#define MakeJSONProperties(Struct, ...) \
167 (JSONProperties) { \
168 .name = #Struct, \
169 .size = sizeof(Struct), \
170 .properties = (const JSONProperty[]) { \
171 __VA_ARGS__, \
172 (JSONProperty) { .key = NULL } \
173 } \
174 }
175
176#pragma mark - Standard JSONSerializers
177
183 const JSONProperty *property,
184 ident value,
185 ident data,
186 JSONContext *context);
187
193 const JSONProperty *property,
194 ident value,
195 ident data,
196 JSONContext *context);
197
202 const JSONProperty *property,
203 ident value,
204 ident data,
205 JSONContext *context);
206
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
233 const JSONProperty *property,
234 ident value,
235 ident data,
236 JSONContext *context);
237
242 const JSONProperty *property,
243 ident value,
244 ident data,
245 JSONContext *context);
246
251 const JSONProperty *property,
252 ident value,
253 ident data,
254 JSONContext *context);
255
260 const JSONProperty *property,
261 ident value,
262 ident data,
263 JSONContext *context);
264
270 const JSONProperty *property,
271 ident value,
272 ident data,
273 JSONContext *context);
274
280 const JSONProperty *property,
281 ident value,
282 ident data,
283 JSONContext *context);
284
285#pragma mark - Standard JSONDeserializers
286
293 const JSONProperty *property,
294 const Object *value,
295 ident field,
296 JSONContext *context);
297
304 const JSONProperty *property,
305 const Object *value,
306 ident field,
307 JSONContext *context);
308
314 const JSONProperty *property,
315 const Object *value,
316 ident field,
317 JSONContext *context);
318
324 const JSONProperty *property,
325 const Object *value,
326 ident field,
327 JSONContext *context);
328
336 const JSONProperty *property,
337 const Object *value,
338 ident field,
339 JSONContext *context);
340
348 const JSONProperty *property,
349 const Object *value,
350 ident field,
351 JSONContext *context);
352
358 const JSONProperty *property,
359 const Object *value,
360 ident field,
361 JSONContext *context);
362
368 const JSONProperty *property,
369 const Object *value,
370 ident field,
371 JSONContext *context);
372
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
400 const JSONProperty *property,
401 const Object *value,
402 ident field,
403 JSONContext *context);
404
405#pragma mark - JSONArrayProperties
406
410#define JSONArrayProperties_NoCount ((ptrdiff_t) -1)
411
415typedef struct {
416
421
425 size_t capacity;
426
431 ptrdiff_t count;
432
434
442#define MakeJSONArrayProperties(properties, capacity, count) \
443 &(JSONArrayProperties){ .properties = (properties), .capacity = (capacity), .count = (count) }
444
445# pragma mark - Object serializers
446
451 const JSONProperty *property,
452 ident value,
453 ident data,
454 JSONContext *context);
455
462 const JSONProperty *property,
463 const Object *value,
464 ident field,
465 JSONContext *context);
466
471 const JSONProperty *property,
472 ident value,
473 ident data,
474 JSONContext *context);
475
482 const JSONProperty *property,
483 const Object *value,
484 ident field,
485 JSONContext *context);
486
492 const JSONProperty *property,
493 ident value,
494 ident data,
495 JSONContext *context);
496
503 const JSONProperty *property,
504 const Object *value,
505 ident field,
506 JSONContext *context);
507
513 const JSONProperty *property,
514 ident value,
515 ident data,
516 JSONContext *context);
517
525 const JSONProperty *property,
526 const Object *value,
527 ident field,
528 JSONContext *context);
529
535 const JSONProperty *property,
536 ident value,
537 ident data,
538 JSONContext *context);
539
546 const JSONProperty *property,
547 const Object *value,
548 ident field,
549 JSONContext *context);
550
556 const JSONProperty *property,
557 ident value,
558 ident data,
559 JSONContext *context);
560
567 const JSONProperty *property,
568 const Object *value,
569 ident field,
570 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