-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMissingJSONTYPE.xml
1486 lines (1340 loc) · 63.5 KB
/
MissingJSONTYPE.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Class name="%ZJSON.Generator">
<Description><![CDATA[
Generator methods for %ZJSON.Adaptor.<br><br>
adjusted for Caché to bypass missing parameter JSONTYPE by rcc; July 2020
]]></Description>
<Abstract>1</Abstract>
<IncludeCode>%occInclude,%ZjsonMap,%ZjsonErrors</IncludeCode>
<IncludeGenerator>%occInclude,%ZjsonMap</IncludeGenerator>
<System>2</System>
<TimeCreated>65552,77260.973292</TimeCreated>
<Method name="GenerateMapping">
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,*mapping</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
// Initialize map to empty
Kill mapping
// Get the inheritance order
Do ..LeftSequenceClass(class,.classSeq)
// Get %JSONIGNOREINVALIDFIELD parameter
Set jsonignoreinvalidfield=''$$$comMemberKeyGet(class,$$$cCLASSparameter,"%JSONIGNOREINVALIDFIELD",$$$cPARAMdefault)
// Get %JSONMAPPING class parameter.
Set jsondefaultmapping=$$$comMemberKeyGet(class,$$$cCLASSparameter,"%JSONMAPPING",$$$cPARAMdefault)
// Get default for %JSONNULL
Set jsondefaultnull=''$$$comMemberKeyGet(class,$$$cCLASSparameter,"%JSONNULL",$$$cPARAMdefault)
// Get default for %JSONIGNORENULL
Set jsondefaultignorenull=''$$$comMemberKeyGet(class,$$$cCLASSparameter,"%JSONIGNORENULL",$$$cPARAMdefault)
// Get default for %JSONREFERENCE
Set jsondefaultreference=$zcvt($$$comMemberKeyGet(class,$$$cCLASSparameter,"%JSONREFERENCE",$$$cPARAMdefault),"U")
If jsondefaultreference="" Set jsondefaultreference="OBJECT"
If $case(jsondefaultreference,"OBJECT":0,"ID":0,"OID":0,"GUID":0,:1) {
Quit $$$ERROR($$$JSONInvalidDefaultParameter,"parameter %JSONREFERENCE","class base")
}
// Create map node for each property
Set sc=$$$OK
Set property=""
For {
Set property=$$$comMemberNext(class,$$$cCLASSproperty,property)
If property="" Quit
// Get %JSONINCLUDE property parameter
Set jsoninclude=$zcvt($$$comMemberArrayGet(class,$$$cCLASSproperty,property,$$$cPROPparameter,"%JSONINCLUDE"),"L")
Set includeProperty=$case(jsoninclude,"":"","inout":1,"inputonly":1,"outputonly":1,"none":1,:-1)
If includeProperty=-1 {
Set sc=$$$ERROR($$$JSONInvalidPropertyParameter,"parameter %JSONINCLUDE",property,"class base")
Quit
}
// Get details of the property's type
Do ..GetMemberInfo(class,property,.includeProperty,.type,.membercat)
If includeProperty {
// Default for %JSONINCLUDE is "inout"
If jsoninclude="" Set jsoninclude="inout"
// Do not support array of streams
If membercat["STREAM",membercat["Array" {
Set sc=$$$ERROR($$$JSONNoStreamArray,property,"class base")
Quit
}
// For string types, set literaltype to string, base64 or hex.
Set literaltype=""
If membercat["CSTREAM" {
Set literaltype="string"
} ElseIf membercat["BSTREAM" {
Set encoding=$$$comMemberArrayGet(class,$$$cCLASSproperty,property,$$$cPROPparameter,"ENCODING")
Set literaltype=$case($zcvt(encoding,"L"),"":"base64","base64":"base64","hex":"hex",:"")
If literaltype="" {
Set sc=$$$ERROR($$$XMLInvalidEncoding,encoding,property)
Quit
}
} ElseIf membercat["LITERAL" {
Set literaltype=$$$comMemberKeyGet(type,$$$cCLASSparameter,"JSONTYPE",$$$cPARAMdefault)
#;rc If literaltype="" Set literaltype="string"
#;rcc Simulate default JSONTYPE for Caché
if literaltype="" {
if type="%Library.Boolean" {
set literaltype="boolean"
} elseif ($e(type,1,9)="%Library.") &&
("BigInt,Counter,Currency,Decimal,Double,Float,Integer,Numeric,SmallInt,TinyInt"[$p(type,".",2)) {
set literaltype="number"
} else {
set literaltype="string"
}
}
#;rcc
// We need to differentiate between registered object strings and JSON strings.
// For example between %String and %Timestamp.
// A string is considered a registered object string if XSDTYPE, as well as JSONTYPE, is = "string".
If literaltype="string",$$$comMemberKeyGet(type,$$$cCLASSparameter,"XSDTYPE",$$$cPARAMdefault)'="string" {
Set literaltype="string/json"
}
}
// Find actual class where property is defined.
Set defClass=$$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPorigin)
// Find if required
Set required=$$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPrequired)
// Get JSON field name. If not specified, then it defaults to the property name.
Set jsonfieldname=$$$comMemberArrayGet(class,$$$cCLASSproperty,property,$$$cPROPparameter,"%JSONFIELDNAME")
If jsonfieldname="" Set jsonfieldname=property
// Get %JSONNULL property parameter. Default is %JSONNULL class parameter.
Set jsonnull=$$$comMemberArrayGet(class,$$$cCLASSproperty,property,$$$cPROPparameter,"%JSONNULL")
Set jsonnull=$select(jsonnull="":jsondefaultnull,1:''jsonnull)
// Get %JSONIGNORENULL property parameter. Default is %JSONIGNORENULL class parameter.
If literaltype="string" {
Set jsonignorenull=$$$comMemberArrayGet(class,$$$cCLASSproperty,property,$$$cPROPparameter,"%JSONIGNORENULL")
Set jsonignorenull=$select(jsonignorenull="":jsondefaultignorenull,1:''jsonignorenull)
} Else {
Set jsonignorenull=0
}
// Get %JSONREFERENCE property parameter. Default is %JSONREFERENCE class parameter.
If membercat["OBJ" {
Set jsonreference=$zcvt($$$comMemberArrayGet(class,$$$cCLASSproperty,property,$$$cPROPparameter,"%JSONREFERENCE"),"U")
If jsonreference="" Set jsonreference=jsondefaultreference
Set jsonreference=$case(jsonreference,
"OBJECT":$$$jsonrefobject,
"ID":$$$jsonrefid,
"OID":$$$jsonrefoid,
"GUID":$$$jsonrefguid,
:"")
If jsonreference="" {
Set sc=$$$ERROR($$$JSONInvalidPropertyParameter,"parameter %JSONREFERENCE",property,"class base")
Quit
}
// Get JSON mapping for object references.
Set jsonmapping=$$$comMemberArrayGet(class,$$$cCLASSproperty,property,$$$cPROPparameter,"%JSONMAPPING")
If jsonmapping="" Set jsonmapping=jsondefaultmapping
} Else {
Set jsonreference=""
}
// Set the node
Set node=$lb(property,$$$QN(property),jsoninclude,jsonfieldname,jsonnull,jsonignorenull,jsonreference,required,literaltype,membercat,type,jsonmapping)
Set item=$increment(tempmap)
Set tempmap(
+classSeq-classSeq(defClass)+1,
+$$$defMemberKeyGet(defClass,$$$cCLASSproperty,property,$$$cPROPsequencenumber),
item)=node
}
}
// Copy nodes to map ordered by Sequence Number
If $$$ISOK(sc) {
Set mapping($c(1),0)=$lb(class,jsonignoreinvalidfield)
Set mapping($c(1))=0
Set cls=""
For {
Set cls=$order(tempmap(cls))
If cls="" Quit
Set prop=""
For {
Set prop=$order(tempmap(cls,prop))
If prop="" Quit
Set num=""
For {
Set num=$order(tempmap(cls,prop,num))
If num="" Quit
Set item=mapping($c(1))+1
Set mapping($c(1))=item
Set mapping($c(1),item)=tempmap(cls,prop,num)
}
}
}
}
Quit sc
]]></Implementation>
</Method>
<Method name="GetMemberInfo">
<Description><![CDATA[
Return member type information about a property
<pre>
includeProperty is set if passed in as "" which means not specified
member cat is defined by
<base cat> = "LITERAL" | "OBJPERSISTENT" | "OBJSERIAL" | "OBJREGISTERED" | "CSTREAM" | "BSTREAM"
If RELATIONSHIP MANY|CHILDREN, then ListCollectionOBJPERSISTENT
If List Of, then { ArrayCollection | ListCollection } <base cat>
If %ListOf... or %ArrayOf... { ArrayCollection | ListCollection } <ELEMENTTYPE cat>
Otherwis <base cat>
type is the type referenced by the property.]]></Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec><![CDATA[class:%String,property:%String,&includeProperty:%Boolean,*type:%String,*membercat:%String]]></FormalSpec>
<Implementation><![CDATA[
Set collection=$$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPcollection)
Set relationship=$$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPrelationship)
Set cardinality=$$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPcardinality)
// Determine default property include if not already specified
If includeProperty = "" {
If $$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPmultidimensional) {
Set includeProperty = 0
} ElseIf $$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPprivate)||(property="%Concurrency") {
Set includeProperty = 0
} ElseIf relationship,cardinality'="children",cardinality'="many" {
Set includeProperty = 0
} Else {
Set includeProperty = 1
}
}
If includeProperty {
Set type=$$$comMemberKeyGet(class,$$$cCLASSproperty,property,$$$cPROPtype)
If type="" Set type="%Library.String"
Set typecat=$$$getClassType(type)
// Get member category for base type
Set membercat=##class(%Compiler.XML.Generator.Adaptor).TypeToMembercat(type,typecat,.mixed)
// If no membercat, then do not include property
If membercat="" Set includeProperty=0
}
If includeProperty {
// Modify membercat for collections
If relationship {
If (cardinality = "many") || (cardinality = "children") {
// RELATIONSHIP MANY|CHILDREN = ListCollectionOBJPERSISTENT
Set membercat = "ListCollection"_membercat
}
} Else {
If collection="" {
// Not a collection - LITERAL|OBJPERSISTENT|OBJSERIAL|OBJREGISTERED
If (membercat'="LITERAL") {
// Get list of super classes
Kill typeSeq
Do ##class(%Compiler.XML.Generator.Adaptor).SequenceClass(type,.typeSeq)
// Check for collection
If $data(typeSeq("%Library.ArrayOfDataTypes")) {
Set membercat = "ArrayCollection"
} ElseIf $data(typeSeq("%Library.ArrayOfObjects")) {
Set membercat = "ArrayCollection"
} ElseIf $data(typeSeq("%Library.ListOfDataTypes")) {
Set membercat = "ListCollection"
} ElseIf $data(typeSeq("%Library.ListOfObjects")) {
Set membercat = "ListCollection"
}
If membercat["Collection" {
Set type=$$$comMemberKeyGet(type,$$$cCLASSparameter,"ELEMENTTYPE",$$$cPARAMdefault)
If membercat'["OBJ",type="" Set type="%Library.String"
If type="" {
Set sc=$$$ERROR($$$XMLNeedElementType,property)
Quit
}
Set type=$$$NormalizeClassname(type)
Set typecat=$$$getClassType(type)
Set membercat=membercat_##class(%Compiler.XML.Generator.Adaptor).TypeToMembercat(type,typecat,.mixed)
}
}
} Else {
if collection = $$$cPROPCOLLECTIONLIST {
// LIST COLLECTION and LITERAL = ListCollectionLITERAL
// LIST COLLECTION and otherwise = ListCollectionOBJ
Set membercat="ListCollection"_membercat
} ElseIf collection = $$$cPROPCOLLECTIONARRAY {
// ARRAY and LITERAL = ArrayCollectionLITERAL
// ARRAY and otherwise = ArrayCollectionOBJ
Set membercat="ArrayCollection"_membercat
}
}
}
}
]]></Implementation>
</Method>
<Method name="LeftSequenceClass">
<Description>
Return all super classes in sequence of left to right with super class immediately following its child.</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec><![CDATA[class:%String,&seqArray]]></FormalSpec>
<Implementation><![CDATA[
If '$data(seqArray(class)) Set seqArray(class)=$increment(seqArray)
Set superList=$$$comClassKeyGet(class,$$$cCLASSsuper)
Set len=$length(superList,",")
For i=len:-1:1 {
Set super=$piece(superList,",",i)
If super'="" {
Do ..LeftSequenceClass(super,.seqArray)
}
}
Quit
]]></Implementation>
</Method>
<Method name="GenerateMappingFromXdata">
<Description>
Get the JSON adaptor configuration information from class and property parameters.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec><![CDATA[class:%String,&mapping]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set compiledclass=##class(%Dictionary.CompiledClass).%OpenId(class,,.sc)
If $$$ISERR(sc) Quit sc
Set count=compiledclass.XDatas.Count()
For i=1:1:count {
Set xdata=compiledclass.XDatas.GetAt(i)
Set configName=xdata.Name
If configName="" Continue
#dim reader As %XML.Reader=##class(%XML.Reader).%New()
Set reader.KeepWhitespace=0
Set sc=reader.OpenStream(xdata.Data)
If $$$ISERR(sc) Quit
#dim document As %XML.Document=reader.Document
#dim node As %XML.Node=document.GetDocumentElement()
If node="" Continue
If node.LocalName'="Mapping" Continue
If node.Namespace'="http://www.intersystems.com/jsonmapping" {
Set sc=$$$ERROR($$$JSONUnexpectedNamespace,node.QName,$$$mappingNameDisplay(configName))
Quit
}
#; Do reader.CorrelateRoot("%JSON.Mapping")
Do reader.CorrelateRoot("%ZJSON.Mapping")
If 'reader.Next(.obj,.sc) Quit
If reader.Next(.dummy) {
Set sc=$$$ERROR($$$JSONExtraMapping,$$$mappingNameDisplay(configName))
Quit
}
Set sc=..GenerateMappingFromClass(class,configName,obj,.mapping)
If $$$ISERR(sc) Quit
}
Quit sc
]]></Implementation>
</Method>
<Method name="GenerateMappingFromClass">
<Description>
Generate mapping based on imported configuration info from Xdata block
ClassMethod GenerateMappingFromClass(class As %String, name As %String, obj As %JSON.Mapping, ByRef mapping) As %Status [ Internal ]</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec><![CDATA[class:%String,name:%String,obj:%ZJSON.Mapping,&mapping]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
kill mapping(name)
// Get %JSONIGNOREINVALIDFIELD parameter
Set jsonignoreinvalidfield=obj.IgnoreInvalidField
// Get %JSONMAPPING class parameter.
Set jsondefaultmapping=obj.Mapping
// Get default for %JSONNULL
Set jsondefaultnull=''obj.Null
// Get default for %JSONIGNORENULL
Set jsondefaultignorenull=''obj.IgnoreNull
// Get default for %JSONREFERENCE
Set jsondefaultreference=$zcvt(obj.Reference,"U")
If jsondefaultreference="" Set jsondefaultreference="OBJECT"
If $case(jsondefaultreference,"OBJECT":0,"ID":0,"OID":0,"GUID":0,:1) {
Quit $$$ERROR($$$JSONInvalidDefaultParameter,"attribute reference",$$$mappingNameDisplay(name))
}
// Save mappng parameterd for entire class
Set mapping(name,0)=$lb(class,jsonignoreinvalidfield)
// Create map node for each property
#; #dim property As %JSON.MappingProperty
#dim property As %ZJSON.MappingProperty
Set sc=$$$OK
For i=1:1:obj.propertyList.Count() {
Set property=obj.propertyList.GetAt(i)
If property="" {
Set sc=$$$ERROR($$$JSONNoPropertyName,$$$mappingNameDisplay(name))
Quit
}
Set propertyName=property.Name
If '$$$comMemberDefined(class,$$$cCLASSproperty,propertyName) {
Set sc=$$$ERROR($$$JSONUnexpectedPropertyName,propertyName,$$$mappingNameDisplay(name))
Quit
}
// Get %JSONINCLUDE property parameter
Set jsoninclude=$zcvt(property.Include,"L")
Set includeProperty=$case(jsoninclude,"":"","inout":1,"inputonly":1,"outputonly":1,"none":1,:-1)
If includeProperty=-1 {
Set sc=$$$ERROR($$$JSONInvalidPropertyParameter,"attribute Include",property,$$$mappingNameDisplay(name))
Quit
}
// Get details of the property's type
Do ..GetMemberInfo(class,propertyName,.includeProperty,.type,.membercat)
If includeProperty {
// Default for %JSONINCLUDE is "inout"
If jsoninclude="" Set jsoninclude="inout"
// Do not support array of streams
If membercat["STREAM",membercat["Array" {
Set sc=$$$ERROR($$$JSONNoStreamArray,property,$$$mappingNameDisplay(name))
Quit
}
// For string types, set literaltype to string, base64 or hex.
Set literaltype=""
If membercat["CSTREAM" {
Set literaltype="string"
} ElseIf membercat["BSTREAM" {
Set encoding=$$$comMemberArrayGet(class,$$$cCLASSproperty,propertyName,$$$cPROPparameter,"ENCODING")
Set literaltype=$case($zcvt(encoding,"L"),"":"base64","base64":"base64","hex":"hex",:"")
If literaltype="" {
Set sc=$$$ERROR($$$XMLInvalidEncoding,encoding,property)
Quit
}
} ElseIf membercat["LITERAL" {
Set literaltype=$$$comMemberKeyGet(type,$$$cCLASSparameter,"JSONTYPE",$$$cPARAMdefault)
If literaltype="" Set literaltype="string"
// We need to differentiate between registered object strings and JSON strings.
// For example between %String and %Timestamp.
// A string is considered a registered object string if XSDTYPE, as well as JSONTYPE, is = "string".
If literaltype="string",$$$comMemberKeyGet(type,$$$cCLASSparameter,"XSDTYPE",$$$cPARAMdefault)'="string" {
Set literaltype="string/json"
}
}
// Find if required
Set required=$$$comMemberKeyGet(class,$$$cCLASSproperty,propertyName,$$$cPROPrequired)
// Get JSON field name. If not specified, then it defaults to the property name.
Set jsonfieldname=property.FieldName
If jsonfieldname="" Set jsonfieldname=propertyName
// Get %JSONNULL property parameter. Default is %JSONNULL class parameter.
Set jsonnull=property.Null
Set jsonnull=$select(jsonnull="":jsondefaultnull,1:''jsonnull)
// Get %JSONIGNORENULL property parameter. Default is %JSONIGNORENULL class parameter.
If literaltype="string" {
Set jsonignorenull=property.IgnoreNull
Set jsonignorenull=$select(jsonignorenull="":jsondefaultignorenull,1:''jsonignorenull)
} Else {
Set jsonignorenull=0
}
// Get %JSONREFERENCE property parameter. Default is %JSONREFERENCE class parameter.
If membercat["OBJ" {
Set jsonreference=$zcvt(property.Reference,"U")
If jsonreference="" Set jsonreference=jsondefaultreference
Set jsonreference=$case(jsonreference,
"OBJECT":$$$jsonrefobject,
"ID":$$$jsonrefid,
"OID":$$$jsonrefoid,
"GUID":$$$jsonrefguid,
:"")
If jsonreference="" {
Set sc=$$$ERROR($$$JSONInvalidPropertyParameter,"attribute Reference",property)
Quit
}
// Get JSON mapping for object references.
Set jsonmapping=property.Mapping
If jsonmapping="" Set jsonmapping=jsondefaultmapping
} Else {
Set jsonreference=""
}
// Set the node
Set mapping(name,$increment(mapping(name)))=
$lb(propertyName,$$$QN(propertyName),jsoninclude,jsonfieldname,jsonnull,jsonignorenull,jsonreference,required,literaltype,membercat,type,jsonmapping)
}
}
Quit sc
]]></Implementation>
</Method>
<Method name="%JSONGenerate">
<Description>
%JSONGenerate creates thee metadata structure and generates the code for all methods.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>%mode,%class,%property,%method,%parameter,%codemode,%code,%classmodify,%context</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set %code=0
If '$$$comMemberKeyGet(%class,$$$cCLASSparameter,"%JSONENABLED",$$$cPARAMdefault) Quit $$$OK
Set abstract=$$$comClassKeyGet(%class,$$$cCLASSabstract)
Set sc=$$$OK
Set sc=..GenerateMapping(%class,.mapping)
If $$$ISERR(sc) Quit sc
Set sc=..GenerateMappingFromXdata(%class,.mapping)
If $$$ISERR(sc) Quit sc
// Create the map from class properties
If 'abstract {
If $$$ISOK(sc) {
Set sc=..GenerateAllImportInternal(%class,.mapping)
}
If $$$ISOK(sc) {
Set sc=..GenerateAllExportInternal(%class,.mapping,1)
}
If $$$ISOK(sc) {
Set sc=..GenerateAllExportInternal(%class,.mapping,0)
}
}
Quit sc
]]></Implementation>
</Method>
<Method name="JSONImportInternal">
<Description>
Get the code for the %JSONImportInternal method to actually do the method generation</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>%mode,%class,%property,%method,%parameter,%codemode,%code,%classmodify,%context</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set %code=0
If $$$comMemberKeyGet(%class,$$$cCLASSparameter,"%JSONENABLED",$$$cPARAMdefault) {
Merge %code=$$$tEXTcode("%JSONImportInternal")
}
Quit $$$OK
]]></Implementation>
</Method>
<Method name="JSONExportInternal">
<Description>
Get the code for the %JSONExportInternal method to actually do the method generation</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>%mode,%class,%property,%method,%parameter,%codemode,%code,%classmodify,%context</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set %code=0
If $$$comMemberKeyGet(%class,$$$cCLASSparameter,"%JSONENABLED",$$$cPARAMdefault) {
Merge %code=$$$tEXTcode("%JSONExportInternal")
}
Quit $$$OK
]]></Implementation>
</Method>
<Method name="JSONExportToStringInternal">
<Description>
Get the code for the %JSONExportToStringInternal method to actually do the method generation</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>%mode,%class,%property,%method,%parameter,%codemode,%code,%classmodify,%context</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set %code=0
If $$$comMemberKeyGet(%class,$$$cCLASSparameter,"%JSONENABLED",$$$cPARAMdefault) {
Merge %code=$$$tEXTcode("%JSONExportToStringInternal")
}
Quit $$$OK
]]></Implementation>
</Method>
<Method name="JSONNew">
<ClassMethod>1</ClassMethod>
<FormalSpec>%mode,%class,%property,%method,%parameter,%codemode,%code,%classmodify,%context</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set %code=0
If $$$comMemberKeyGet(%class,$$$cCLASSparameter,"%JSONENABLED",$$$cPARAMdefault) {
$$$GENERATE(" Quit ##class("_%class_").%New()")
}
Quit $$$OK
]]></Implementation>
</Method>
<Method name="GenerateAllImportInternal">
<Description>
Generate the code for the %JSONImportInternal method</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec><![CDATA[class:%String,&mapping:%String]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
New %code
Set %code=0
Set sc=$$$OK
Try {
$$$GENERATE(" Set sc=$$$OK")
Set needTestInvalidField=0
Set needRequired=0
// Process all mappings except base
Set indent=""
Set mappingName=$c(1)
Set count=0
For {
Set mappingName=$order(mapping(mappingName))
If mappingName="" Quit
$$$GENERATE(" "_$select(count=0:"If",1:"} ElseIf")_" %mappingName="""_mappingName_""" {")
Set count=count+1
Set indent=" "
Set sc=..GenerateImportInternal(class,indent,mappingName,.mapping,.needRequired,.needTestInvalidField)
If $$$ISERR(sc) Quit
}
If $$$ISERR(sc) Quit
// Add in base mapping.
If count>0 $$$GENERATE(" } Else {")
Set sc=..GenerateImportInternal(class,indent,$c(1),.mapping,.needRequired,.needTestInvalidField)
If $$$ISERR(sc) Quit
If count>0 $$$GENERATE(" }")
// Generate code to check for invalid field
If needTestInvalidField {
$$$GENERATE(" If $get(testInvalidField) {")
$$$GENERATE(" Set invalid=0,iterator=%JSONObject.%GetIterator()")
$$$GENERATE(" While iterator.%GetNext(.key,.value) { If $data(proplist(key))=0 Set invalid=1 Quit }")
$$$GENERATE(" If invalid Quit $$$ERROR($$$JSONImportBadField,key,$$$mappingNameDisplay(%mappingName))")
$$$GENERATE(" }")
}
$$$GENERATE("%JSONImportExit Quit sc")
$$$GENERATE("%JSONImportError Quit $$$ERROR($$$JSONImportError,field,$$$mappingNameDisplay(%mappingName))")
If needRequired $$$GENERATE("%JSONImportRequired Quit $$$ERROR($$$JSONImportRequired,field,$$$mappingNameDisplay(%mappingName))")
// Save the code for this method
Merge $$$tEXTcode("%JSONImportInternal")=%code
} Catch ex {
Set sc=ex.AsStatus()
}
Quit sc
]]></Implementation>
</Method>
<Method name="GenerateImportInternal">
<Description>
Generate the code for the %JSONImportInternal method for the specified mappingName.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec><![CDATA[class:%String,indent:%String,mappingName:%String,&mapping:%String,&needRequired:%Boolean,&needTestInvalidField:%Boolean]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Set sc=$$$OK
Set ignoreInvalidField=$$$jsonignoreinvalidfield(mapping,mappingName)
If 'ignoreInvalidField {
Set needTestInvalidField=1
$$$GENERATE(indent_" Set testInvalidField=1")
}
For index=1:1:$$$jsonpropertycount(mapping,mappingName) {
Set propertyMap=$$$jsongetprop(mapping,mappingName,index)
Set fieldname=$replace($$$jsonfieldname(propertyMap),"""","""""")
// Code to indicate property seen
If 'ignoreInvalidField {
$$$GENERATE(indent_" Set proplist("""_fieldname_""")=""""")
}
// Skip processing for outputonly and none values of JSONINCLUDE.
If $$$jsoninclude(propertyMap)'["in" Continue
// Code to test if field is present
$$$GENERATE(indent_" Set field="""_fieldname_""" Set jsontype=$$$JSONGetTypeOf(%JSONObject,field)")
$$$GENERATE(indent_" If jsontype'=""unassigned"" {")
$$$GENERATE(indent_" If jsontype=""null"" {")
// null value does not satisfy field required.
If $$$jsonrequired(propertyMap) {
$$$GENERATE(indent_" Goto %JSONImportRequired")
} Else {
$$$GENERATE(indent_" Set .."_$$$jsonpropertyQN(propertyMap)_"=""""")
}
$$$GENERATE(indent_" } Else {")
// Generate code to import this property.
Set membercat=$$$jsonmembercat(propertyMap)
If membercat["Collection" {
// Generate code to import collections
If membercat["List" {
// Generate code to import lists
Do ..GenImportList(class,propertyMap,indent_" ")
} Else {
// Generate code to import arrays
Do ..GenImportArray(class,propertyMap,indent_" ")
}
} Else {
// Generate code to import this non-collection field
Do ..GenImportField(class,propertyMap,indent_" ",0)
}
// Handle checking for field required.
If $$$jsonrequired(propertyMap) {
$$$GENERATE(indent_" } Else { Goto %JSONImportRequired }")
Set needRequired=1
} Else {
$$$GENERATE(indent_" }")
}
}
Quit sc
]]></Implementation>
</Method>
<Method name="GenImportList">
<Description>
Generate code for importing a JSON array as a List collection.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String</FormalSpec>
<ReturnType>%Boolean</ReturnType>
<Implementation><![CDATA[
// If present, then must be the expected JSON array type
$$$GENERATE(indent_" If jsontype'=""array"" Goto %JSONImportError")
$$$GENERATE(indent_" Set data=%JSONObject."_$$$QN($$$jsonfieldname(propertyMap)))
// Get each element of the array.
$$$GENERATE(indent_" For index=0:1:data.%Size()-1 {")
// Get the field value
$$$GENERATE(indent_" Set jsontype=$$$JSONGetTypeOf(data,index)")
$$$GENERATE(indent_" If jsontype=""null"" {")
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".Insert("""")")
$$$GENERATE(indent_" } Else {")
$$$GENERATE(indent_" Set value=data.%Get(index)")
// Generate code to import list element
Do ..GenImportField(class,propertyMap,indent_" ",$$$isCollectionList)
$$$GENERATE(indent_" }}")
]]></Implementation>
</Method>
<Method name="GenImportArray">
<Description>
Generate code for importing an array value from JSON object of the form {"key":value,...}</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String</FormalSpec>
<ReturnType>%Boolean</ReturnType>
<Implementation><![CDATA[
// If present, then must be the expected JSON object type
$$$GENERATE(indent_" If jsontype'=""object"" Goto %JSONImportError")
$$$GENERATE(indent_" Set data=%JSONObject."_$$$QN($$$jsonfieldname(propertyMap)))
$$$GENERATE(indent_" Set iterator=data.%GetIterator()")
$$$GENERATE(indent_" While iterator.%GetNext(.index,.value) {")
$$$GENERATE(indent_" Set jsontype=$$$JSONGetTypeOf(data,index)")
$$$GENERATE(indent_" If jsontype=""null"" {")
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".SetAt("""",index)")
$$$GENERATE(indent_" } Else {")
Do ..GenImportField(class,propertyMap,indent_" ",$$$isCollectionArray)
$$$GENERATE(indent_" }}")
]]></Implementation>
</Method>
<Method name="GenImportField">
<Description>
Generate code for importing a single field value.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String,isCollectionValue:%Integer</FormalSpec>
<ReturnType>%Boolean</ReturnType>
<Implementation><![CDATA[
// Generate code to import this property.
Set membercat=$$$jsonmembercat(propertyMap)
If membercat["OBJ" {
Set reference=$$$jsonreference(propertyMap)
If reference=$$$jsonrefid {
// Generate code to import object ID.
Do ..GenImportID(class,propertyMap,indent,isCollectionValue)
} ElseIf reference=$$$jsonrefoid {
// Generate code to import object OID.
Do ..GenImportOID(class,propertyMap,indent,isCollectionValue)
} ElseIf reference=$$$jsonrefguid {
// Generate code to import object GUID.
Do ..GenImportGUID(class,propertyMap,indent,isCollectionValue)
} Else {
// Generate code to import object references.
Do ..GenImportObject(class,propertyMap,indent,isCollectionValue)
}
} ElseIf membercat["STREAM" {
// Input stream
If membercat="BSTREAM" {
// Generate code to import binary stream
Do ..GenImportBinaryStream(class,propertyMap,indent,isCollectionValue)
} Else {
// Generate code to import character stream
Do ..GenImportCharacterStream(class,propertyMap,indent,isCollectionValue)
}
} Else {
// Generate code to import literals
Do ..GenImportLiteral(class,propertyMap,indent,isCollectionValue)
}
$$$GENERATE(indent_"}")
]]></Implementation>
</Method>
<Method name="GenImportID">
<Description>
Generate code for importing an object ID and storing the object reference in variable named data.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String,isCollectionValue:%Integer</FormalSpec>
<Implementation><![CDATA[
// If present, then must be the expected JSON type
$$$GENERATE(indent_" If jsontype'=""string"",jsontype'=""number"" Goto %JSONImportError")
// Get the field value
If isCollectionValue {
Set indexarg=$select(isCollectionValue=$$$isCollectionList:"index+1",1:"index")
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".SetObjectIdAt(value,"_indexarg_")")
} Else {
$$$GENERATE(indent_" Do .."_$$$QN($$$jsonproperty(propertyMap)_"SetObjectId")_"(%JSONObject."_$$$QN($$$jsonfieldname(propertyMap))_")")
}
]]></Implementation>
</Method>
<Method name="GenImportOID">
<Description>
Generate code for importing an object OID and storing the object reference in variable named data.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String,isCollectionValue:%Integer</FormalSpec>
<Implementation><![CDATA[
// If present, then must be the expected JSON type
$$$GENERATE(indent_" If jsontype'=""string"" Goto %JSONImportError")
// Get the field value
Set var=$select(isCollectionValue:"value",1:"data")
Set exp="$lb($piece("_var_","","",2,*),$piece("_var_","","",1))"
If isCollectionValue {
Set indexarg=$select(isCollectionValue=$$$isCollectionList:"index+1",1:"index")
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".SetObjectAt("_exp_","_indexarg_")")
} Else {
$$$GENERATE(indent_" Set data=%JSONObject."_$$$QN($$$jsonfieldname(propertyMap)))
$$$GENERATE(indent_" Do .."_$$$QN($$$jsonproperty(propertyMap)_"SetObject")_"("_exp_")")
}
]]></Implementation>
</Method>
<Method name="GenImportGUID">
<Description>
Generate code for importing an object GUID and storing the object reference in variable named data.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String,isCollectionValue:%Integer</FormalSpec>
<Implementation><![CDATA[
// If present, then must be the expected JSON type
$$$GENERATE(indent_" If jsontype'=""string"" Goto %JSONImportError")
// Get the field value
If isCollectionValue {
Set indexarg=$select(isCollectionValue=$$$isCollectionList:"index+1",1:"index")
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".SetObjectAt(##class(%Library.GUID).%GUIDFind(value),"_indexarg_")")
} Else {
$$$GENERATE(indent_" Do .."_$$$QN($$$jsonproperty(propertyMap)_"SetObject")_"(##class(%Library.GUID).%GUIDFind(%JSONObject."_$$$QN($$$jsonfieldname(propertyMap))_"))")
}
]]></Implementation>
</Method>
<Method name="GenImportObject">
<Description>
Generate code for importing a referenced object and storing in variable named data.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String,isCollectionValue:%Integer</FormalSpec>
<Implementation><![CDATA[
Set mappingName=$$$jsonmapping(propertyMap)
// If present, then must be the expected JSON type
$$$GENERATE(indent_" If jsontype=""object"" {")
// Get a new empty object
$$$GENERATE(indent_" Set newobj=##class("_$$$jsontype(propertyMap)_").%JSONNew(%JSONObject,$this)")
// Get the field value
$$$GENERATE(indent_" Set saveJSON=%JSONObject")
If mappingName'="" {
$$$GENERATE(indent_" Set saveMapping=%mappingName,%mappingName="""_mappingName_"""")
}
$$$GENERATE(indent_" Set %JSONObject="_$select(isCollectionValue:"value",1:"%JSONObject."_$$$QN($$$jsonfieldname(propertyMap))))
// Call adapter for referenced object.
$$$GENERATE(indent_" Set sc=newobj.%JSONImportInternal()")
$$$GENERATE(indent_" Set %JSONObject=saveJSON")
If mappingName'="" {
$$$GENERATE(indent_" Set %mappingName=saveMapping")
}
$$$GENERATE(indent_" If $$$ISERR(sc) Goto %JSONImportExit")
If isCollectionValue=$$$isCollectionList {
// Generate code to save value in list
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".Insert(newobj)")
} ElseIf isCollectionValue=$$$isCollectionArray {
// Generate code to save value in array
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".SetAt(newobj,index)")
} Else {
// Code to store the property value
$$$GENERATE(indent_" Set .."_$$$jsonpropertyQN(propertyMap)_"=newobj")
}
$$$GENERATE(indent_" } ElseIf jsontype=""oref"" {")
If isCollectionValue=$$$isCollectionList {
// Generate code to save value in list
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".Insert(value)")
} ElseIf isCollectionValue=$$$isCollectionArray {
// Generate code to save value in array
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".SetAt(value,index)")
} Else {
// Code to store the property value
$$$GENERATE(indent_" Set .."_$$$jsonpropertyQN(propertyMap)_"=%JSONObject."_$$$QN($$$jsonfieldname(propertyMap)))
}
$$$GENERATE(indent_" } Else { Goto %JSONImportError }")
]]></Implementation>
</Method>
<Method name="GenImportBinaryStream">
<Description>
Generate code for getting the field value from a binary stream and storing in variable named data.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String,isCollectionValue:%Integer</FormalSpec>
<Implementation><![CDATA[
Set property=$$$jsonproperty(propertyMap)
// If present, then must be the expected JSON type
$$$GENERATE(indent_" If jsontype'=""string"" Goto %JSONImportError")
// Get the field value
If isCollectionValue {
// Code for new stream to insert
Set indexarg=$select(isCollectionValue=$$$isCollectionList:"index+1",1:"index")
$$$GENERATE(indent_" Set stream=..##class("_$$$jsontype(propertyMap)_").%New()")
If $$$jsonliteraltype(propertyMap)["hex" {
$$$GENERATE(indent_" For i=1:2:$length(value) {")
$$$GENERATE(indent_" Set bin=bin_$char($zhex($extract(value,i,i+1)))")
$$$GENERATE(indent_" }")
$$$GENERATE(indent_" Set sc=stream.Write(value) If $$$ISERR(sc) Goto %JSONImportExit")
} Else {
$$$GENERATE(indent_" Set sc=stream.Write($system.Encryption.Base64Decode(value)) If $$$ISERR(sc) Goto %JSONImportExit")
}
If isCollectionValue=$$$isCollectionList {
// Generate code to save value in list
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".Insert(stream)")
} Else {
// Generate code to save value in array
$$$GENERATE(indent_" Do .."_$$$jsonpropertyQN(propertyMap)_".SetAt(stream,index)")
}
} Else {
// Code to use property stream
$$$GENERATE(indent_" Set stream=.."_$$$jsonpropertyQN(propertyMap)_" Do stream.Rewind()")
If $$$jsonliteraltype(propertyMap)["hex" {
$$$GENERATE(indent_" Set value=%JSONObject."_$$$QN($$$jsonfieldname(propertyMap))_",bin=""""")
$$$GENERATE(indent_" For i=1:2:$length(value) {")
$$$GENERATE(indent_" Set bin=bin_$char($zhex($extract(value,i,i+1)))")
$$$GENERATE(indent_" }")
$$$GENERATE(indent_" Set sc=stream.Write(bin) If $$$ISERR(sc) Goto %JSONImportExit")
} Else {
$$$GENERATE(indent_" Set sc=stream.Write($system.Encryption.Base64Decode(%JSONObject."_$$$QN($$$jsonfieldname(propertyMap))_")) If $$$ISERR(sc) Goto %JSONImportExit")
}
}
]]></Implementation>
</Method>
<Method name="GenImportCharacterStream">
<Description>
Generate code for getting the field value from a character stream and storing in variable named data.</Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String,propertyMap:%List,indent:%String,isCollectionValue:%Integer</FormalSpec>
<Implementation><![CDATA[
Set property=$$$jsonproperty(propertyMap)
// If present, then must be the expected JSON type
$$$GENERATE(indent_" If jsontype'=""string"" Goto %JSONImportError")
// Get the field value
If isCollectionValue {
// Code for new stream to insert
Set indexarg=$select(isCollectionValue=$$$isCollectionList:"index+1",1:"index")
$$$GENERATE(indent_" Set stream=..##class("_$$$jsontype(propertyMap)_").%New()")