-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path%ZJSON.xml
5690 lines (5429 loc) · 264 KB
/
%ZJSON.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.Adaptor">
<Description><![CDATA[
The following table displays the correspondence between JSON field values and
property values that will be implemented by the JSON Adaptor.<br><br>
The actual conversion between registered object values and JSON values will be done by new datatype methods: JSONToLogical and LogicalToJSON.
In addition, a new JSONTYPE parameter will be introduced for the datatype classes
which indicates how the data type will be mapped to JSON.<br><br>
<pre>
JSON Registered object type
object class referenced by property
array - error if all values are not literals %ListOfDatatypes
array - error if all values are not objects %ListOfObjects or relationship
array - error if all values are not literals %List
string %String, %Char, %Name, %FileCharacterStream, %GlobalCharacterStream
string - must be base64 encoded %Binary, %FileBinaryStream, %GlobalBinaryStream
string - ODBC format date %Date
string - ODBC format time %Time
string - ODBC format date/time %DateTime, %TimeStamp
numeric - bounds must match object type %BigInt, %Currency, %Decimal, %Double, %Float, %Integer, %Numeric, %SmallInt, %TinyInt
boolean %Boolean
</pre>
Note that the types %xsd package are all subclasses of base % datatypes and will map as the super type.<br><br>]]></Description>
<Abstract>1</Abstract>
<IncludeCode>%occInclude,%callout,%ZPVA,%ZjsonMap,%ZjsonErrors</IncludeCode>
<IncludeGenerator>%occInclude</IncludeGenerator>
<PropertyClass>%ZJSON.PropertyParameters</PropertyClass>
<System>3</System>
<TimeCreated>65552,76950.608494</TimeCreated>
<Parameter name="%JSONENABLED">
<Description>
If this parameter is true then JSON-enabling methods will be generated.
Otherwise, the method generators do not produce a runnable method.</Description>
<Default>1</Default>
</Parameter>
<Parameter name="%JSONIGNOREINVALIDFIELD">
<Description>
The %JSONIGNOREINVALIDFIELD parameter allows the programmer to control handling of unexpected fields in the JSON input.
The default (%JSONIGNOREINVALIDFIELD = 0) will treat an unexpected field as an error.
If %JSONIGNOREINVALIDFIELD is set = 1, then unexpected fields will be ignored.</Description>
<Type>BOOLEAN</Type>
<Default>0</Default>
</Parameter>
<Parameter name="%JSONNULL">
<Description>
If %JSONNULL is true (=1), then unspecified properties are exported as the null value.
Otherwise the field corresponding to the property is just skipped during export.
The corresponding property parameter overrides this parameter if specified.</Description>
<Type>BOOLEAN</Type>
<Default>0</Default>
</Parameter>
<Parameter name="%JSONIGNORENULL">
<Description><![CDATA[
%JSONIGNORENULL allows the programmer to override the default handling of empty strings for string properties.
This paramneter applies to only true strings which is determined by XSDTYPE = "string" as well as JSONTYPE="string"
By default (%JSONIGNORENULL = 0), empty strings in the JSON input are stored as $c(0)
and $c(0) is written to JSON as the string "". A missing field in the JSON input is always stored as ""
and "" is always output to JSON according to the %JSONNULL parameter.<br>
If %JSONIGNORENULL is set = 1, then both missing fields in the JSON and empty strings are input as "",
and both "" and $c(0) are output as field values of "".
The corresponding property parameter overrides this parameter if specified.]]></Description>
<Type>BOOLEAN</Type>
<Default>0</Default>
</Parameter>
<Parameter name="%JSONREFERENCE">
<Description><![CDATA[
%JSONREFERENCE specifies the default value of the %JSONREFERENCE property parameter that specifies how to
project references to JSON. %JSONREFERENCE may be specified for any property to override this default value.
Possible values of %JSONREFERENCE are "OBJECT", "ID", "OID", "GUID".<br>
="OBJECT" is the default and indicates that the properties of the referenced class are used to represent the referenced object.<br>
="ID" indicates that the id of a persistent or serial class is used to represent the reference.<br>
="OID" indicates that the oid of a persistent or serial class is used to represent the reference.
The form of the oid as projected to JSON will be classname,id.<br>
="GUID" indicates the GUID of a persistent class is used to represent the reference.<br>
The corresponding property parameter overrides this parameter if specified.]]></Description>
<Type>STRING</Type>
<Constraint>OBJECT,ID,OID,GUID</Constraint>
<Default>OBJECT</Default>
<Flags>ENUM</Flags>
</Parameter>
<Method name="%JSONImport">
<Description><![CDATA[
%JSONImport imports JSON or dynamic object input into this object.<br>
The input argument is either JSON as a string or stream, or a subclass of %DynamicAbstractObject.<br>
mappingName is the name of the mapping to use for the import. The base mapping is represented by "" and is the default.]]></Description>
<FormalSpec>input,%mappingName:%String=""</FormalSpec>
<ReturnType>%Status</ReturnType>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[
Try {
Set sc=$$$OK
New %JSONObject
If $isobject(input),input.%IsA("%Library.DynamicAbstractObject") {
// Already a dynamic object
Set %JSONObject=input
} Else {
// A JSON stream or string
Set %JSONObject=##class(%Library.DynamicAbstractObject).%FromJSON(input)
}
// Do the import now.
Set sc=..%JSONImportInternal()
} Catch ex {
Set sc=ex.AsStatus()
}
Quit sc
]]></Implementation>
</Method>
<Method name="%JSONNew">
<Description><![CDATA[
Get an instance of an JSON enabled class.<br><br>
You may override this method to do custom processing (such as initializing
the object instance) before returning an instance of this class.
However, this method should not be called directly from user code.<br>
Arguments:<br>
dynamicObject is the dynamic object with thee values to be assigned to the new object.<br>
containerOref is the containing object instance when called from JSONImport.]]></Description>
<ClassMethod>1</ClassMethod>
<CodeMode>generator</CodeMode>
<FormalSpec>dynamicObject:%DynamicObject,containerOref:%RegisteredObject=""</FormalSpec>
<GenerateAfter>%JSONGenerate</GenerateAfter>
<ReturnType>%RegisteredObject</ReturnType>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[ Quit ##class(%ZJSON.Generator).JSONNew(.%mode,.%class,.%property,.%method,.%parameter,.%codemode,.%code,.%classmodify,.%context)
]]></Implementation>
</Method>
<Method name="%JSONExport">
<Description><![CDATA[
Serialize a JSON enabled class as a JSON document and write it to the current device.<br>
mappingName is the name of the mapping to use for the export. The base mapping is represented by "" and is the default.]]></Description>
<FormalSpec>%mappingName:%String=""</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Try {
Set sc=$$$OK
// Do the export to current device now.
Set sc=..%JSONExportInternal()
} Catch ex {
If ex.%IsA("%Exception.SystemException") {
Set zerror=ex.AsSystemError()
If $piece(zerror,">",1)="<METHOD DOES NOT EXIST" {
Return $$$ERROR($$$JSONRefNotEnabled,$piece(zerror,",",2))
}
}
Set sc=ex.AsStatus()
}
Quit sc
]]></Implementation>
</Method>
<Method name="%JSONExportToStream">
<Description><![CDATA[
Serialize a JSON enabled class as a JSON document and write it to a stream.<br>
mappingName is the name of the mapping to use for the export. The base mapping is represented by "" and is the default.]]></Description>
<FormalSpec><![CDATA[&export:%Stream.Object,%mappingName:%String=""]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
// Save current device
Set io=$io
Try {
// Always output to %FileCharacterStream
If $get(export)="" {
Set export=##class(%FileCharacterStream).%New()
// JSON is always UTF-8
Set export.TranslateTable="UTF8"
Set filestream=1
} Else {
Set filestream = ($zobjclass(export)="%Library.FileCharacterStream")
}
If filestream {
Set stream=export
} Else {
Set stream=##class(%FileCharacterStream).%New()
Set stream.TranslateTable="UTF8"
}
// Force stream's file to open
Set sc=stream.Write("")
// Export JSON to the stream
If $$$ISOK(sc) {
Set file=stream.Filename ; get filename and make current device
Use file:(/NOXY)
Set sc=..%JSONExportInternal()
// Don't Close file to leave stream positioned
Use io
}
// Need to ensdure that LineTerminator is correct for the platform
If $$$ISOK(sc) Set stream.LineTerminator=$select($$$isUNIX:$char(10),1:$char(13,10))
// If we created the stream and caller passed in stream, then copy it to the caller's stream
If 'filestream,$$$ISOK(sc) {
Set sc=export.CopyFrom(stream)
}
} Catch ex {
Set sc=ex.AsStatus()
}
Quit sc
]]></Implementation>
</Method>
<Method name="%JSONExportToString">
<Description><![CDATA[
Serialize a JSON enabled class as a JSON document and return it as a string.<br>
mappingName is the name of the mapping to use for the export. The base mapping is represented by "" and is the default.]]></Description>
<FormalSpec><![CDATA[&%export:%String,%mappingName:%String=""]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Try {
Set sc=$$$OK
// Do the export to current device now.
Set %export=""
Set sc=..%JSONExportToStringInternal()
} Catch ex {
If ex.Name="<MAXSTRING>" {
Set sc=$$$ERROR($$$JSONMaxString,$$$mappingNameDisplay(%mappingName))
} Else {
Set sc=ex.AsStatus()
}
}
Quit sc
]]></Implementation>
</Method>
<Method name="%JSONX">
<Description>
Dummy method which exists to force GenerateAfter code to take affect for %JSONGenerate.
The GenerateAfter will not have affect unless the after method is laterin collating sequence.
All this is necessary to force geenrator methods to be run after member methods are created.
This method is for internal use only. You should not make direct
use of it within your applications. There is no guarantee made about either
the behavior or future operation of this method. </Description>
<Internal>1</Internal>
<Abstract>1</Abstract>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[ Quit
]]></Implementation>
</Method>
<Method name="%JSONGenerate">
<Description><![CDATA[
%JSONGenerate is an internal method that controls the generation of the other methods in %JSON.Adaptor.
If %JSONGenerate is overridden, then JSON meta data and the other generator methods
of %JSON.Adaptor will not be created.<p>]]></Description>
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<CodeMode>generator</CodeMode>
<GenerateAfter>%JSONX</GenerateAfter>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[ Quit ##class(%ZJSON.Generator).%JSONGenerate(.%mode,.%class,.%property,.%method,.%parameter,.%codemode,.%code,.%classmodify,.%context)
]]></Implementation>
</Method>
<Method name="%JSONImportInternal">
<Description>
%JSONImportInternal is internal method used by %JSONImport</Description>
<Internal>1</Internal>
<CodeMode>generator</CodeMode>
<GenerateAfter>%JSONGenerate</GenerateAfter>
<ReturnType>%Status</ReturnType>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[ Quit ##class(%ZJSON.Generator).JSONImportInternal(.%mode,.%class,.%property,.%method,.%parameter,.%codemode,.%code,.%classmodify,.%context)
]]></Implementation>
</Method>
<Method name="%JSONExportInternal">
<Description>
%JSONExportInternal is internal method used by %JSONExport</Description>
<Internal>1</Internal>
<CodeMode>generator</CodeMode>
<GenerateAfter>%JSONGenerate</GenerateAfter>
<ReturnType>%Status</ReturnType>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[ Quit ##class(%ZJSON.Generator).JSONExportInternal(.%mode,.%class,.%property,.%method,.%parameter,.%codemode,.%code,.%classmodify,.%context)
]]></Implementation>
</Method>
<Method name="%JSONExportToStringInternal">
<Description>
%JSONExportToStringInternal is internal method used by %JSONExport</Description>
<Internal>1</Internal>
<CodeMode>generator</CodeMode>
<GenerateAfter>%JSONGenerate</GenerateAfter>
<ReturnType>%Status</ReturnType>
<ServerOnly>1</ServerOnly>
<Implementation><![CDATA[ Quit ##class(%ZJSON.Generator).JSONExportToStringInternal(.%mode,.%class,.%property,.%method,.%parameter,.%codemode,.%code,.%classmodify,.%context)
]]></Implementation>
</Method>
</Class>
<Class name="%ZJSON.Formatter">
<Description>
%JSON.Formatter implements the ability to format JSON documents which are
expressed as strings, text or dynamic objects.
Class %JSON.Formatter Extends %RegisteredObject [ System = 4 ]</Description>
<IncludeCode>%occInclude,%ZPVA</IncludeCode>
<Super>%RegisteredObject</Super>
<TimeCreated>65341,83137.459069</TimeCreated>
<UDLText name="T">
<Content><![CDATA[
/* Copyright (c) 2019 by InterSystems Corporation.
Cambridge, Massachusetts, U.S.A. All rights reserved.
Confidential property of InterSystems Corporation. */
]]></Content>
</UDLText>
<Property name="Indent">
<Description>
Indent specifies that indentation of the JSON output should take place.</Description>
<Type>%Boolean</Type>
<InitialExpression>1</InitialExpression>
</Property>
<Property name="IndentChars">
<Description>
IndentChars specifies the character sequence to be used for each indent level if Indent=1.</Description>
<Type>%String</Type>
<InitialExpression>" "</InitialExpression>
</Property>
<Property name="LineTerminator">
<Description>
LineTerminator specifies the character sequence to terminate eaach line when indenting.</Description>
<Type>%String</Type>
<InitialExpression>$char(13,10)</InitialExpression>
</Property>
<Method name="Format">
<Description><![CDATA[
Format a JSON document using the specified indentation and write it to the current device.<br>
The <var>input</var> argument is either JSON as a string or stream, or a subclass of %DynamicAbstractObject.<br>]]></Description>
<FormalSpec>input</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[ Quit ..FormatInternal(input,"",1)
]]></Implementation>
</Method>
<Method name="FormatToStream">
<Description><![CDATA[
Format a JSON document using the specified indentation and write it to a stream.<br>
The <var>input</var> argument is either JSON as a string or stream, or a subclass of %DynamicAbstractObject.<br>]]></Description>
<FormalSpec><![CDATA[input,&export:%Stream.Object]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
Try {
Set io=""
// Always output to %FileCharacterStream
If $get(export)="" {
Set export=##class(%FileCharacterStream).%New()
// JSON is always UTF-8
Set export.TranslateTable="UTF8"
Set filestream=1
} Else {
Set filestream = ($zobjclass(export)="%Library.FileCharacterStream")
}
If filestream {
Set stream=export
} Else {
Set stream=##class(%FileCharacterStream).%New()
Set stream.TranslateTable="UTF8"
}
// Force stream's file to open
Set sc=stream.Write("")
// Export JSON to the stream
If $$$ISOK(sc) {
// Save current device
Set io=$io
// Make the stream file be the current device
Set file=stream.Filename ; get filename and make current device
Use file:(/NOXY)
Set sc=..FormatInternal(input,"",1)
// Don't Close file to leave stream positioned
Use io
}
// Need to ensure that LineTerminator is correct for the platform
If $$$ISOK(sc) Set stream.LineTerminator=$select($$$isUNIX:$char(10),1:$char(13,10))
// If we created the stream and caller passed in stream, then copy it to the caller's stream
If 'filestream,$$$ISOK(sc) {
Set sc=export.CopyFrom(stream)
}
} Catch ex {
If io'="" Use io
Set sc=ex.AsStatus()
}
Quit sc
]]></Implementation>
</Method>
<Method name="FormatToString">
<Description><![CDATA[
Format a JSON document using the specified indentation and write it to a string.<br>
Serialize a JSON enabled class as a JSON document and return it as a string.<br>]]></Description>
<FormalSpec><![CDATA[input,&export:%String=""]]></FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[ Quit ..FormatInternal(input,.export,0)
]]></Implementation>
</Method>
<Method name="FormatInternal">
<Description>
Internal method to actually do the formatting</Description>
<Internal>1</Internal>
<FormalSpec><![CDATA[input,&export:%String,toDevice:%Boolean]]></FormalSpec>
<Implementation><![CDATA[
Set sc=$$$OK
Try {
// If writing to current device then we need to clear our string buffer for each iteraation of loop
If toDevice Set export=""
// initialize indentation
Set indent=..Indent
If indent {
Set lineTerminator=..LineTerminator
Set indentChars=..IndentChars
Set indentLength=$length(indentChars)
} Else {
Set lineTerminator=""
Set indentChars=""
Set indentLength=0
}
Set currentIndent=""
// If a JSON string, then convert to dynamic object or array before formatting
If $isobject(input),input.%IsA("%Library.DynamicAbstractObject") {
// Already a dynamic object
Set currentContainer=input
} Else {
// A JSON stream or string
Set currentContainer=##class(%Library.DynamicAbstractObject).%FromJSON(input)
}
// Initialize loop variables
Set stackCount=0
Set currentType=$select(currentContainer.%IsA("%Library.DynamicObject"):"object",1:"array")
Set currentIterator=currentContainer.%GetIterator()
Set sep=""
// Push stack
Set iteratorStack(stackCount)=currentIterator
Set containerStack(stackCount)=currentContainer
Set typeStack(stackCount)=currentType
Set stackCount=stackCount+1
// Start output of object or array
Set export=export_$select(currentType="object":"{",1:"[")
If indent {
Set currentIndent=currentIndent_indentChars
}
// Main loop to walk tree without using recursion
For {
// Finish up for this value and prepare for next
Set currentIteratorIsObject=currentIterator.%IsA("%Iterator.Object")
If currentIterator.%GetNext(.key,.current) {
// There is another value in this object or array.
Set currentType=$$$JSONGetTypeOf(currentContainer,key)
// Output , and indentation
Set export=export_sep
If indent Set export=export_lineTerminator_currentIndent
Set sep=","
} Else {
// There are no more values in this array or object
// Output closing } or ] with indentation
If indent {
Set currentIndent=$extract(currentIndent,1,*-indentLength)
Set export=export_lineTerminator_currentIndent
}
Set export=export_$select(currentIteratorIsObject:"}",1:"]")
// Pop stack to make containing object or array the current object or array.
Set stackCount=stackCount-1
If stackCount=0 Quit
Set currentIterator=iteratorStack(stackCount)
Set currentContainer=containerStack(stackCount)
Set currentType=typeStack(stackCount)
Set sep=","
// Get next value at previous level
Continue
}
// If this is an object, then output next field name.
If currentIteratorIsObject {
Set export=export_""""_key_""":"
}
// Stack processing for object and array values
If (currentType="object") || (currentType="array") {
// Push stack
Set iteratorStack(stackCount)=currentIterator
Set containerStack(stackCount)=currentContainer
Set typeStack(stackCount)=currentType
Set sep=""
Set stackCount=stackCount+1
// Setup for loop over object values
Set currentIterator=current.%GetIterator()
Set currentContainer=current
// Start output of object or array
Set export=export_$select(currentType="object":"{",1:"[")
If indent {
Set currentIndent=currentIndent_indentChars
If lineTerminator="" Set lineTerminator=..LineTerminator
}
// Output of string
} ElseIf currentType="string" {
Set export=export_""""_$zcvt(current,"O","JSON")_""""
// Output of boolean
} ElseIf currentType="boolean" {
Set export=export_$select(current:"true",1:"false")
// Output null
} ElseIf currentType="null" {
Set export=export_"null"
// Output of number
} Else {
If $extract(current)="." Set current="0"_current
If $extract(current,1,2)="-." Set current="-0."_$extract(current,3,*)
Set export=export_current
}
// If writing to current device then write buffer and clear our string buffer for each iteraation of loop
If toDevice {
Write export
Set export=""
}
}
// Write the remainder
If toDevice,export'="" {
Write export
}
} Catch ex {
Set sc=ex.AsStatus()
}
Quit sc
]]></Implementation>
</Method>
</Class>
<Class name="%ZJSON.Generator">
<Description><![CDATA[
Generator methods for %ZJSON.Adaptor.<br><br>
This class is used internally by InterSystems IRIS. You should not make direct
use of it within your applications. There is no guarantee made about either
the behavior or future operation of this class.]]></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)
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 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" {