-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-array-size.bmx
1139 lines (1008 loc) · 31.4 KB
/
test-array-size.bmx
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
SuperStrict
Import brl.objectlist
Import "lexer/lexer.bmx"
Import "parser/parser.bmx"
'Include "src/JSON.bmx"
Include "src/TJSONLexer.bmx"
Include "src/TJSONParser.bmx"
' JSON MODULE FOR BLITZMAX
' (c) Copyright Si Dunford, July 2021, All Rights Reserved
' V3.0
' Version 3.0 constants
Const JINVALID:Int = 0
Const JARRAY:Int = 1
Const JBOOLEAN:Int = 2
Const JKEYWORD:Int = 3
Const JNUMBER:Int = 4
Const JNULL:Int = 5
Const JOBJECT:Int = 6
Const JSTRING:Int = 7
' Version 2.5 constants - Depreciated
'Const JSON_INVALID:Int = 0
'Const JSON_ARRAY:Int = 1
'Const JSON_BOOLEAN:Int = 2
'Const JSON_KEYWORD:Int = 3
'Const JSON_NUMBER:Int = 4
'Const JSON_NULL:Int = 5
'Const JSON_OBJECT:Int = 6
'Const JSON_STRING:Int = 7
Type JSON
Private
Field class:Int
Field value:Object
Public
' Error Text, Line and Character
Field errLine:Int = 0
Field errPos:Int = 0
Field errNum:Int = 0
Field errText:String = ""
Method New()
Self.class = JOBJECT
Self.value = New TMap()
End Method
Method New( jtype:Int, value:String="" )
Select jtype
Case JARRAY
Self.class = JARRAY
Self.value = New TObjectList()
' Value Argument Ignored
Case JBOOLEAN
Self.class = JKEYWORD
value = Lower(value)
Select value
Case "true", "false" ; Self.value = value
Case "1" ; Self.value = "true"
Default ; Self.value = "false"
End Select
Case JINVALID
Self.class = JINVALID
Self.value = ""
Case JKEYWORD
Self.class = JKEYWORD
Self.value = value
Case JNUMBER
Self.class = JNUMBER
Self.value = value 'String(Int(value))
Case JNULL
Self.class = JNULL
Self.value = "null"
Case JSTRING
Self.class = JSTRING
Self.value = value
Default
Self.class = JOBJECT
Self.value = New TMap()
' Value Argument Ignored
End Select
End Method
Method New( jtype:Int, value:Object )
Self.class = jtype
Select jtype
Case JARRAY
Self.value = TObjectList(value)
If Self.value; Return
Case JOBJECT
Self.value = TMap(value)
If Self.value; Return
End Select
' Invalid...
Self.class = JINVALID
Self.value = ""
End Method
' Create a JARRAY from a string array
Method New( value:String[] )
For Local str:String = EachIn String[](value)
addlast( New JSON( JSTRING, str ) )
Next
End Method
' Request location of last error
Method error:String()
Return errtext+" ["+errnum+"] at "+errline+":"+errpos
End Method
Public
' Get integer representation of the class
' NOTE: Internally these are strings, but that may change at some point
Method getClassId:Int()
Select class
Case JARRAY, JNUMBER, JSTRING, JOBJECT
Return class
Case JKEYWORD
Select String(value)
Case "true", "false" ; Return JBOOLEAN
Case "null" ; Return JNULL
End Select
Return JKEYWORD
End Select
Return JINVALID
End Method
' Get a string representation of the class
Method getClassName:String()
Select class
Case JARRAY ; Return "array"
Case JNUMBER ; Return "number"
Case JSTRING ; Return "string"
Case JOBJECT ; Return "object"
Case JKEYWORD
Select String(value)
Case "true", "false" ; Return "boolean"
Case "null" ; Return "null"
End Select
Return JKEYWORD
End Select
Return JINVALID
End Method
' Copy / Duplicate a JSON type
Method copy:JSON()
'DebugStop
Select class
Case JARRAY
Local J:JSON = New JSON()
J.class = class
J.value = New TObjectList()
For Local item:JSON = EachIn TObjectList( value )
'addlast( J.value, item.copy() )
J.addlast( item.copy() )
Next
Return J
Case JKEYWORD ; Return New JSON( JKEYWORD, String( value ) )
Case JNUMBER ; Return New JSON( JNUMBER, String( value ) )
Case JSTRING ; Return New JSON( JSTRING, String( value ) )
Case JOBJECT
Local J:JSON = New JSON()
J.class = class
'J.value = New TMap()
Local map:TMap = TMap( value )
For Local key:String = EachIn map.keys()
J.set( key, JSON(map[key]).copy() )
Next
Return J
End Select
Return New JSON( JINVALID )
End Method
Method toArray:JSON[]()
If class<>JARRAY Return [New JSON( JINVALID, "Node is not an array" )]
Return JSON[](TObjectList(value).toArray())
End Method
Method toByte:Byte()
Select class
Case JNUMBER,JSTRING ; Return Long(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toDouble:Double()
Select class
Case JNUMBER,JSTRING ; Return Double(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toFloat:Float()
Select class
Case JNUMBER,JSTRING ; Return Float(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toInt:Int()
Select class
Case JNUMBER,JSTRING ; Return Int(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toLong:Long()
Select class
Case JNUMBER,JSTRING ; Return Long(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toShort:Short()
Select class
Case JNUMBER,JSTRING ; Return Short(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toSizeT:Size_T()
Return toSize_T()
End Method
Method toSize_T:Size_T()
Select class
Case JNUMBER,JSTRING ; Return Size_T(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toString:String()
Return String(value)
End Method
Method toUInt:UInt()
Select class
Case JNUMBER,JSTRING ; Return UInt(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method toULong:ULong()
Select class
Case JNUMBER,JSTRING ; Return ULong(String(value))
Case JKEYWORD
Select String(value)
Case "true" ; Return True
Case "false","null" ; Return False
End Select
End Select
Return 0
End Method
Method isValid:Int()
Return ( class <> JINVALID )
End Method
Method isInvalid:Int()
Return ( class = JINVALID )
End Method
Method isTrue:Int()
Return (class = JKEYWORD And String(value) = "true")
End Method
Method isFalse:Int()
Return (class = JKEYWORD And String(value) = "false")
End Method
Method isNull:Int()
Return (class = JKEYWORD And String(value) = "null")
End Method
Method is:Int( criteria:Int )
Return ( class = criteria )
End Method
' Get value of a JSON object's child using string index
Method operator []:String( key:String )
If class = JOBJECT
Local map:TMap = TMap( value )
If map
'DebugStop
'key = unescape(key)
Local J:JSON = JSON( map.valueforkey(key) )
If J Return String( J.value )
End If
End If
Return ""
End Method
' Get value in a JSON array using integer index
Method operator []:JSON( key:Int )
If class <> JARRAY Return Null
Local items:TObjectList = TObjectList( value )
If key>items.count() Or key<0 Return Null
Return JSON( items.valueatIndex(key) )
'If items
' Local J:JSON = items[key]
' If J Return J
'End If
'Return Null
End Method
' Set Values
Method operator []=( route:String, value:String )
set( route, value )
End Method
Method operator []=( route:String, value:Int )
set( route, value )
End Method
Method operator []=( route:String, value:Float )
set( route, value )
End Method
Method operator []=( route:String, value:JSON )
set( route, value )
End Method
' 14/12/22, Operator overload doesn;t seem to allow "Object" types as params
'Method operator []=( route:String, value:Object )
' Local J:JSON = JSON.serialise( value )
' set( route, J )
'End Method
' Produce a stFring representation of a JSON datatype
Method Stringify:String()
Local txt:String
'DebugStop
'If Not j Return "~q~q"
Select class ' JSON NODE TYPES
Case JOBJECT
Local map:TMap = TMap( value )
txt :+ "{"
If map
For Local key:String = EachIn map.keys()
Local j:JSON = JSON( map[key] )
'Local K:String = key
txt :+ "~q"+key+"~q:"+j.stringify()+","
Next
' Strip trailing comma
If txt.endswith(",") txt = txt[..(Len(txt)-1)]
End If
txt :+ "}"
Case JARRAY
txt :+ "["
'For Local J:JSON = EachIn JSON[](value)
' txt :+ J.stringify()+","
'Next
For Local J:JSON = EachIn TObjectList(value)
txt :+ J.stringify()+","
Next
' Strip trailing comma
If txt.endswith(",") txt = txt[..(Len(txt)-1)]
txt :+ "]"
Case JNUMBER
txt :+ String(value)
Case JSTRING
txt :+ "~q"+escape(String(value))+"~q"
Case JKEYWORD
txt :+ escape(String(value))
Case JINVALID
txt :+ "#ERR#"
Default
fail( "INVALID SYMBOL: '"+class+"', ''" )
End Select
Return txt
End Method
Method Prettify:String( tabs:Int )
Return Prettify( " "[..tabs] )
End Method
Method Prettify:String( tab:String=" " )
Local txt:String = Stringify()
Try
For Local set:String[] = EachIn [["{","{~n"],["[","[~n"],["}","~n}"],["]","~n]"],["~q:","~q: "],[",",",~n"]]
txt = txt.Replace( set[0], set[1] )
Next
Local tokens:String[] = txt.split( "~n" )
Local indent:String
For Local i:Int = 0 Until tokens.length
Local token:String = tokens[i]
If token.startswith("}") Or token.startswith("]") ; indent = indent[..(Len(indent)-Len(tab))]
tokens[i] = indent+token
If token.endswith("{") Or token.endswith("[") ; indent :+ tab
Next
txt = "~n".join( tokens )
Catch e:TRegExException
DebugLog "Error : " + e.toString()
End Try
Return txt
End Method
' Serialise a Blitzmax object into JSON
Function serialise:JSON( obj:Object ) ' British English
Return _Object2JSON( obj )
End Function
Function serialize:JSON( obj:Object ) ' American English
Return _Object2JSON( obj )
End Function
Private
Function _Array2JSON:JSON( array:Object, fieldtype:TTypeId )
Local J:JSON = New JSON( JARRAY )
Local length:Int, dimensions:Int
Try
length = fieldtype.arrayLength( array )
Catch e:String
length = 0
End Try
Try
dimensions = fieldtype.arrayDimensions( array )
Catch e:String
dimensions = 0
End Try
If length = 0 Or dimensions = 0; Return J
Local elementTypeid:TTypeId = fieldtype.ElementType()
'DebugStop
For Local e:Int = 0 Until length
'Local element:JSON
Local fieldname:String = elementTypeID.Name()
'DebugStop
Select ElementTypeId
Case ByteTypeId; J.addlast( New JSON( JNUMBER, fieldtype.GetByteArrayElement( array,e ) ))
Case DoubleTypeId; J.addlast( New JSON( JNUMBER, fieldtype.GetDoubleArrayElement( array,e ) ))
Case FloatTypeId; J.addlast( New JSON( JNUMBER, fieldtype.GetFloatArrayElement( array,e ) ))
Case IntTypeId; J.addlast( New JSON( JNUMBER, fieldtype.GetIntArrayElement( array,e ) ))
Case LongTypeId; J.addlast( New JSON( JNUMBER, fieldtype.GetLongArrayElement( array,e ) ))
Case ShortTypeId; J.addlast( New JSON( JNUMBER, fieldtype.GetShortArrayElement( array,e ) ))
Case SizeTTypeId; J.addlast( New JSON( JNUMBER, fieldtype.GetSizeTArrayElement( array,e ) ))
Case StringTypeId
Local str:String = fieldtype.GetStringArrayElement( array,e )
If str; J.addlast( New JSON( JSTRING, str ) )
Case UIntTypeId; J.addlast( New JSON( JSTRING, fieldtype.GetUIntArrayElement( array,e ) ))
Case ULongTypeId; J.addlast( New JSON( JSTRING, fieldtype.GetULongArrayElement( array,e ) ))
Default
'DebugStop
Select True
Case elementTypeID.extendsType( ArrayTypeID )
Local a:Object = elementTypeID.GetArrayElement( array,e )
J.AddLast( _Array2JSON( a, elementTypeID ) )
Case elementTypeID.extendsType( ObjectTypeId )
Local o:Object = fieldtype.GetArrayElement( array,e )
'Local o:Object = fld.get( obj )
If o
Local objectTypeId:TTypeId = TTypeId.ForObject(o)
J.AddLast( _Object2JSON( o ) )
End If
Default
DebugLog( "Unable to serialize "+fieldName+":"+fieldType.name() )
End Select
End Select
Next
Return J
End Function
Function _Object2JSON:JSON( obj:Object )
Local typeid:TTypeId = TTypeId.ForObject( obj )
Local J:JSON = New JSON()
For Local fld:TField = EachIn typeid.EnumFields()
'DebugStop
If fld.metadata("noserialize") Or fld.metadata("noserialise"); Continue
Local fieldType:TTypeId = fld.TypeID()
Local fieldName:String = fld.name()
If fld.metadata("serializedname"); fieldname = fld.metadata("serializedname")
If fld.metadata("serialisedname"); fieldname = fld.metadata("serialisedname")
'If fieldname.startswith("initialization"); DebugStop
Select fieldType
Case ByteTypeId; J.set( fieldName, fld.GetByte(obj) )
Case DoubleTypeId; J.set( fieldName, fld.GetDouble(obj) )
Case FloatTypeId; J.set( fieldName, fld.GetFloat(obj) )
Case IntTypeId; J.set( fieldName, fld.GetInt(obj) )
Case LongTypeId; J.set( fieldName, fld.GetLong(obj) )
Case ShortTypeId; J.set( fieldName, fld.GetShort(obj) )
Case SizeTTypeId; J.set( fieldName, fld.GetSizeT(obj) )
Case StringTypeId
Local str:String = fld.GetString(obj)
If str; J.set( fieldName, str )
Case UIntTypeId; J.set( fieldName, fld.GetUInt(obj) )
Case ULongTypeId; J.set( fieldName, fld.GetULong(obj) )
Default
'DebugStop
Select True
Case Lower(fieldtype.name())="json"
'Local f:JSON = JSON( fld.get( obj ) )
Local o:Object = fld.get( obj )
If o; J.set( fieldname, JSON( o ).copy() )
Case fieldtype.extendsType( ArrayTypeID )
Local array:Object = fld.get( obj )
J.set( fieldname, _Array2JSON( array, fieldtype ) )
Case fieldtype.extendsType( ObjectTypeId )
Local o:Object = fld.get( obj )
If o
Local objectTypeId:TTypeId = TTypeId.ForObject(o)
J.set( fieldname, _Object2JSON( o ) )
End If
Default
DebugLog( "Unable to serialize "+fieldName+":"+fieldType.name() )
End Select
End Select
'DebugLog( fieldName )
'fields.insert( fld.name(), fld.typeid.name() )
Next
Return J
End Function
Public
' Transpose a JSON object into a Blitzmax Object using Reflection
Method transpose:Object( typestr:String )
'fail( "Transpose('"+typestr+"')" )
If class=JOBJECT; Return _JSON2Object( typestr )
Return fail( "Unable to transpose "+class )
End Method
Private
Method _JSON2Array:Object( typestr:String )
Local typeid:TTypeId = TTypeId.ForName( typestr )
If Not typeid; Return fail( "- '"+typestr+"' is not a Blitzmax Type" )
' Get the list
Local list:TObjectList = TObjectList(value)
'DebugLog( list.count() + " elements" )
'DebugStop
' Create an object of this type
Local array:Object = typeid.NewArray( list.count() )
If Not array Return fail( "Unable to create array of type "+typestr )
' Loop through array elements
For Local index:Int = 0 Until list.count()
Local J:JSON = JSON( list.valueAtIndex( index ) )
typeid.setArrayElement( array, index, J._JSON2Object( typestr[..(Len(typestr)-2)] ) )
Next
Return array
End Method
Method _JSON2Object:Object( typestr:String )
' Get the typeid associated with the type string
Local typeid:TTypeId = TTypeId.ForName( typestr )
If Not typeid; Return fail( "- '"+typestr+"' is not a Blitzmax Type" )
' Create an object of this type
Local obj:Object = typeid.newObject()
If Not obj Return fail( "Unable to create object of type "+typestr )
' Get object map
Local map:TMap = TMap( value )
'DebugStop
' Debug the field list
'For Local key:String = EachIn map.keys()
' Print( ": "+ key )
'Next
' Loop through object fields
For Local fld:TField = EachIn typeid.EnumFields()
' Check if field is marked for non-transpose
If fld.metadata("notranspose"); Continue
' Get field name using serialisedname if provided
Local fieldName:String = Lower( fld.name() )
If fld.metadata("serializedname"); fieldname = Lower( fld.metadata("serializedname") )
If fld.metadata("serialisedname"); fieldname = Lower( fld.metadata("serialisedname") )
'DebugLog( "->"+fieldname )
'fields.insert( fld.name(), fld.typeid.name() )
' Check if field exists in JSON
Local J:JSON = JSON( map.valueforkey( fieldname ) )
If Not J; Continue
'If fieldname = "workspacefolders"; DebugStop
'DebugStop
Select fld.TypeID()
Case ByteTypeId; fld.setByte( obj, J.toByte() )
Case DoubleTypeId; fld.setDouble( obj, J.toDouble() )
Case FloatTypeId; fld.SetFloat( obj, J.toFloat() )
Case IntTypeId; fld.setInt( obj, J.toInt() )
Case LongTypeId; fld.setLong( obj, J.toLong() )
Case ShortTypeId; fld.setShort( obj, J.toShort() )
Case SizeTTypeId; fld.setSizeT( obj, J.toSize_T() )
Case StringTypeId; fld.SetString( obj, J.tostring() )
Case UIntTypeId; fld.setUInt( obj, J.toUInt() )
Case ULongTypeId; fld.setULong( obj, J.toULong() )
Default
Select True
Case Lower( fld.typeid().name() ) = "json"
fld.set( obj, J )
Case fld.typeid().extendsType( ObjectTypeId )
'DebugStop
If fld.typeid().extendsType( ArrayTypeID )
' Type[]
'DebugLog( "Custom type arrays are not supported" )
'DebugStop
fld.set( obj, J._JSON2Array( fld.typeid().name() ) )
Else
' Type
fld.set( obj, J._JSON2Object( fld.typeid().name() ) )
End If
Default
'DebugStop
fail( "Blitzmax Type '"+fld.typeid().name()+"' cannot be transposed()" )
End Select
End Select
Next
' ' Add Field names and types to map
' Local fields:TMap = New TMap()
' For Local fld:TField = EachIn typeid.EnumFields()
' fields.insert( fld.name(), fld.typeid.name() )
' Next
' ' Extract MAP (of JSONs) from value
' Local map:TMap = TMap( value )
' If Not map Return Null
' 'logfile.write( "Map extracted from value successfully" )
' 'for local key:string = eachin map.keys()
' ' logfile.write " "+key+" = "+ JSON(map[key]).toString()
' 'next
Rem
' Transpose fields into object
For Local fldname:String = EachIn fields.keys()
Local fldtype:String = String(fields[fldname]).tolower()
'debuglog( "Field: "+fldname+":"+fldtype )
'logfile.write "- Field: "+fldname+":"+fldtype
Local fld:TField = typeid.findField( fldname )
If fld
'logfile.write "-- Is not null"
Try
Select fldtype ' BLITZMAX TYPES
Case "string"
Local J:JSON = JSON(map[fldname])
If J fld.SetString( invoke, J.tostring() )
Case "int"
Local J:JSON = JSON(map[fldname])
If J fld.setInt( invoke, J.toInt() )
'if J
' local fldvalue:int = J.toInt()
' logfile.write fldname+":"+fldtype+"=="+fldvalue
' fld.setInt( invoke, fldvalue )
' logfile.write "INT FIELD SET"
'end if
Case "json"
' This is a direct copy of JSON
Local J:JSON = JSON(map[fldname])
fld.set( invoke, J )
Default
SendUpdate( "log", "ERRR", "Blitzmax type '"+fldtype+"' cannot be transposed()" )
End Select
Catch Exception:String
SendUpdate( "log", "CRIT", "Transpose exception" )
SendUpdate( "log", "CRIT", Exception )
End Try
'else
'logfile.write "-- Is null"
End If
Next
Return invoke
End Rem
Return obj
End Method
' ##### JSON HELPER
Public
' Set the value of a JSON
Method set( value:String )
If value.startswith("~q") And value.endswith("~q")
' STRING
Self.class = JSTRING
Self.value = dequote(value)
Else
If Lower(value) = "true" Or Lower(value) = "false" Or Lower(value) = "null"
' KEYWORD (true/false/null)
Self.class = JKEYWORD
Self.value = Lower(value)
Else
' Treat it as a string
Self.class = JSTRING
Self.value = value
End If
End If
End Method
Method set( value:Int )
' If existing value is NOT a number, overwrite it
Self.class = JNUMBER
Self.value = String(value)
End Method
Method set( value:Float )
' If existing value is NOT a number, overwrite it
Self.class = JNUMBER
Self.value = String(value)
End Method
Method Set( route:String, value:String )
'_set( route, value, "string" )
Local J:JSON = find( route, True ) ' Find route and create if missing
If J ; J.set( value )
End Method
Method Set( route:String, value:Float )
'_set( route, value, "string" )
Local J:JSON = find( route, True ) ' Find route and create if missing
If J ; J.set( value )
End Method
Method Set( route:String, value:Int )
'_set( route, value, "number" )
Local J:JSON = find( route, True ) ' Find route and create if missing
If J ; J.set( value )
End Method
Method Set( route:String, values:String[][] )
Local J:JSON = find( route, True ) ' Find route and create if missing
For Local value:String[] = EachIn values
'_set( route+"|"+value[0], value[1], "string" )
If value.length=2
Local node:JSON = J.find( value[0], True )
node.set( value[1] )
End If
Next
End Method
' V0.2
' Set a route to an existing JSON
Method Set( route:String, node:JSON )
Local J:JSON = find( route, True ) ' Find route and create if missing
J.class = node.class
J.value = node.value
End Method
' V0.1
Method find:JSON( route:String, createme:Int = False )
' Ignore empty route
route = Trim(route)
If route="" Return New JSON()
' Split up the path
Return find( route.split("|"), createme )
End Method
Method find:JSON( path:String[], createme:Int = False )
'DebugStop
If path.length=0 ' Found!
Return Self
Else
' If child is specified then I MUST be an object right?
Local child:JSON, map:TMap
If class=JOBJECT ' Yay, I am an object.
If value=Null
value = New TMap()
End If
map = TMap( value )
Else
If Not createme Return New JSON() ' Not found
' I must now evolve into an object, destroying my previous identity!
map = New TMap()
class = JOBJECT
value = map
End If
' Does child exist?
child = JSON( map.valueforkey( path[0] ) )
'For Local k:String = EachIn map.keys()
' Print k
'Next
If Not child
If Not createme Return New JSON() ' Not found
' Add a new child
child = New JSON( JSTRING, "" )
map.insert( path[0], child )
End If
Return child.find( path[1..], createme )
End If
End Method
' V0.2
Method exists:Int( route:String )
' Ignore empty route
route = Trim(route)
If route="" Return False
' Split up the path
Return Not( search( route.split("|") ) = Null )
End Method
Method search:JSON( route:String )
' Ignore empty route
route = Trim(route)
If route="" Return Null
' Split up the path
Return search( route.split("|") )
End Method
' Search is like FIND but returns NUL if not found
Method search:JSON( path:String[] )
'DebugStop
If path.length=0 ' Found!
Return Self
Else
' If child is specified then I MUST be an object right?
Local child:JSON, map:TMap
If class=JOBJECT ' Yay, I am an object.
If value=Null
value = New TMap()
End If
map = TMap( value )
Else
Return Null ' Not found
End If
' Does child exist?
child = JSON( map.valueforkey( path[0] ) )
If Not child Return Null ' Not found
Return child.find( path[1..] )
End If
End Method
Method contains:Int( name:String )
' Only an Object contains named children
If class<>JOBJECT ; Return False
' Check I am not empty!
If Not value ; Return False
' Check if I contain the given field
Local map:TMap = TMap( value )
Return map.contains(name)
End Method
' Get SIZE of an ARRAY
Method size:Int()
Select class
Case JARRAY
Return TObjectList(value).count()
Case JOBJECT
Local map:TMap = TMap( value )
If Not map Return 0
Local count:Int = 0
For Local key:String = EachIn map.keys()
count :+1
Next
Return count
End Select
Return 0
End Method
' Insert a record at the beginning of an array
Method addFirst( data:JSON )
'DebugStop
' If class is not an array, upgrade it using existing element as first
Local list:TObjectList = _UpgradeToArray()
' Add value
If list ; list.addFirst( data )
End Method
' Append a record to an ARRAY
Method addLast( data:JSON )
' If class is not an array, upgrade it using existing element as first
Local list:TObjectList = _UpgradeToArray()
' Add value
If list ; list.addlast( data )
End Method
' Removes a record from the beginning of an array
Method RemoveFirst:JSON()
If class = JARRAY
Local list:TObjectList = TObjectList(value)
If list ; Return JSON(list.RemoveFirst())
End If
Return Null
End Method
' Remove the last record of an ARRAY
Method RemoveLast:JSON()
If class = JARRAY
Local list:TObjectList = TObjectList(value)
If list ; Return JSON(list.RemoveLast())
End If
Return Null
End Method
Method _UpgradeToArray:TObjectList()
Select class
Case JNUMBER, JSTRING, JKEYWORD, JOBJECT
' Copy self into another node
Local node:JSON = New JSON( class )
node.value = value
' Create an Array-backed list and insert copy
Local list:TObjectList = New TObjectList()
list.addlast( node )
' Upgrade self to an array
class = JARRAY
value = list
Return list
Case JINVALID
Local list:TObjectList = New TObjectList()
class = JARRAY
value = list
Return list
Case JARRAY
' Do nothing
Return TObjectList(value)
End Select
' We dont support other types
Return Null
End Method
' Method Copy:JSON()
' End Method
' Method merge:JSON( with:JSON )
' End Method
' Event handler
'Method SendUpdate:Object( message:String, state:String, alt:String )
' DebugLog( message + ", " + state + ", "+ alt )
' Return Null
'End Method
Method fail:Object( message:String )
errText = message
Return Null
End Method
Method GetLastError:String()
Return errText
End Method
' Escape a string
Method escape:String( Text:String )
Local escaped:String = Text
escaped = escaped.Replace( "\", "\\" ) ' Reverse Solidus
escaped = escaped.Replace( "~q", "\~q" ) ' Quotation mark