-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraft-newton-json-content-rules-09.txt
2352 lines (1485 loc) · 78.7 KB
/
draft-newton-json-content-rules-09.txt
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Network Working Group A. Newton
Internet-Draft ARIN
Intended status: Standards Track P. Cordell
Expires: April 1, 2018 Codalogic
September 28, 2017
A Language for Rules Describing JSON Content
draft-newton-json-content-rules-09
Abstract
This document describes a language for specifying and testing the
expected content of JSON structures found in JSON-using protocols,
software, and processes.
Status of This Memo
This Internet-Draft is submitted in full conformance with the
provisions of BCP 78 and BCP 79.
Internet-Drafts are working documents of the Internet Engineering
Task Force (IETF). Note that other groups may also distribute
working documents as Internet-Drafts. The list of current Internet-
Drafts is at https://datatracker.ietf.org/drafts/current/.
Internet-Drafts are draft documents valid for a maximum of six months
and may be updated, replaced, or obsoleted by other documents at any
time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress."
This Internet-Draft will expire on April 1, 2018.
Copyright Notice
Copyright (c) 2017 IETF Trust and the persons identified as the
document authors. All rights reserved.
This document is subject to BCP 78 and the IETF Trust's Legal
Provisions Relating to IETF Documents
(https://trustee.ietf.org/license-info) in effect on the date of
publication of this document. Please review these documents
carefully, as they describe your rights and restrictions with respect
to this document. Code Components extracted from this document must
include Simplified BSD License text as described in Section 4.e of
the Trust Legal Provisions and are provided without warranty as
described in the Simplified BSD License.
Newton & Cordell Expires April 1, 2018 [Page 1]
Internet-Draft JSON Content Rules September 2017
Table of Contents
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1. A First Example: Specifying Content . . . . . . . . . . . 3
1.2. A Second Example: Testing Content . . . . . . . . . . . . 3
2. Overview of the Language . . . . . . . . . . . . . . . . . . 5
3. Lines and Comments . . . . . . . . . . . . . . . . . . . . . 7
4. Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1. Rule Names and Assignments . . . . . . . . . . . . . . . 8
4.2. Annotations . . . . . . . . . . . . . . . . . . . . . . . 9
4.3. Starting Points and Root Rules . . . . . . . . . . . . . 10
4.4. Type Specifications . . . . . . . . . . . . . . . . . . . 10
4.5. Primitive Specifications . . . . . . . . . . . . . . . . 12
4.5.1. Numbers, Booleans and Null . . . . . . . . . . . . . 12
4.5.2. Strings . . . . . . . . . . . . . . . . . . . . . . . 13
4.6. Any Type . . . . . . . . . . . . . . . . . . . . . . . . 16
4.7. Member Specifications . . . . . . . . . . . . . . . . . . 16
4.8. Object Specifications . . . . . . . . . . . . . . . . . . 16
4.9. Array Specifications . . . . . . . . . . . . . . . . . . 19
4.9.1. Unordered Array Specifications . . . . . . . . . . . 21
4.10. Group Specifications . . . . . . . . . . . . . . . . . . 21
4.11. Ordered and Unordered Groups in Arrays . . . . . . . . . 22
4.12. Sequence and Choice Combinations in Array, Object, and
Group Specifications . . . . . . . . . . . . . . . . . . 22
4.13. Repetition in Array, Object, and Group Specifications . . 23
4.14. Negating Evaluation . . . . . . . . . . . . . . . . . . . 25
5. Directives . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.1. jcr-version . . . . . . . . . . . . . . . . . . . . . . . 26
5.2. ruleset-id . . . . . . . . . . . . . . . . . . . . . . . 27
5.3. import . . . . . . . . . . . . . . . . . . . . . . . . . 27
6. Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . 28
6.1. Any Member with Any Value . . . . . . . . . . . . . . . . 28
6.2. Lists of Values . . . . . . . . . . . . . . . . . . . . . 29
6.3. Groups in Arrays . . . . . . . . . . . . . . . . . . . . 29
6.4. Groups in Objects . . . . . . . . . . . . . . . . . . . . 30
6.5. Group Rules as Macros . . . . . . . . . . . . . . . . . . 31
6.6. Object Mixins . . . . . . . . . . . . . . . . . . . . . . 31
6.7. Subordinate Dependencies . . . . . . . . . . . . . . . . 31
7. Implementation Status . . . . . . . . . . . . . . . . . . . . 32
7.1. JCR Validator . . . . . . . . . . . . . . . . . . . . . . 32
7.2. Codalogic JCR Parser . . . . . . . . . . . . . . . . . . 33
7.3. JCR Java . . . . . . . . . . . . . . . . . . . . . . . . 33
8. ABNF Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 33
9. Acknowledgements . . . . . . . . . . . . . . . . . . . . . . 39
10. References . . . . . . . . . . . . . . . . . . . . . . . . . 39
10.1. Normative References . . . . . . . . . . . . . . . . . . 39
10.2. Infomative References . . . . . . . . . . . . . . . . . 40
10.3. URIs . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Newton & Cordell Expires April 1, 2018 [Page 2]
Internet-Draft JSON Content Rules September 2017
Appendix A. Co-Constraints . . . . . . . . . . . . . . . . . . . 40
Appendix B. Testing Against JSON Content Rules . . . . . . . . . 41
B.1. Locally Overriding Rules . . . . . . . . . . . . . . . . 41
B.2. Rule Callbacks . . . . . . . . . . . . . . . . . . . . . 42
Appendix C. Changes from -07 and -08 . . . . . . . . . . . . . . 42
Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 42
1. Introduction
This document describes JSON Content Rules (JCR), a language for
specifying and testing the interchange of data in JSON [RFC7159]
format used by computer protocols and processes. The syntax of JCR
is not JSON but is "JSON-like", possessing the conciseness and
utility that has made JSON popular.
1.1. A First Example: Specifying Content
The following JSON data describes a JSON object with two members,
"line-count" and "word-count", each containing an integer.
{ "line-count" : 3426, "word-count" : 27886 }
Figure 1
This is also JCR that describes a JSON object with a member named
"line-count" that is an integer that is exactly 3426 and a member
named "word-count" that is an integer that is exactly 27886.
For a protocol specification, it is probably more useful to specify
that each member is any integer and not specific, exact integers:
{ "line-count" : integer, "word-count" : integer }
Figure 2
Since line counts and word counts should be either zero or a positive
integer, the specification may be further narrowed:
{ "line-count" : 0.. , "word-count" : 0.. }
Figure 3
1.2. A Second Example: Testing Content
Building on the first example, this second example describes the same
object but with the addition of another member, "file-name".
Newton & Cordell Expires April 1, 2018 [Page 3]
Internet-Draft JSON Content Rules September 2017
{
"file-name" : "rfc7159.txt",
"line-count" : 3426,
"word-count" : 27886
}
Figure 4
The following JCR describes objects like it.
{
"file-name" : string,
"line-count" : 0..,
"word-count" : 0..
}
Figure 5
For the purposes of writing a protocol specification, JCR may be
broken down into named rules to reduce complexity and to enable re-
use. The following example takes the JCR from above and rewrites the
members as named rules.
{
$fn,
$lc,
$wc
}
$fn = "file-name" : string
$lc = "line-count" : 0..
$wc = "word-count" : 0..
Figure 6
With each member specified as a named rule, software testers can
override them locally for specific test cases. In the following
example, the named rules are locally overridden for the test case
where the file name is "rfc4627.txt".
$fn = "file-name" : "rfc4627.txt"
$lc = "line-count" : 2102
$wc = "word-count" : 16714
Figure 7
Newton & Cordell Expires April 1, 2018 [Page 4]
Internet-Draft JSON Content Rules September 2017
In this example, the protocol specification describes the JSON object
in general and an implementation overrides the rules for testing
specific cases.
All figures used in this specification are available here [1].
2. Overview of the Language
JCR is composed of rules (as the name suggests). A collection of
rules that is processed together is a ruleset. Rulesets may also
contain comments, blank lines, and directives that apply to the
processing of a ruleset.
Rules are composed of two parts, an optional rule name and a rule
specification. A rule specification can be either a type
specification or a member specification. A member specification
consists of a member name specification and a type specification.
A type specification is used to specify constraints on a superset of
a JSON value (e.g. number / string / object / array etc.). In
addition to defining primitive types (such as string or integer),
array types, and object types, type specifications may define the JCR
specific concept of group types.
Type specifications corresponding to arrays, objects and groups may
be composed of other rule specifications.
A member specification is used to specify constraints on a JSON
member (i.e. members of a JSON object).
Rules with rule name assignments may be referenced in place of type
specifications and member specifications.
Rules may be defined across line boundaries and there is no line
continuation syntax.
Any rule consisting only of a type specification is considered a root
rule. Unless otherwise specified, all the root rules of a ruleset
are evaluated against a JSON instance or document.
Putting it all together, Figure 9 describes the JSON in Figure 8.
Newton & Cordell Expires April 1, 2018 [Page 5]
Internet-Draft JSON Content Rules September 2017
Example JSON shamelessly lifted from RFC 4627
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/481989943",
"Height": 125,
"Width": 100
},
"IDs": [116, 943, 234, 38793]
}
}
Figure 8
Newton & Cordell Expires April 1, 2018 [Page 6]
Internet-Draft JSON Content Rules September 2017
Rules describing Figure 8
; the root of the JSON instance is an object
; this root rule describes that object
{
; the object specification contains
; one member specification
"Image" : {
; $width and $height are defined below
$width,
$height,
; "Title" member specification
"Title" :string,
; "Thumbnail" member specification, which
; defines an object
"Thumbnail": {
; $width and $height are re-used again
$width, $height,
"Url" :uri
},
; "IDs" member that is an array of
; one ore more integers
"IDs" : [ integer * ]
}
}
; The definitions of the rules $width and $height
$width = "Width" : 0..1280
$height = "Height" : 0..1024
Figure 9
3. Lines and Comments
There is no statement terminator and therefore no need for a line
continuation syntax. Rules may be defined across line boundaries.
Blank lines are allowed.
Newton & Cordell Expires April 1, 2018 [Page 7]
Internet-Draft JSON Content Rules September 2017
Comments are the same as comments in ABNF [RFC4234]. They start with
a semi-colon (';') and continue to the end of the line.
4. Rules
Rules have two main components, an optional rule name assignment and
a type or member specification.
Type specifications define arrays, objects, etc... of JSON and may
reference other rules using rule names. Most type specifications can
be defined with repetitions for specifying the frequency of the type
being defined. In addition to the type specifications describing
JSON types, there is an additional group specification for grouping
types.
Member specifications define members of JSON objects, and are
composed of a member name specification and either a type
specification or a rule name referencing a type specification.
Rules may also contain annotations which may affect the evaluation of
all or part of a rule. Rules without a rule name assignment are
considered root rules, though rules with a rule name assignment can
be considered a root rule with the appropriate annotation.
Type specifications, depending on their type, can contain zero or
more other specifications or rule names. For example, an object
specification might contain multiple member specifications or rule
names that resolve to member specifications or a mixture of member
specifications and rule names. For the purposes of this document,
specifications and rule names composing other specifications are
called subordinate components.
4.1. Rule Names and Assignments
Rule names are signified with the dollar character ('$'), which is
not part of the rule name itself. Rule names have two components, an
optional ruleset identifier alias and a local rule name.
Local rule names must start with an alphabetic character (a-z,A-Z)
and must contain only alphabetic characters, numeric characters, the
hyphen character ('-') and the underscore character ('_'). Local
rule names are case sensitive, and must be unique within a ruleset
(that is, no two rule name assignments may use the same local rule
name).
Ruleset identifier aliases enable referencing rules from another
ruleset. They are not allowed in rule name assignments, and only
found in rule names referencing other rules. Ruleset identifiers
Newton & Cordell Expires April 1, 2018 [Page 8]
Internet-Draft JSON Content Rules September 2017
must start with an alphabetic character and contain no whitespace.
Ruleset identifiers are case sensitive. Simple use cases of JCR will
most likely not use ruleset identifiers.
In Figure 10 below, "http://ietf.org/rfcYYYY.JCR" and
"http://ietf.org/rfcXXXX.JCR" are ruleset identifiers and "rfcXXXX"
is a ruleset identifier alias.
# ruleset-id http://ietf.org/rfcYYYY.JCR
# import http://ietf.org/rfcXXXX.JCR as rfcXXXX
$my_encodings = ( "mythic" | "magic" )
$all_encodings = ( $rfcXXXX.encodings | $my_encodings )
Figure 10
There are two forms of rule name assignments: assignments of
primitive types and assignments of all other types. Rule name
assignments to primitive type specifications separate the rule name
from the type specification with the character sequence '=:', whereas
rule name assignments for all other type specifications only require
the separation using the '=' character.
;rule name assignments for primitive types
$foo =: "foo"
$some_string =: string
;rule name assignments for arrays
$bar = [ integer, integer, integer ]
;rule name assignement for objects
$bob = { "bar" : $bar, "foo" : $foo }
Figure 11
This is the one little "gotcha" in JCR. This syntax is necessary so
that JCR parsers may readily distinguish between rule name
assignments involving string and regular expressions primitive types
and member names of member specifications.
4.2. Annotations
Annotations may appear before a rule name assignment, before a type
or member specification, or before a rule name contained within a
type specification. In each place, there may be zero or more
annotations. Each annotation begins with the character sequence "@{"
and ends with "}". The following is an example of a type
specification with the not annotation (explained in Section 4.14):
Newton & Cordell Expires April 1, 2018 [Page 9]
Internet-Draft JSON Content Rules September 2017
@{not} [ "fruits", "vegatables" ]
Figure 12
This specification defines the annotations "root", "not", and
"unordered", but other annotations may be defined for other purposes.
4.3. Starting Points and Root Rules
Evaluation of a JSON instance or document against a ruleset begins
with the evaluation of a root rule or set of root rules. If no root
rule (or rules) is specified locally at runtime, the set of root
rules specified in the ruleset are evaluated. The order of
evaluation is undefined.
The set of root rules specified in a ruleset is composed of all rules
without a rule name assignment and all rules annotated with the
"@{root}" annotation.
The "@{root}" annotation may either appear before a rule name
assignment or before a type definition. It is ignored if present
before referenced rule name inside of a type specification.
4.4. Type Specifications
The syntax of each type of type specifications varies depending on
the type:
Newton & Cordell Expires April 1, 2018 [Page 10]
Internet-Draft JSON Content Rules September 2017
; primitive types can be string
; or number literals
; or number ranges
"foo"
2
1..10
; primitive types can also be more generalized types
string
integer
; primitive type rules may be named
$my_int =: 12
; member specifications consist of a member name
; followed by a colon and then followed by another
; type specification or a rule name
; (example shown with a rule name assignment)
$mem1 = "bar" : "baz"
$mem2 = "fizz" : $my_int
; member names may either be quoted strings
; or regular expressions
; (example shown with a rule name assignment)
$mem3 = /^dev[0-9]$/ : 0..4096
; object specifications start and end with "curly braces"
; object specifications contain zero
; or more member specifications
; or rule names which reference a member specification
{ $mem1, "foo" : "fuzz", "fizz" : $my_int }
; array specifications start and end with square brackets
; array specifications contain zero
; or more non-member type specifications
[ 1, 2, 3, $my_int ]
; finally, group specifications start and end with parenthesis
; groups contain other type specifications
( [ integer, integer], $rule1 )
$rule1 = [ string, string ]
Figure 13
Newton & Cordell Expires April 1, 2018 [Page 11]
Internet-Draft JSON Content Rules September 2017
4.5. Primitive Specifications
Primitive type specifications define content for JSON numbers,
booleans, strings, and null.
4.5.1. Numbers, Booleans and Null
The rules for booleans and null are the simplest and take the
following forms:
true
false
boolean
null
Figure 14
Rules for numbers can specify the number be either an integer or
floating point number:
integer
float
double
Figure 15
The keyword 'float' represents a single precision IEEE-754 floating
point number represented in decimal. The keyword 'double' represents
a double precision IEEE-754 floating point number represented in
decimal format.
Numbers may also be specified as an absolute value or a range of
possible values, where a range may be specified using a minimum,
maximum, or both:
n
n..m
..m
n..
n.f
n.f..m.f
..m.f
n.f..
Figure 16
When specifying a minimum and a maximum, both must either be an
integer or a floating point number. Thus to specify a floating point
Newton & Cordell Expires April 1, 2018 [Page 12]
Internet-Draft JSON Content Rules September 2017
number between zero and ten a definition of the following form is
used:
0.0..10.0
Figure 17
Integers may also be specified as ranges using bit lengths preceded
by the 'int' or 'uint' words (i.e. 'int8', 'uint16'). The 'int'
prefix specifies the integer as being signed whereas the 'uint'
prefix specifies the integer as being unsigned.
; 0..255
uint8
; -32768..32767
int16
; 0..65535
uint16
; -9223372036854775808..9223372036854775807
int64
; 0..18446744073709551615
uint64
Figure 18
4.5.2. Strings
JCR provides a large number of data types to define the contents of
JSON strings. Generically, a string may be specified using the word
'string'. String literals may be specified using a double quote
character followed by the literal content followed by another double
quote. And regular expressions may be specified by enclosing a
regular expression within the forward slash ('/') character.
Newton & Cordell Expires April 1, 2018 [Page 13]
Internet-Draft JSON Content Rules September 2017
; any string
string
; a string literal
"she sells sea shells"
; a regular expression
/^she sells .*/
Figure 19
Regular expressions are not implicitly anchored and therefore must be
explicitly anchored if necessary.
A string can be specified as a URI [RFC3986] using the word 'uri',
but also may be more narrowly scoped to a URI of a specific scheme.
Specific URI schemes are specified with the word 'uri' followed by
two period characters ('..') followed by the URI scheme.
; any URI
uri
;a URI narrowed for an HTTPS uri
uri..https
Figure 20
IP addresses may be specified with either the word 'ipv4' for IPv4
addresses [RFC1166] or the word 'ipv6' for IPv6 addresses [RFC5952].
Fully qualified A-label and U-label domain names may be specified
with the words 'fqdn' and 'idn'.
Dates and time can be specified as formats found in RFC 3339
[RFC3339]. The word 'date' corresponds to the full-date ABNF rule,
the word 'time' corresponds to the full-time ABNF rule, and the word
'datetime' corresponds to the 'date-time' ABNF rule.
Email addresses formatted according to RFC 5322 [RFC5322] may be
specified using the 'email' word, and E.123 phone numbers may be
specified using the word 'phone'.
Newton & Cordell Expires April 1, 2018 [Page 14]
Internet-Draft JSON Content Rules September 2017
;IP addresses
ipv4
ipv6
ipaddr
;domain names
fqdn
idn
; RFC 3339 full-date
date
; RFC 3339 full-time
time
; RFC 3339 date-time
datetime
; RFC 5322 email address
email
; phone number
phone
Figure 21
Binary data can be specified in string form using the encodings
specified in RFC 4648 [RFC4648]. The word 'hex' corresponds to
base16, while 'base32', 'base32hex', 'base64', and 'base64url'
correspond with their RFC 4648 counterparts accordingly.
; RFC 4648 base16
hex
; RFC 4648 base32
base32
; RFC 4648 base32hex
base32hex
; RFC 4648 base64
base64
; RFC 4648 base64url
base64url
Figure 22
Newton & Cordell Expires April 1, 2018 [Page 15]
Internet-Draft JSON Content Rules September 2017
4.6. Any Type
It is possible to specify that a value can be of any type allowable
by JSON using the word 'any'. The 'any' type specifies any primitive
type, array, or object.
4.7. Member Specifications
Member specifications define members of JSON objects. Unlike other
type specifications, member specifications cannot be root rules and
must be part of an object specification or preceded by a rule name
assignment.
Member specifications consist of a member name specification followed
by a colon character (':') followed by either a subordinate
component, which is either a rule name or a primitive, object, array,
or group specification. Member name specifications can be given
either as a quoted string using double quotes or as a regular
expression using forward slash ('/') characters. Regular expressions
are not implicitly anchored and therefore must have explicit anchors
if needed.
;member name will exactly match "locationURI"
$location_uri = "locationURI" : uri
;member name will match "eth0", "eth1", ... "eth9"
$iface_mappings = /^eth[0-9]$/ : ipv4
Figure 23
4.8. Object Specifications
Object specifications define JSON objects and are composed of zero or
more subordinate components, each of which can be either a rule name,
member specification, or group specification. The subordinate
components are enclosed at the start with a left curly brace
character ('{') and at the end with a right curly brace character
('}').
Evaluation of the subordinate components of object specifications is
as follows:
o No order is implied for the members of the object being evaluated.
o Subordinate components of the object specification are evaluated
in the order they appear.
Newton & Cordell Expires April 1, 2018 [Page 16]
Internet-Draft JSON Content Rules September 2017
o Each member of the object being evaluated can only match one
subordinate component.
o Any members not matched against a subordinate component are
ignored.
The following examples illustrate matching of JSON objects to JCR
object specifications.
As order is not implied for the members of objects under evaluation,
the following rule will match the JSON in Figure 25 and Figure 26.
{ "locationUri" : uri, "statusCode" : integer }
Figure 24
{ "locationUri" : "http://example.com", "statusCode" : 200 }
Figure 25
{ "statusCode" : 200, "locationUri" : "http://example.com" }
Figure 26
Newton & Cordell Expires April 1, 2018 [Page 17]
Internet-Draft JSON Content Rules September 2017
Because subordinate components of an object specification are
evaluated in the order in which they are specified (i.e. left to
right, top to bottom) and object members can only match one
subordinate component of an object specification, the rule o1 below
will not match against the JSON in Figure 28 but the rule o2 below
will match it.
; zero or more members that match "p0", "p1", etc
; and a member that matches "p1"
$o1 = { /^p\d+$/ : integer *, "p1" : integer }
; a member that matches "p1" and
; zero or more members that match "p0", "p1", etc
$o2 = { "p1" : integer, /^p\d+$/ : integer * }
The first subordinate of rule o1 specifies that an object can have
zero or more members (that is the meaning of "*", see Section 4.13)
where the member name is the letter 'p' followed by a number (e.g.
"p0", "p1", "p2"), and the second rule specifies a member with the
exact member name of "p1". Rule o2 has the exact same member
specifications but in the opposite order. Figure 28 does not match
rule o1 because all of the members match the first subordinate rule
leaving none to match the second subordinate rule. However, rule o2
does match because the first subordinate rule matches only one member
of the JSON object allowing the second subordinate rule to match the
other member of the JSON object.
Figure 27
{ "p0" : 1, "p1" : 2 }
Figure 28
As stated above, members of objects which do not match a rule are
ignored. The reason for this validation model is due to the nature
of the typical access model to JSON objects in many programming
languages, where members of the object are obtained by referencing
the member name. Therefore extra members may exist without harm.
However, some specifications may need to restrict the members of a
JSON object to a known set. To construct a rule specifying that no
extra members are expected, the @{not} annotation (see Section 4.14)
may be used with a "match-all" regular expression as the last
subordinate component of the object specification.