-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSerializer_MTC.xojo_code
1291 lines (1026 loc) · 33 KB
/
Serializer_MTC.xojo_code
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
#tag Class
Protected Class Serializer_MTC
#tag Method, Flags = &h21
Private Shared Function AreArraysEqual(v1 As Variant, v2 As Variant, propType As String) As Boolean
// Assumes that these are the same kinds of arrays
// and neither is nil
dim r as Boolean = true
select case True
case propType = "String()" or v1.ArrayElementType = Variant.TypeString
dim arr1() as string = v1
dim arr2() as string = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if StrComp( arr1( i ), arr2( i ), 0 ) <> 0 then
r = false
exit
end if
next
end if
case propType = "UInt8()"
dim arr1() as UInt8 = v1
dim arr2() as UInt8 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Int8()"
dim arr1() as Int8 = v1
dim arr2() as Int8 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "UInt16()"
dim arr1() as UInt16 = v1
dim arr2() as UInt16 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Int16()"
dim arr1() as Int16 = v1
dim arr2() as Int16 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "UInt32()"
dim arr1() as UInt32 = v1
dim arr2() as UInt32 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Int32()" or v1.ArrayElementType = Variant.TypeInteger
dim arr1() as Int32 = v1
dim arr2() as Int32 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "UInt64()"
dim arr1() as UInt64 = v1
dim arr2() as UInt64 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Int64()" or v1.ArrayElementType = Variant.TypeLong
dim arr1() as Int64 = v1
dim arr2() as Int64 = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Double()" or v1.ArrayElementType = Variant.TypeDouble
dim arr1() as Double = v1
dim arr2() as Double = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Single()" or v1.ArrayElementType = v1.TypeSingle
dim arr1() as single = v1
dim arr2() as single = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Currency()" or v1.ArrayElementType = Variant.TypeCurrency
dim arr1() as currency = v1
dim arr2() as currency = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Boolean()" or v1.ArrayElementType = Variant.TypeBoolean
dim arr1() as boolean = v1
dim arr2() as boolean = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if arr1( i ) <> arr2( i ) then
r = false
exit
end if
next
end if
case propType = "Variant()"
dim arr1() as variant = v1
dim arr2() as variant = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if not AreValuesEqual( arr1( i ), arr2( i ), "" ) then
r = false
exit
end if
next
end if
case propType = "Object()"
dim arr1() as Object = v1
dim arr2() as Object = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if not AreObjectsEqual( arr1( i ), arr2( i ) ) then
r = false
exit
end if
next
end if
case else // Must be some object or variant
dim arr1() as Object = v1
dim arr2() as Object = v2
if arr1.Ubound <> arr2.Ubound then
r = false
else
for i as integer = 0 to arr1.Ubound
if not AreValuesEqual( arr1( i ), arr2( i ), "" ) then
r = false
exit
end if
next
end if
end select
return r
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Shared Function AreDictionariesEqual(d1 As Dictionary, d2 As Dictionary) As Boolean
dim keys1() as Variant = d1.Keys
dim keys2() as Variant = d2.Keys
if not AreArraysEqual( keys1, keys2, "" ) then
return false
end if
dim values1() as Variant = d1.Values
dim values2() as Variant = d2.Values
if not AreArraysEqual( values1, values2, "" ) then
return false
end if
return true
End Function
#tag EndMethod
#tag Method, Flags = &h0
Shared Function AreObjectsEqual(o1 As Object, o2 As Object) As Boolean
// A utility method to compare two objects to make sure they contain the same data
if o1 is nil and o2 is nil then
return true
end if
if o1 is nil or o2 is nil then
return false
end if
if o1 is o2 then
return true
end if
dim ti1 as Introspection.TypeInfo = Introspection.GetType( o1 )
dim ti2 as Introspection.TypeInfo = Introspection.GetType( o2 )
if ti1.FullName <> ti2.FullName then
return false
end if
dim r as boolean = true // Assume they are the same
if o1 IsA Dictionary then
r = AreDictionariesEqual( Dictionary( o1 ), Dictionary( o2 ) )
elseif o1 IsA JSONItem then
dim j1 as JSONItem = JSONItem( o1 )
dim j2 as JSONItem = JSONItem( o2 )
r = StrComp( j1.ToString, j2.ToString, 0 ) = 0
elseif o1 IsA Date then
dim d1 as Date = Date( o1 )
dim d2 as Date = Date( o2 )
if d1.GMTOffset <> d2.GMTOffset or d1.TotalSeconds <> d2.TotalSeconds then
r = false
end if
end if
if r then // Haven't determined it to be false yet
//
// They are the same class, so let's get the actual data
//
dim props() as Introspection.PropertyInfo = ti1.GetProperties
for each prop as Introspection.PropertyInfo in props
dim v1 as Variant = prop.Value( o1 )
dim v2 as Variant = prop.Value( o2 )
if not AreValuesEqual( v1, v2, prop.PropertyType.Name ) then
r = false
exit
end if
next
end if
return r
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Shared Function AreValuesEqual(v1 As Variant, v2 As Variant, propType As String) As Boolean
if v1.IsNull and v2.IsNull then
return true
end if
if v1.IsNull or v2.IsNull then
return false
end if
if v1 is v2 then
return true
end if
//
// If either is text, make it a string
//
if v1.Type = Variant.TypeText then
v1 = v1.StringValue
end if
if v2.Type = Variant.TypeText then
v2 = v2.StringValue
end if
dim type1 as integer = v1.Type
dim type2 as integer = v2.Type
if type1 <> type2 then
return false
end if
if v1.Type = Variant.TypeObject or v1.Type = Variant.TypeDate then
if not AreObjectsEqual( v1, v2 ) then
return false
end if
elseif v1.IsArray or v1.Type = Variant.TypeArray or propType.Right( 2 ) = "()" then
if not AreArraysEqual( v1, v2, propType ) then
return false
end if
elseif v1.Type = Variant.TypeString then
if StrComp( v1.StringValue, v2.StringValue, 0 ) <> 0 then
return false
end if
else // It's something other than an object, array, or string
if v1 <> v2 then
return false
end if
end if
return true
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function DecodeArray(root As Xojo.Core.Dictionary) As Variant
//
// WARNING: All the possible array types are not represented
//
dim type as string = root.Value( kKeyArrayType )
dim propsChild as Xojo.Core.Dictionary = root.Value( kKeyProperties )
dim elementsChild() as Auto = propsChild.Value( kKeyArrayElements )
select case type
case "String()"
dim arr() as string
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Integer()"
dim arr() as integer
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Int8()"
dim arr() as Int8
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Int16()"
dim arr() as Int16
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Int32()"
dim arr() as Int32
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Int64()"
dim arr() as Int64
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "UInt8()"
dim arr() as UInt8
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "UInt16()"
dim arr() as UInt16
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "UInt32()"
dim arr() as UInt32
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "UInt64()"
dim arr() as UInt64
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Double()"
dim arr() as Double
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Single()"
dim arr() as Single
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Boolean()"
dim arr() as Boolean
for i as integer = 0 to elementsChild.Ubound
arr.Append elementsChild( i )
next
return arr
case "Color()"
dim arr() as Color
for i as integer = 0 to elementsChild.Ubound
dim value as variant = elementsChild( i )
value = FromJSONValue( value )
arr.Append value
next
return arr
case "Object()"
dim arr() as Object
for i as integer = 0 to elementsChild.Ubound
dim value as variant = elementsChild( i )
value = FromJSONValue( value )
arr.Append value
next
return arr
case "Variant()"
dim arr() as Variant
for i as integer = 0 to elementsChild.Ubound
dim value as variant = elementsChild( i )
value = FromJSONValue( value )
arr.Append value
next
return arr
case "Auto()"
dim arr() as Auto
for i as integer = 0 to elementsChild.Ubound
dim value as variant = elementsChild( i )
value = FromJSONValue( value )
arr.Append value
next
return arr
end
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function DecodeColor(propsChild As Xojo.Core.Dictionary) As Color
dim r as integer = propsChild.Value( "Red" )
dim g as integer = propsChild.Value( "Green" )
dim b as integer = propsChild.Value( "Blue" )
dim a as integer = propsChild.Value( "Alpha" )
dim c as color = RGB( r, g, b, a )
return c
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Sub DecodeDictionary(propsChild As Xojo.Core.Dictionary, d As Global. Dictionary)
dim keysChild() as Auto = propsChild.Value( kKeyKeys )
dim valuesChild() as Auto = propsChild.Value( kKeyValues )
for i as integer = 0 to keysChild.Ubound
dim key as variant = keysChild( i )
dim value as variant = valuesChild( i )
key = FromJSONValue( key )
value = FromJSONValue( value )
d.Value( key ) = value
next
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub DecodeDictionary(propsChild As Xojo.Core.Dictionary, d As Xojo.Core.Dictionary)
dim keysChild() as Auto = propsChild.Value( kKeyKeys )
dim valuesChild() as Auto = propsChild.Value( kKeyValues )
for i as integer = 0 to keysChild.Ubound
dim key as variant = keysChild( i )
dim value as variant = valuesChild( i )
key = FromJSONValue( key )
value = FromJSONValue( value )
d.Value( key ) = value
next
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Function DecodeObject(root As Xojo.Core.Dictionary) As Object
if root is nil then
return nil
end if
dim r as Object
dim propsChild as Xojo.Core.Dictionary = root.Value( kKeyProperties )
dim type as string = root.Value( kKeyType )
//
// We have to figure out what class it is and create a new one
//
dim ti as Introspection.TypeInfo = TypeInfoDict.Lookup( type, nil )
if ti is nil then
dim err as new UnsupportedFormatException
err.Message = "Cannot deserialize a class that has not been registered"
raise err
end if
dim constructors() as Introspection.ConstructorInfo = ti.GetConstructors
//
// Look for the zero param constructor
//
for each constructor as Introspection.ConstructorInfo in constructors
dim params() as Introspection.ParameterInfo = constructor.GetParameters
if params.Ubound = -1 then
r = constructor.Invoke
exit
end if
next
if r is nil then
//
// Work around a bug constructing a Dictionary
//
if type = "Dictionary" then
r = new Global.Dictionary
elseif type = "Xojo.Core.Dictionary" then
r = new Xojo.Core.Dictionary
elseif type = "JSONItem" then
r = new JSONItem
else
dim err as new UnsupportedFormatException
err.Message = "Could not find a zero-param Constructor for type """ + type + """"
raise err
end if
end if
dim props() as Introspection.PropertyInfo = ti.GetProperties
for each prop as Introspection.PropertyInfo in props
//
// Make sure we can write this property
//
if not prop.CanWrite then
continue for prop
end if
if prop.IsComputed and not IncludeComputed then
continue for prop
end if
if prop.IsProtected and not IncludeProtected then
continue for prop
end if
if prop.IsPrivate and not IncludePrivate then
continue for prop
end if
//
// Make sure this property is in the properties list
//
if not propsChild.HasKey( prop.Name ) then
continue for prop
end if
dim value as variant = propsChild.Value( prop.Name )
value = FromJSONValue( value )
//
// If an array of object, cycle through
//
dim valueType as integer = value.Type
dim elementType as integer = if( valueType >= 4096, valueType - 4096, -1 )
if elementType = Variant.TypeObject then
dim o() as Object = value
dim a as Auto = prop.Value( r )
dim dest() as Object = a
for i as integer = 0 to o.Ubound
dest.Append o( i )
next
else
prop.Value( r ) = value
end if
next
//
// Handle the special cases
//
if r IsA Global.Dictionary then
DecodeDictionary( propsChild, Global.Dictionary( r ) )
elseif r IsA Xojo.Core.Dictionary then
DecodeDictionary( propsChild, Xojo.Core.Dictionary( r ) )
elseif r IsA JSONItem then
dim j as JSONItem = JSONItem( r )
j.Load propsChild.Value( kKeyJSONStringValue )
elseif r IsA Date then
dim d as Date = Date( r )
d.TotalSeconds = propsChild.Value( "TotalSeconds" ) // Have to make sure this is the last thing set
end if
return r
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function EncodeArray(v As Variant, propType As String) As Xojo.Core.Dictionary
//
// WARNING: All the possible array types are not represented
//
//
// Determine the type of array it is first
//
dim root as new Xojo.Core.Dictionary
root.Value( kKeyType ) = "Array"
dim propsChild as new Xojo.Core.Dictionary
root.Value( kKeyProperties ) = propsChild
dim elementsChild() as Auto
propsChild.Value( kKeyArrayElements ) = elementsChild
dim elementType as integer = v.ArrayElementType
if propType = "" and elementType = Variant.TypeObject then
propType = "Object()"
end if
select case elementType
case Variant.TypeString
root.Value( kKeyArrayType ) = "String()"
propsChild.Value( kKeyArrayElements ) = v
case Variant.TypeText
root.Value( kKeyArrayType ) = "Text()"
propsChild.Value( kKeyArrayElements ) = v
case Variant.TypeDouble
root.Value( kKeyArrayType ) = "Double()"
propsChild.Value( kKeyArrayElements ) = v
case Variant.TypeSingle
root.Value( kKeyArrayType ) = "Single()"
propsChild.Value( kKeyArrayElements ) = v
case Variant.TypeInteger
if propType = "UInt32()" then
root.Value( kKeyArrayType ) = propType
else
root.Value( kKeyArrayType ) = "Int32()"
end
propsChild.Value( kKeyArrayElements ) = v
case Variant.TypeLong
if propType = "UInt64()" then
root.Value( kKeyArrayType ) = propType
else
root.Value( kKeyArrayType ) = "Int64()"
end
dim arr() as Int64 = v
for i as integer = 0 to arr.Ubound
elementsChild.Append arr( i )
next
case Variant.TypeBoolean
root.Value( kKeyArrayType ) = "Boolean()"
propsChild.Value( kKeyArrayElements ) = v
case Variant.TypeCurrency
root.Value( kKeyArrayType ) = "Currency()"
dim arr() as Currency = v
for i as integer = 0 to arr.Ubound
elementsChild.Append arr( i )
next
case Variant.TypeColor
root.Value( kKeyArrayType ) = "Color()"
dim arr() as color = v
for i as integer = 0 to arr.Ubound
dim c as color = arr( i )
elementsChild.Append EncodeColor( c )
next
case else
select case propType
case "Auto()"
root.Value( kKeyArrayType ) = "Auto()"
dim arr() as Auto = v
for i as integer = 0 to arr.Ubound
dim value as variant = ToJSONValue( arr( i ), "" )
elementsChild.Append value
next
case else // Treat is at variant
root.Value( kKeyArrayType ) = "Variant()"
//
// You can't go from a Variant that holds an array of some objects directly to
// an Object array, but you can go from an Auto that holds that array
// to an Object array. Am I exploiting a bug? Maybe, but it works.
//
dim a as Auto = v
dim arr() as Variant = a
for i as integer = 0 to arr.Ubound
elementsChild.Append ToJSONValue( arr( i ), "" )
next
end
end
return root
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function EncodeColor(c As Color) As Xojo.Core.Dictionary
dim root as new Xojo.Core.Dictionary
root.Value( kKeyType ) = "Color"
dim propsChild as new Xojo.Core.Dictionary
root.Value( kKeyProperties ) = propsChild
propsChild.Value( "Red" ) = c.Red
propsChild.Value( "Green" ) = c.Green
propsChild.Value( "Blue" ) = c.Blue
propsChild.Value( "Alpha" ) = c.Alpha
return root
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function EncodeDictionary(dict As Global.Dictionary) As Xojo.Core.Dictionary
dim root as new Xojo.Core.Dictionary
root.Value( kKeyType ) = "Dictionary"
dim propsChild as new Xojo.Core.Dictionary
root.Value( kKeyProperties ) = propsChild
dim keysChild() as Auto
propsChild.Value( kKeyKeys ) = keysChild
dim valuesChild() as Auto
propsChild.Value( kKeyValues ) = valuesChild
dim keys() as variant = dict.Keys
dim values() as variant = dict.Values
for i as integer = 0 to keys.Ubound
dim key as variant = keys( i )
dim value as variant = values( i )
key = ToJSONValue( key, "" )
value = ToJSONValue( value, "" )
keysChild.Append key
valuesChild.Append value
next
return root
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function EncodeDictionary(dict As Xojo.Core.Dictionary) As Xojo.Core.Dictionary
dim root as new Xojo.Core.Dictionary
root.Value( kKeyType ) = "Dictionary"
dim propsChild as new Xojo.Core.Dictionary
root.Value( kKeyProperties ) = propsChild
dim keysChild() as Auto
propsChild.Value( kKeyKeys ) = keysChild
dim valuesChild() as Auto
propsChild.Value( kKeyValues ) = valuesChild
for each entry as Xojo.Core.DictionaryEntry in dict
dim key as variant = ToJSONValue( entry.Key, "" )
dim value as variant = ToJSONValue( entry.Value, "" )
keysChild.Append key
valuesChild.Append value
next
return root
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function EncodeJSONItem(j As JSONItem) As Xojo.Core.Dictionary
// Converts a stored JSONItem to a serialized version
dim root as new Xojo.Core.Dictionary
root.Value( kKeyType ) = Introspection.GetType( j ).FullName
dim propsChild as new Xojo.Core.Dictionary
root.Value( kKeyProperties ) = propsChild
dim s as string
dim cnt as integer
s = j.ToString
cnt = j.Count
propsChild.Value( kKeyJSONStringValue ) = s
propsChild.Value( kKeyJSONCount ) = cnt
return root
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function EncodeObject(o As Object) As Xojo.Core.Dictionary
dim root as Xojo.Core.Dictionary
if o is nil then
//
// Return nil
//
else
//
// Get all the properties via Introspection
// Structure will be:
// Type: FullName
// Properties:
// PropName: Value
//
dim ti as Introspection.TypeInfo = Introspection.GetType( o )
//
// Make sure this is a registered class
//
if not TypeInfoDict.HasKey( ti.FullName ) then
dim err as new UnsupportedFormatException
err.Message = "Attempting to serialize a class that has not been registered"
raise err
end if
dim propsChild as Xojo.Core.Dictionary
if o IsA Dictionary then
root = EncodeDictionary( Dictionary( o ) )
propsChild = root.Value( kKeyProperties )
elseif o IsA Xojo.Core.Dictionary then
root = EncodeDictionary( Xojo.Core.Dictionary( o ) )
propsChild = root.Value( kKeyProperties )
elseif o IsA JSONItem then
root = EncodeJSONItem( JSONItem( o ) )
propsChild = root.Value( kKeyProperties )
else
root = new Xojo.Core.Dictionary
propsChild = new Xojo.Core.Dictionary
root.Value( kKeyProperties ) = propsChild
end if
root.Value( kKeyType ) = ti.FullName
dim props() as Introspection.PropertyInfo = ti.GetProperties
for each prop as Introspection.PropertyInfo in props
if not prop.CanRead or not prop.CanWrite then
//
// We only want properties that we can read and write back later
//
continue for prop
end if
if prop.IsComputed and not IncludeComputed then
continue for prop
end if
if prop.IsProtected and not IncludeProtected then
continue for prop
end if