forked from netmail-open/wjelement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwjelement.html
734 lines (718 loc) · 20.3 KB
/
wjelement.html
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
<html>
<head>
<title>WJElement Documentation: wjelement</title>
</head>
<body>
<a href="index.html">Index</a>
<h1>WJElement Synopsis</h1>
<p>
The WJElement interfaces allow you to load an entire JSON document into
memory and to then access elements within it more easily.
</p>
<p>
WJElement uses UTF-8 encoding internally, as do the underlying
<a href="wjreader.html">WJReader</a>
and
<a href="wjwriter.html">WJWriter</a>.
libraries.
Adding other unicode support to WJR/WJW would not take too much work.
</p>
<p>
WJElement is thread-safe, though an individual WJElement document and its
children should not be used across threads without locking. WJElement does
not provide locking, but its structure provides a handy client pointer which
you may use for this or any other purpose.
</p>
<h1>WJElement Data Types</h1>
<p>
<strong>WJElement</strong>
is the main structure used for JSON documents. In most circumstances
it will not be necessary to access anything within such a structure directly,
but there are times when peeking at a JSON object's name or type (and other
properties) comes in handy.
</p>
<p>
From wjelement.h:
<blockquote><pre>
typedef struct WJElementPublic {
char *name;
WJRType type;
struct WJElementPublic *next;
struct WJElementPublic *prev;
struct WJElementPublic *child;
struct WJElementPublic *parent;
/* The number of children */
int count;
/*
A count of changes that have been performed on this element, which can
be reset by the consumer.
*/
int changes;
void *client;
/*
If set then this freecb will be called before actually free'ing a
WJElement. If it returns FALSE then the WJElement will NOT be free'd.
This can be used to allow caching of objects. If used in this way then
the consumer that set the callback is responsible for ensuring that it
does get free'd correctly at the correct time.
*/
XplBool (* freecb)(struct WJElementPublic *);
} WJElementPublic;
typedef WJElementPublic * WJElement;
</pre></blockquote>
</p>
<p>
<strong>WJEAction</strong>
specifies which operation to carry out when calling the JSON manipulation
functions.
</p>
<p>
From wjelement.h:
<blockquote><pre>
typedef enum {
WJE_GET = 0,
WJE_SET,
WJE_NEW,
WJE_MOD
} WJEAction;
</pre></blockquote>
</p>
<p>
<ul>
<li>
<strong>WJE_GET</strong>
<ul>
<li>
Return the value of an element. If the element does not exist then the
provided default value will be returned.
</li>
</ul>
</li>
<li>
<strong>WJE_SET</strong>
<ul>
<li>
Assign the specified value to an element. If the element does not exist
then it will be created.
</li>
<li>
If the element can not be created then an appropriate value will be
returned to indicate the error.
</li>
<li>
When applicable a NULL will be returned. Otherwise a value that does
not match the requested value will be returned.
</li>
</ul>
</li>
<li>
<strong>WJE_NEW</strong>
<ul>
<li>
Create a new element and assign it the specified value.
</li>
<li>
If an element already exists then it will not be modified, and the value
of that existing element will be returned.
</li>
</ul>
</li>
<li>
<strong>WJE_MOD</strong>
<ul>
<li>
Assign the specified value to an existing element, and return the value
if successful.
</li>
<li>
If the element does not exist then no elements are created or modified.
</li>
<li>
When applicable a NULL will be returned. Otherwise a value that does
not match the requested value will be returned.
</li>
</ul>
</li>
</ul>
</p>
<h1>WJElement Interfaces</h1>
<h2>Document/Element Management</h2>
<p>
<strong>WJEParse</strong>
- Parse the provided JSON document and return the new WJElement
</p>
<p>
<blockquote><pre>
WJElement WJEParse(const char *json);
WJElement _WJEParse(const char *json, char quote);
</pre></blockquote>
</p>
<p>
The _WJEParse version can be used to parse a document with a non-standard
quote character. This allows easy parsing of simple documents directly in a
C source file without having to escape double quote characters. Example:
<blockquote><pre>
doc = _WJEParse("{ 'foo': true, 'bar': 'yup' }", '\'');
</pre></blockquote>
</p>
<p>
<strong>WJEOpenDocument</strong>
- Load a WJElement object from the provided
<a href="wjreader.html">WJReader</a>
</p>
<p>
<blockquote><pre>
WJElement WJEOpenDocument(WJReader reader, char *where, WJELoadCB loadcb, void *data);
</pre></blockquote>
</p>
<p>
If a load callback is provided then it will be called before adding any new
children, allowing the consumer to leave specific elements of the hierarchy.
</p>
<p>
<strong>WJEWriteDocument</strong>
- Write a WJElement object to the provided
<a href="wjwriter.html">WJWriter</a>
<blockquote><pre>
XplBool WJEWriteDocument(WJElement document, WJWriter writer, char *name);
</pre></blockquote>
</p>
<p>
<strong>WJEWriteFILE</strong>
- Write a WJElement object to the provided FILE*
<blockquote><pre>
void WJEWriteFILE(WJElement document, FILE* fd);
</pre></blockquote>
</p>
<p>
WJEWriteFILE() requires an already opened FILE* to write to.
It is also used internally by <a href="#WJEDump">WJEDump()</a>
</p>
<p>
<strong>WJEWriteMEM</strong>
- Allocate and write to a string
<blockquote><pre>
char * WJEWriteMEM(WJElement document, XplBool pretty, size_t maxlength);
</pre></blockquote>
</p>
<p>
WJEWriteMEM() allocates and returns a string containing the document.
A maxlength of 0 means the string may be of unlimited length.
The returned string must be freed with MemFree by the consumer.
</p>
<p>
<strong>WJECloseDocument</strong>
- Destroy a WJElement object
</p>
<p>
<blockquote><pre>
XplBool WJECloseDocument(WJElement document);
</pre></blockquote>
</p>
<p>
WJECloseDocument is also used to delete/remove an item from a parent document:
WJECloseDocument(WJEGet(...));
</p>
<p>
<strong>WJECopyDocument</strong>
- Duplicate an existing WJElement
</p>
<p>
<blockquote><pre>
WJElement WJECopyDocument(WJElement to, WJElement from, WJECopyCB loadcb, void *data);
</pre></blockquote>
</p>
<p>
If a copy callback is provided, then it will be called with each element in
order to allow filtered copying on an element-by-element basis, using any
criteria.
</p>
<p>
<strong>WJEDetach</strong>
- Remove a WJElement from it's parent (and siblings)
</p>
<p>
<blockquote><pre>
XplBool WJEDetach(WJElement document);
</pre></blockquote>
</p>
<p>
<strong>WJEAttach</strong>
- Add a document to another document as a child
</p>
<p>
<blockquote><pre>
XplBool WJEAttach(WJElement container, WJElement document);
</pre></blockquote>
</p>
<p>
<strong>WJERename</strong>
- Rename an element
</p>
<p>
<blockquote><pre>
XplBool WJERename(WJElement document, const char *name);
</pre></blockquote>
</p>
<p>
<strong>WJEMergeObjects</strong>
- Merge all fields from one object to another
</p>
<p>
<blockquote><pre>
XplBool WJEMergeObjects(WJElement to, WJElement from, XplBool overwrite);
</pre></blockquote>
</p>
<h2>JSON Manipulation</h2>
<p>
All JSON manipulation functions take a 'path' argument. This is a string as
explained below, see <a href="#selectors">WJElement Selectors</a>
</p>
<p>
Functions which take a 'last' parameter allow enumeration of multiple
matching elements, if non-NULL is passed. Handy for looping through objects
and arrays.
</p>
<p>
<strong>WJEGet</strong>
- Find the first element within the hierarchy of a WJElement that matches the
specified path.
</p>
<p>
<blockquote><pre>
WJElement WJEGet(WJElement container, char *path, WJElement last);
</pre></blockquote>
</p>
<p>
In cases where a direct child of the WJElement is needed,
<strong>WJEChild</strong> may be more suitable, especially if the child's
name begins with a non-alphanumeric character.
</p>
<p>
<strong>WJEBool</strong>
- Access a boolean element
</p>
<p>
<blockquote><pre>
XplBool WJEBool(WJElement container, char *path, WJEAction action, XplBool value);
XplBool _WJEBool(WJElement container, char *path, WJEAction action, WJElement *last, XplBool value);
</pre></blockquote>
</p>
<p>
<strong>WJEString, WJEStringN</strong>
- Access a string element
</p>
<p>
<blockquote><pre>
char * WJEString(WJElement container, char *path, WJEAction action, char *value);
char * _WJEString(WJElement container, char *path, WJEAction action, WJElement *last, char *value);
char * WJEStringN(WJElement container, char *path, WJEAction action, char *value, size_t len);
char * _WJEStringN(WJElement container, char *path, WJEAction action, WJElement *last, char *value, size_t len);
</pre></blockquote>
</p>
<p>
<strong>WJEObject</strong>
- Access an object element
</p>
<p>
<blockquote><pre>
WJElement WJEObject(WJElement container, char *path, WJEAction action);
WJElement _WJEObject(WJElement container, char *path, WJEAction action, WJElement *last);
</pre></blockquote>
</p>
<p>
<strong>WJEArray</strong>
- Access an array element
</p>
<p>
<blockquote><pre>
WJElement WJEArray(WJElement container, char *path, WJEAction action);
WJElement _WJEArray(WJElement container, char *path, WJEAction action, WJElement *last);
</pre></blockquote>
</p>
<p>
<strong>WJENull</strong>
- Access a null element
</p>
<p>
<blockquote><pre>
WJElement WJENull(WJElement container, char *path, WJEAction action);
WJElement _WJENull(WJElement container, char *path, WJEAction action, WJElement *last);
</pre></blockquote>
</p>
<p>
<strong>WJEInt32</strong>
- Access a number element, as a 32-bit integer
</p>
<p>
<blockquote><pre>
int32 WJEInt32(WJElement container, char *path, WJEAction action, int32 value);
int32 _WJEInt32(WJElement container, char *path, WJEAction action, WJElement *last, int32 value);
</pre></blockquote>
</p>
<p>
<strong>WJEUInt32</strong>
- Access a number element, as a 32-bit unsigned integer
</p>
<p>
<blockquote><pre>
uint32 WJEUInt32(WJElement container, char *path, WJEAction action, uint32 value);
uint32 _WJEUInt32(WJElement container, char *path, WJEAction action, WJElement *last, uint32 value);
</pre></blockquote>
</p>
<p>
<strong>WJEInt64</strong>
- Access a number element, as a 64-bit integer
</p>
<p>
<blockquote><pre>
int64 WJEInt64(WJElement container, char *path, WJEAction action, int64 value);
int64 _WJEInt64(WJElement container, char *path, WJEAction action, WJElement *last, int64 value);
</pre></blockquote>
</p>
<p>
<strong>WJEUInt64</strong>
- Access a number element, as a 64-bit unsigned integer
</p>
<p>
<blockquote><pre>
uint64 WJEUInt64(WJElement container, char *path, WJEAction action, uint64 value);
uint64 _WJEUInt64(WJElement container, char *path, WJEAction action, WJElement *last, uint64 value);
</pre></blockquote>
</p>
<p>
<strong>WJEDouble</strong>
- Access a number element, as a double (as in double-precision floating point)
</p>
<p>
<blockquote><pre>
double WJEDouble(WJElement container, char *path, WJEAction action, double value);
double _WJEDouble(WJElement container, char *path, WJEAction action, WJElement *last, double value);
</pre></blockquote>
</p>
<p>
<strong>WJE...F variants</strong>
- Format-capable versions of element functions
</p>
<p>
The following functions...
<ul>
<li><strong>WJEGetF</strong></li>
<li><strong>WJEBoolF</strong></li>
<li><strong>WJEStringF</strong></li>
<li><strong>WJEStringNF</strong></li>
<li><strong>WJEObjectF</strong></li>
<li><strong>WJEArrayF</strong></li>
<li><strong>WJENullF</strong></li>
<li><strong>WJEInt32F</strong></li>
<li><strong>WJEUInt32F</strong></li>
<li><strong>WJEInt64F</strong></li>
<li><strong>WJEUInt64F</strong></li>
<li><strong>WJEDoubleF</strong></li>
</ul>
...are identical to the non F variants except that the
path argument has been moved to the end and is used as a format string. For
example if you need a value by index, and that index is stored in the
variable x:
<blockquote><pre>
val = WJENumberF(doc, WJE_GET, NULL, 0, "foo[%d]", x);
</pre></blockquote>
</p>
<p>
<strong>WJEChild</strong>
- Find, create or update an element by name instead of path. This allows
access to elements that would be difficult to reference by path. No selector
syntax is used, so only direct children of the element can be found.
</p>
<p>
<blockquote><pre>
WJElement WJEChild(WJElement container, char *name, WJEAction action);
</pre></blockquote>
</p>
<p>
Type specific actions may be done by passing the resulting WJElement and a
NULL path to WJEBool(), WJENumber(), WJEString(), WJEObject(), WJEArray() or
WJENull().
</p>
<h2>Data Processing</h2>
<p>
<strong>WJEHash</strong>
- Calculate a hash for a document
</p>
<p>
<blockquote><pre>
typedef int (* WJEHashCB)(void *context, void *data, size_t size);
EXPORT void WJEHash(WJElement document, WJEHashCB update, void *context);
</pre></blockquote>
</p>
<h2>Schema</h2>
<p>
Callbacks:
</p>
<p>
<blockquote><pre>
typedef WJElement (* WJESchemaLoadCB)(const char *name, void *client, const char *file, const int line);
typedef void (* WJESchemaFreeCB)(WJElement schema, void *client);
typedef void (* WJESchemaMatchCB)(WJElement schema, const char *selector, void *client);
typedef void (* WJEErrCB)(void *client, const char *format, ...);
</pre></blockquote>
</p>
<p>
WJESchemaLoadCB callbacks are used to fetch schema as needed.
WJESchemaFreeCB are called when schema is no longer needed.
</p>
<p>
<strong>WJESchemaValidate</strong>
- Validate a document against a given schema.
</p>
<p>
<blockquote><pre>
XplBool WJESchemaValidate(WJElement schema, WJElement document,
WJEErrCB err, WJESchemaLoadCB load,
WJESchemaFreeCB freecb, void *client);
</pre></blockquote>
</p>
<p>
Additional schema will be loaded
via the load callback if needed. Any validation errors will be reported,
printf-style, to errcb.</p>
<p>
If a the load callback is used to acquire schema but a NULL free callback is
provided, WJECloseDocument will be used internally to release it.
</p>
<p>
<strong>WJESchemaIsType</strong>
- Determine if a document implements a specific schema.
</p>
<p>
<blockquote><pre>
XplBool WJESchemaIsType(WJElement document, const char *type,
WJESchemaLoadCB loadcb, WJESchemaFreeCB freecb,
void *client);
</pre></blockquote>
</p>
<p>
Additional schema will be loaded via the load callback if needed.
</p>
<p>
If a load callback is not provided then the object type will still be checked
but it will not be considered a match if it is a type that extends the
specifed type.
</p>
<p>
<strong>WJESchemaNameIsType</strong>
- variation of WJESchemaIsType which acts on schema name instead of a document
</p>
<p>
<blockquote><pre>
XplBool WJESchemaNameIsType(const char *describedby, const char *type,
WJESchemaLoadCB loadcb,
WJESchemaFreeCB freecb, void *client);
</pre></blockquote>
</p>
<p>
<strong>WJESchemaGetSelectors</strong>
- find type/format-matching properties
</p>
<p>
<blockquote><pre>
void WJESchemaGetSelectors(WJElement document,
char *type, char *format,
WJESchemaLoadCB load,
WJESchemaFreeCB freecb,
WJESchemaMatchCB matchcb, void *client);
</pre></blockquote>
</p>
<p>
WJESchemaGetSelectors
calls back matchcb for each WJElement selector which will fetch a property
of a given type and format, from a given document. The load callback will be
used to load all necessary schema, starting with the document's "describedby".
stripat-type wildcards may be used; "Date*" will find "date" and "date-time".
</p>
<p>
<strong>WJESchemaGetAllSelectors</strong>
- variation of WJESchemaGetSelectors which provides selectors that
<em>could</em> exist in objects of the given "describedby" schema name
</p>
<p>
<blockquote><pre>
void WJESchemaGetAllSelectors(char *describedby,
char *type, char *format,
WJESchemaLoadCB load,
WJESchemaFreeCB freecb,
WJESchemaMatchCB matchcb, void *client);
</pre></blockquote>
</p>
<p>
<strong>WJESchemaFindBacklink</strong>
- find "backlink" property by schema
</p>
<p>
<blockquote><pre>
char * WJESchemaFindBacklink(WJElement document, const char *format,
WJESchemaLoadCB loadcb, WJESchemaFreeCB freecb,
void *client);
</pre></blockquote>
</p>
<p>
<strong>WJESchemaNameFindBacklink</strong>
- find "backlink" property by name
</p>
<p>
<blockquote><pre>
char * WJESchemaNameFindBacklink(char *describedby, const char *format,
WJESchemaLoadCB loadcb, WJESchemaFreeCB freecb,
void *client);
</pre></blockquote>
</p>
<p>
<strong>WJESchemaFreeBacklink</strong>
- clean up a previously-found backlink
</p>
<p>
<blockquote><pre>
void WJESchemaFreeBacklink(char *backlink);
</pre></blockquote>
</p>
<h2>Debug</h2>
<p><a name="WJEDump" />
<strong>WJEDump</strong>
- write a document to stdout
</p>
<p>
<blockquote><pre>
void WJEDump(WJElement document);
</pre></blockquote>
</p>
<a name="selectors"><h1>WJElement Selectors</h1></a>
<p>
Elements within the hierarchy of a JSON document can be referenced using a
path. Multiple levels of hierarchy can be referenced to find any element
below the provided container. The following rules apply to any WJE
functions that take a path argument.
</p>
<p>
A child may be referenced with an alpha-numeric name, or a subscript within
square brackets:
<blockquote><pre>
foo
["foo"]
["$foo"]
</pre></blockquote>
</p>
<p>
Additional levels of heirarchy can be referenced by appending an additional
subscript, or appending a dot and an additional alpha-numeric name:
<blockquote><pre>
one.two.three.four
one["two"]["three"].four
</pre></blockquote>
</p>
<p>
Subscripts may contain double quoted names. Any special characters,
(including .[]*?"'\) can be included by prefixing with a \.
<blockquote><pre>
foo["bar.smeg"]
foo["something with a \"quote\""]
</pre></blockquote>
</p>
<p>
Subscripts may contain single quoted names, which behave as double quoted
names but also allow for * and ? wild card substitution:
<blockquote><pre>
foo['bar.*']
</pre></blockquote>
</p>
<p>
Subscripts may reference an item by it's offset in an array (or object):
<blockquote><pre>
foo[0]
foo[3]
</pre></blockquote>
</p>
<p>
Negative offsets are wrapped to the end of the array (or object) meaning
that [-1] references the last item.
</p>
<p>
Subscripts may reference a range of offsets by providing 2 offsets seperated
by a colon:
<blockquote><pre>
foo[2:5]
</pre></blockquote>
</p>
<p>
Subscripts may reference a set of items by seperating offsets, offset,
ranges, double quoted and single quoted values:
<blockquote><pre>
foo[2,4:6,'bar.*', "smeg"]
</pre></blockquote>
</p>
<p>
An empty subscript may be specified to reference all children.
<blockquote><pre>
[]
</pre></blockquote>
</p>
<p>
A subscript of $ may be specified in actions perform creations to reference
the item after the end of an array. This allows appending to an array.
<blockquote><pre>
[$]
</pre></blockquote>
</p>
<p>
A subscript may be prefixed by an | character to indicate that the subscript
is optional. The portion of the selector from the | on will be ignored if
there is a match and that match has no children.
</p>
<p>
For example, if an element named foo may either be a string or an array of
strings you can enumerate all values using a selector of:
<blockquote><pre>
foo|[]
</pre></blockquote>
</p>
<p>
A NULL path refers to the container itself.
</p>
<p>
A path may end in a condition. The condition consists of an operator and a
value. The value may be a number, a double quoted string, or a single
quoted string. If a single quoted string is used it may contain * and ?
wild cards.
</p>
<p>
The following operators are supported for any value:
<blockquote><pre>
==, !=
</pre></blockquote>
</p>
<p>
A number value may also use the following operators:
<blockquote><pre>
<, >, <=, >=
</pre></blockquote>
</p>
<p>
Example:
<blockquote><pre>
foo.bar <= 3
foo.bar != 'foo*'
foo.bar != "one"
</pre></blockquote>
<p>
A condition may be separated from a path with a ; character.
</p>
<p>
In the following examples the object named "foo" will be returned if it has
a child named "bar" that matches the condition.
<blockquote><pre>
foo; bar <= 3
foo; bar != 'foo*'
foo; bar != "one"
</pre></blockquote>
</p>
</body>
</html>