aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_JSONPair.h
blob: dff8485b6a7a2c03f71a247449643aeafde62604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_JSON.h
 * @ingroup inputoutput
 * @brief JSON A JSON pair consists of a name and a value separated by a colon.
 *
 */
#ifndef libparc_parc_JSONPair_h
#define libparc_parc_JSONPair_h

#include <stdbool.h>

struct parcJSONPair;
typedef struct parcJSONPair PARCJSONPair;

#include <parc/algol/parc_Buffer.h>
#include <parc/algol/parc_BufferComposer.h>
#include <parc/algol/parc_List.h>

#include <parc/algol/parc_JSONArray.h>
#include <parc/algol/parc_JSONValue.h>
#include <parc/algol/parc_JSONParser.h>

/**
 * Create a new JSON Pair
 *
 * @param [in] name A pointer to a {@link PARCBuffer} instance containing the name for the JSON Pair.
 * @param [in] value A pointer to a {@link PARCJSONValue} instance containing the value for the JSON Pair.
 * @return A pointer to a new `PARCJSONPair`, or NULL if an error occured.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *name = parcBuffer_AllocateCString("myname");
 *     PARCJSONValue *value = parcJSONValue_CreateFromInteger(31415);
 *     PARCJSONPair *pair = parcJSONPair_Create(name, value);
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see {@link parcJSONPair_CreateFromString}
 * @see {@link parcJSONPair_CreateFromNULL}
 * @see {@link parcJSONPair_CreateFromBoolean}
 * @see {@link parcJSONPair_CreateFromInteger}
 * @see {@link parcJSONPair_CreateFromDouble}
 * @see {@link parcJSONPair_CreateFromJSONArray}
 * @see {@link parcJSONPair_CreateFromJSON}
 */
PARCJSONPair *parcJSONPair_Create(const PARCBuffer *name, PARCJSONValue *value);

/**
 * Create a `PARCJSONPair` consisting of the given name and value represented as null-terminated C strings.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 * @param [in] value A pointer to a null-terminated C string for the value of the `PARCJSONPair`.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromString("name", "value");
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromString(const char *name, const char *value);

/**
 * Create a `PARCJSONPair` consisting of the given name and `PARCJSONValue`.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 * @param [in] value A pointer to a `PARCJSONValue` for the value of the `PARCJSONPair`.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromJSONValue("name", "value");
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromJSONValue(const char *name, PARCJSONValue *value);

/**
 * Create a `PARCJSONPair` consisting of the given name and the null value.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromNULL("name");
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromNULL(const char *name);

/**
 * Create a `PARCJSONPair` consisting of the given name and a JSON boolean of true or false as the value.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 * @param [in] value Either `true` or `false`.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromBoolean("name", true);
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromBoolean(const char *name, bool value);

/**
 * Create a `PARCJSONPair` consisting of the given name and a JSON Integer.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 * @param [in] value An integer value.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromInteger("name", 314159);
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromInteger(const char *name, int64_t value);

/**
 * Create a `PARCJSONPair` consisting of the given name and a JSON floating point value.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 * @param [in] value A double value.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromDouble("name", 3.14159);
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromDouble(const char *name, double value);

/**
 * Create a `PARCJSONPair` consisting of the given name and a JSON Array.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 * @param [in] value A pointer to a {@link PARCJSONArray} instance for the value.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSONArray *array = parcJSONArray_Create();
 *     PARCJSONPair *pair = parcJSONPair_CreateFromJSONArray("name", array);
 *     parcJSONArray_Release(&array);
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromJSONArray(const char *name, PARCJSONArray *value);

/**
 * Create a `PARCJSONPair` consisting of the given name and a JSON Object.
 *
 * @param [in] name A pointer to a null-terminated C string for the name of the `PARCJSONPair`.
 * @param [in] value A pointer to a {@link PARCJSON} instance.
 *
 * @return A pointer to a `PARCJSONPair` instance, or NULL if an error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCJSON *json parcJSON_Create();
 *     PARCJSONPair *pair = parcJSONPair_CreateFromJSON("name", json);
 *     parcJSON_Release(&json);
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONPair *parcJSONPair_CreateFromJSON(const char *name, PARCJSON *value);

/**
 * Increase the number of references to a `PARCJSONPair` instance.
 *
 * A new instance is not created,
 * only that the given instance's reference count is incremented.
 * Discard the acquired reference by invoking {@link parcJSONPair_Release}.
 *
 * @param [in] pair A pointer to a `PARCJSONPair` instance.
 * @return A pointer to the original instance.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromDouble("name", 3.14159);
 *
 *     PARJSON *reference = parcJSONPair_Acquire(pair);
 *
 *     parcJSONPair_Release(&pair);
 *     parcJSONPair_Release(&reference);
 * }
 * @endcode
 */
PARCJSONPair *parcJSONPair_Acquire(const PARCJSONPair *pair);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * The contents of the deallocated memory used for the PARC object are undefined.
 * Do not reference the object after the last release.
 *
 * @param [in,out] pairPtr A pointer to a pointer to the `PARCJSONPair` instance to release.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromDouble("name", 3.14159);
 *
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 */
void parcJSONPair_Release(PARCJSONPair **pairPtr);

/**
 * Get the {@link PARCBuffer} containing the name of the given `PARCJSONPair`.
 *
 * A new reference to the `PARCBuffer` is not created.
 * The caller must create a new reference, if it retains a reference to the buffer.
 *
 * @param [in] pair A pointer to a `PARCJSONPair` instance.
 *
 * @return A pointer to a `PARCBuffer` instance.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromDouble("name", 3.14159);
 *
 *     const char *name = parcJSONPair_GetName(pair);
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCBuffer *parcJSONPair_GetName(const PARCJSONPair *pair);

/**
 * Print a human readable representation of the given `PARCJSONPair`.
 *
 * @param [in] pair A pointer to the instance to display.
 * @param [in] indentation The level of indentation to use to pretty-print the output.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromDouble("name", 3.14159);
 *     parcJSONPair_Display(pair, 0);
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 */
void parcJSONPair_Display(const PARCJSONPair *pair, int indentation);

/**
 * Get the {@link PARCJSONValue} containing the value of the given `PARCJSONPair`.
 *
 * A new reference to the `PARCJSONValue` is not created.
 * The caller must create a new reference, if it retains a reference to the value instance.
 *
 * @param [in] pair A pointer to a `PARCJSONPair` instance.
 *
 * @return A pointer to a `PARCJSONValue` instance.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *pair = parcJSONPair_CreateFromDouble("name", 3.14159);
 *     PARCJSONValue *value = parcJSONPair_GetValue(pair);
 *     parcJSONPair_Release(&pair);
 * }
 * @endcode
 *
 * @see parcJSONPair_Create
 */
PARCJSONValue *parcJSONPair_GetValue(const PARCJSONPair *pair);

/**
 * Determine if two `PARCJSONPair` instances are equal.
 *
 * Two `PARCJSONPair` instances are equal if, and only if, their names and values are equal.
 *
 * The following equivalence relations on non-null `PARCJSONPair` instances are maintained:
 *
 *   * It is reflexive: for any non-null reference value x, `parcJSONPair_Equals(x, x)`
 *       must return true.
 *
 *   * It is symmetric: for any non-null reference values x and y,
 *     `parcJSONPair_Equals(x, y)` must return true if and only if
 *        `parcJSONPair_Equals(y, x)` returns true.
 *
 *   * It is transitive: for any non-null reference values x, y, and z, if
 *        `parcJSONPair_Equals(x, y)` returns true and
 *        `parcJSONPair_Equals(y, z)` returns true,
 *        then  `parcJSONPair_Equals(x, z)` must return true.
 *
 *   * It is consistent: for any non-null reference values x and y, multiple
 *       invocations of `parcJSONPair_Equals(x, y)` consistently return true or
 *       consistently return false.
 *
 *   * For any non-null reference value x, `parcJSONPair_Equals(x, NULL)` must
 *       return false.
 *
 * @param [in] x A pointer to a `PARCJSONPair` instance.
 * @param [in] y A pointer to a `PARCJSONPair` instance.
 * @return true if the two `PARCJSONPair` instances are equal.
 *
 * Example:
 * @code
 * {
 *     PARCJSONPair *a = parcJSONPair_Equals();
 *     PARCJSONPair *b = parcJSONPair_Equals();
 *
 *     if (parcJSONPair_Equals(a, b)) {
 *         // true
 *     } else {
 *         // false
 *     }
 * }
 * @endcode
 */
bool parcJSONPair_Equals(const PARCJSONPair *x, const PARCJSONPair *y);

/**
 * Produce a null-terminated string representation of the specified instance of `PARCJSONPair`.
 *
 * The non-null result must be freed by the caller via {@link parcMemory_Deallocate}.
 *
 * @param [in] pair A pointer to the `PARCJSONPair` instance.
 *
 * @return NULL Cannot allocate memory.
 * @return non-NULL A pointer to an allocated,
 *         null-terminated C string that must be deallocated via {@link parcMemory_Deallocate}.
 *
 * Example:
 * @code
 * {
 *     PARCJSONValue *value = parcJSONValue_CreateFromInteger(123456);
 *     PARCJSONPair *instance = parcJSONPair_Create(parcBuffer_Wrap("Hello", 5, 0, 5), value);
 *     parcJSONValue_Release(&value);
 *
 *     char *string = parcJSONPair_ToString(instance);
 *
 *     if (string != NULL) {
 *         printf("%s\n", string);
 *         parcMemory_Deallocate((void **)&string);
 *     } else {
 *         printf("Cannot allocate memory\n");
 *     }
 *
 *     parcJSONPair_Release(&instance);
 * }
 * @endcode
 *
 * @see parcJSONPair_BuildString
 * @see parcJSONPair_Display
 */
char *parcJSONPair_ToString(const PARCJSONPair *pair);

/**
 * Append a representation of the specified instance to the given
 * {@link PARCBufferComposer}.
 *
 * @param [in] pair A pointer to the `PARCJSONPair` instance.
 * @param [in,out] composer A pointer to the `PARCBufferComposer` instance.
 *
 * @return NULL Cannot allocate memory.
 * @return non-NULL The given `PARCBufferComposer`.
 *
 * Example:
 * @code
 * {
 *     PARCBufferComposer *result = parcBufferComposer_Create();
 *
 *     parcJSONPair_BuildString(instance, result);
 *
 *     PARCBuffer *string = parcBufferComposer_FinalizeBuffer(result);
 *     printf("Hello: %s\n", parcBuffer_ToString(string));
 *     parcBuffer_Release(&string);
 *
 *     parcBufferComposer_Release(&result);
 * }
 * @endcode
 */
PARCBufferComposer *parcJSONPair_BuildString(const PARCJSONPair *pair, PARCBufferComposer *composer, bool compact);

/**
 * Parse a complete JSON pair
 *
 * A pair consists of a name and a value separated by a colon.
 *
 * @param [in] parser A pointer to a {@link PARCJSONParser} instance.
 *
 * @return non-NULL A pointer to a valid `PARCJSONPair`
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *buffer = parcBuffer_AllocateCString("\"name\" : \"value\"");
 *
 *     PARCJSONParser *parser = parcJSONParser_Create(buffer);
 *     PARCJSONPair *pair = parcJSONPair_Parser(parser);
 *
 *     parcJSONPair_Release(&pair);
 *     parcJSONParser_Release(&parser);
 *     parcBuffer_Release(&buffer);
 * }
 * @endcode
 */
PARCJSONPair *parcJSONPair_Parser(PARCJSONParser *parser);
#endif // libparc_parc_JSONPair_h