This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvcmi_json.pas
1450 lines (1187 loc) · 32.4 KB
/
vcmi_json.pas
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
{ This file is a part of Map editor for VCMI project
Copyright (C) 2013-2017 Alexander Shishkin [email protected]
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit vcmi_json;
{$I compilersetup.inc}
{$INTERFACES CORBA}
interface
uses
Classes, SysUtils, fpjson, fgl, RegExpr, vcmi_fpjsonrtti, typinfo, filesystem_base,
editor_classes, editor_types, editor_rtti;
type
TSubstMap = specialize TFPGMap<TJSONStringType,TJSONStringType>;
{ TVCMIJsonString }
TVCMIJsonString = class (TJSONString)
private
FMeta: AnsiString;
procedure SetMeta(AValue: AnsiString);
public
function Clone: TJSONData; override;
property Meta: AnsiString read FMeta write SetMeta;
end;
{ TVCMIJsonObject }
TVCMIJsonObject = class(TJSONObject)
private
FMergeOverride: Boolean;
FMeta: AnsiString;
procedure SetMergeOverride(AValue: Boolean);
public
function Clone: TJSONData; override;
procedure SetMeta(AValue: AnsiString; Recursive: Boolean = true);
property Meta: AnsiString read FMeta;
procedure InheritFrom(ABase:TVCMIJsonObject);
property MergeOverride: Boolean read FMergeOverride write SetMergeOverride;
end;
{ TVCMIJsonArray }
TVCMIJsonArray = class(TJSONArray)
private
FMeta: AnsiString;
public
function Clone: TJSONData; override;
procedure SetMeta(AValue: AnsiString; Recursive: Boolean = true);
property Meta: AnsiString read FMeta;
end;
{ TVCMIJSONDestreamer }
TStreamerObjectEvent = procedure (const JSON: TJSONObject; AObject: TObject) of object;
TVCMIJSONDestreamer = class (TJSONDeStreamer)
private
procedure DestreamEmbeddedValue(ASrc: TJSONData; AObject: TObject);
procedure DestreamCollectionItem(ACollection: TCollection; ASrc: TJSONData; AItem: TCollectionItem);
procedure CollectionObjCallback(Const AName : TJSONStringType; Item: TJSONData;
Data: TObject; var Continue: Boolean);
procedure CollectionArrayCallback(Item: TJSONData; Data: TObject; var Continue: Boolean);
procedure PostProcessTags(AObject: TVCMIJsonObject);
protected
procedure DoPreparePropName(var PropName: AnsiString); override;
procedure DoRestoreProperty(AObject: TObject; PropInfo: PPropInfo; PropData: TJSONData); override;
//preprocess comments, postprocess tags
function ObjectFromString(const JSON: TJSONStringType): TJSONData; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Procedure DoDeStreamCollection(Const JSON : TJSONData; ACollection : TCollection);
Procedure JSONToCollection(Const JSON : TJSONData; ACollection : TCollection); override;
procedure JSONToObjectEx(const JSON: TJSONData; AObject: TObject); override;
procedure JSONStreamToObject(AStream: TStream; AObject: TObject; AName: string);
function JSONStreamToJson(AStream: TStream): TJSONData;
function JSONStreamToJSONObject(AStream: TStream; AName: string): TJSONObject;
end;
{ TVCMIJSONStreamer }
TVCMIJSONStreamer = class (TJSONStreamer)
private
function EmbeddedValueToJson(Aobject: TObject): TJSONData;
protected
procedure DoBeforeStreamProperty(const AObject: TObject;
PropertyInfo: PPropInfo; var Skip: boolean); override;
procedure DoPreparePropName(var PropName: AnsiString); override;
public
constructor Create(AOwner: TComponent); override;
function ObjectToJsonEx(const AObject: TObject): TJSONData; override;
function StreamCollection(const ACollection: TCollection): TJSONData;
override;
function DoStreamCollection(const ACollection: TCollection): TJSONData;
end;
ISerializeSpecial = interface ['ISerializeSpecial']
function Serialize(AHandler: TVCMIJSONStreamer): TJSONData;
procedure Deserialize(AHandler: TVCMIJSONDestreamer; ASrc: TJSONData);
end;
{ TJsonResource }
TJsonResource = class (TBaseResource, IResource)
private
FRoot: TJSONObject;
destreamer: TVCMIJSONDestreamer;
public
constructor Create(APath: AnsiString);
destructor Destroy; override;
procedure LoadFromStream(AFileName: AnsiString; AStream: TStream); override;
property Root: TJSONObject read FRoot;
procedure DestreamTo(AObject: TObject; AFieldName: string = '');
end;
{ TJsonCombinedResource }
TJsonCombinedResource = class (TJsonResource)
public
procedure LoadFromStream(AFileName: AnsiString; AStream: TStream); override;
procedure Load(ALoader: IResourceLoader); override;
function TryLoad(ALoader: IResourceLoader): Boolean; override;
end;
TJsonObjectList = specialize TFPGObjectList<TJSONObject>;
{ TModdedConfig }
TModdedConfig = class
private
FConfig: TVCMIJSONObject;
FModId: TModId;
FPatches: TVCMIJSONObject;
procedure SetModId(AValue: TModId);
public
constructor Create;
destructor Destroy; override;
property ModId: TModId read FModId write SetModId;
property Config: TVCMIJSONObject read FConfig;
property Patches: TVCMIJSONObject read FPatches;
end;
{ TModdedConfigs }
TModdedConfigs = class
strict private
type
TMap = specialize TObjectMap<TModId, TModdedConfig>;
var
FMap: TMap;
procedure Preload(AProgess: IProgressCallback; APaths: TModdedConfigPaths; ALoader:IResourceLoader);
procedure ExtractPatches;
procedure ApplyPatches;
procedure CombineTo(ADest: TJSONObject);
public
constructor Create;
destructor Destroy; override;
procedure Load(AProgess: IProgressCallback; APaths: TModdedConfigPaths; ALoader:IResourceLoader; ADest: TJSONObject);
end;
{ TJSONObjectHelper }
TJSONObjectHelper = class helper for TJSONObject
public
function GetOrCreateObject(AName: AnsiString): TJSONObject;
procedure Assign(AValue: TJSONObject);
end;
procedure MergeJson(ASrc: TJSONData; ADest: TJSONData);
procedure ParseObjectId(AID: AnsiString; out AModId: AnsiString; out AObjectId: AnsiString);
//serialise helpers
procedure SaveHeroSex(ADest: TJSONData; AValue: THeroSex);
function LoadHeroSex(ASrc: TJSONData):THeroSex;
implementation
uses
LazLoggerBase, editor_consts, editor_utils, rttiutils, types;
var
rexp_oid: TRegExpr;
rexp_tags: TRegExpr;
procedure MergeJsonStruct(ASrc: TVCMIJsonObject; ADest: TVCMIJsonObject; AllowOverride: Boolean); forward;
procedure MergeJsonStruct(ASrc: TVCMIJsonArray; ADest: TVCMIJsonArray); forward;
procedure DoSetMeta(ATarget: TJSONData; AValue: AnsiString);
begin
case ATarget.JSONType of
jtString: TVCMIJsonString(ATarget).SetMeta(AValue);
jtObject: TVCMIJsonObject(ATarget).SetMeta(AValue,True);
jtArray: TVCMIJsonArray(ATarget).SetMeta(AValue,True);
end;
end;
procedure MakeCamelCase(var s:string);
var
c: char;
begin
c := s[1];
c := lowerCase(c);
s[1] := c;
end;
procedure MergeJson(ASrc: TJSONData; ADest: TJSONData);
begin
if(ASrc.JSONType = jtNull) then
begin
ADest.Clear;
exit;
end;
if not (ASrc.JSONType = ADest.JSONType) then
begin
raise EJSON.CreateFmt('Incompatible JSON values %s -> %s',[JSONTypeName(ASrc.JSONType), JSONTypeName(ADest.JSONType)]);
end;
case ASrc.JSONType of
jtNull: assert(false);
jtArray: MergeJsonStruct(ASrc as TVCMIJsonArray, ADest as TVCMIJsonArray) ;
jtBoolean: ADest.AsBoolean := ASrc.AsBoolean ;
jtNumber:begin
case TJSONNumber(ASrc).NumberType of
ntFloat: ADest.AsFloat := ASrc.AsFloat ;
ntInt64: ADest.AsInt64 := ASrc.AsInt64;
ntInteger: ADest.AsInteger := ASrc.AsInteger;
end;
end;
jtObject:MergeJsonStruct(ASrc as TVCMIJsonObject, ADest as TVCMIJsonObject, true) ;
jtString:ADest.AsString := ASrc.AsString;
else
begin
raise EJSON.Create('Unknown JSON type');
end;
end;
end;
procedure MergeJsonStruct(ASrc: TVCMIJsonObject; ADest: TVCMIJsonObject; AllowOverride: Boolean);
var
src_idx, dest_idx: Integer;
name: TJSONStringType;
begin
if AllowOverride and ASrc.MergeOverride then
begin
ADest.Clear;
src_idx := ASrc.Count - 1;
while src_idx >= 0 do
begin
name := ASrc.Names[src_idx];
ADest.Add(name, ASrc.Extract(src_idx));
dec(src_idx);
end;
end
else
begin
src_idx := ASrc.Count - 1;
while src_idx >= 0 do
begin
name := ASrc.Names[src_idx];
dest_idx := ADest.IndexOfName(name);
if dest_idx >=0 then
begin
if ADest.Types[name] = jtNull then
begin
ADest.Delete(dest_idx);
ADest.Add(name, ASrc.Extract(src_idx));
end
else if Asrc.Types[name] = jtNull then
begin
ADest.Delete(dest_idx);
end
else
begin
MergeJson(ASrc.Items[src_idx],ADest.Items[dest_idx]);
end;
end
else
begin
ADest.Add(name, ASrc.Extract(src_idx));
end;
dec(src_idx);
end;
end;
end;
procedure MergeJsonStruct(ASrc: TVCMIJsonArray; ADest: TVCMIJsonArray);
var
idx: SizeInt;
begin
if ASrc.Count = 0 then
exit;
ADest.Clear;
idx := ASrc.Count - 1;
while idx >= 0 do
begin
ADest.Insert(0, ASrc.Extract(idx));
dec(idx);
end;
end;
procedure ParseObjectId(AID: AnsiString; out AModId: AnsiString; out
AObjectId: AnsiString);
begin
if not rexp_oid.Exec(AID) then
begin
raise EConfigurationError.CreateFmt('Invalid object ID %s',[AID]);
end;
AModId:=rexp_oid.Match[2];
AObjectId:=rexp_oid.Match[3];
Assert(Length(AObjectId)>0,'ObjectId is empty');
end;
procedure SaveHeroSex(ADest: TJSONData; AValue: THeroSex);
var
o: TJSONObject;
begin
o := ADest as TJSONObject;
case AValue of
//THeroSex.default: o.Add('female');
THeroSex.male: o.Add('female', false);
THeroSex.female: o.Add('female', True);
end;
end;
function LoadHeroSex(ASrc: TJSONData): THeroSex;
var
o: TJSONObject;
idx: Integer;
begin
Result := THeroSex.default;
o := ASrc as TJSONObject;
idx := o.IndexOfName('female');
if idx > -1 then
begin
case o.Get('female', false) of
false: Result := THeroSex.male;
true: Result := THeroSex.female;
end;
end;
end;
{ TJsonCombinedResource }
procedure TJsonCombinedResource.LoadFromStream(AFileName: AnsiString; AStream: TStream);
var
current: TJSONObject;
begin
if not Assigned(FRoot) then
begin
FRoot := CreateJSONObject([]);
end;
current := destreamer.JSONStreamToJSONObject(AStream,'');
try
MergeJson(current, Root);
finally
current.Free;
end;
end;
procedure TJsonCombinedResource.Load(ALoader: IResourceLoader);
begin
ALoader.LoadResourceCombined(Self, Typ, Path);
end;
function TJsonCombinedResource.TryLoad(ALoader: IResourceLoader): Boolean;
begin
Result:= ALoader.ExistsResource(Self.Typ, Self.Path);
if Result then
begin
Load(ALoader);
end;
end;
{ TVCMIJsonArray }
function TVCMIJsonArray.Clone: TJSONData;
begin
Result:=inherited Clone;
TVCMIJsonArray(Result).SetMeta(Meta, False);
end;
procedure TVCMIJsonArray.SetMeta(AValue: AnsiString; Recursive: Boolean);
var
iter: TJSONEnum;
begin
FMeta:=AValue;
if Recursive then
for iter in self do
begin
DoSetMeta(iter.Value, AValue);
end;
end;
{ TVCMIJsonObject }
procedure TVCMIJsonObject.SetMergeOverride(AValue: Boolean);
begin
if FMergeOverride=AValue then Exit;
FMergeOverride:=AValue;
end;
function TVCMIJsonObject.Clone: TJSONData;
begin
Result:=inherited Clone;
TVCMIJsonObject(Result).SetMeta(Meta, False);
end;
procedure TVCMIJsonObject.SetMeta(AValue: AnsiString; Recursive: Boolean);
var
iter: TJSONEnum;
begin
FMeta:=AValue;
if Recursive then
for iter in self do
begin
DoSetMeta(iter.Value, AValue);
end;
end;
procedure TVCMIJsonObject.InheritFrom(ABase: TVCMIJsonObject);
var
temp: TVCMIJsonObject;
s, old_meta: String;
begin
old_meta := Meta;
temp := ABase.Clone as TVCMIJsonObject;
MergeJsonStruct(Self, temp, false);
Clear;
while temp.Count > 0 do
begin
s := temp.Names[0];
Self.Add(s, temp.Extract(0));
end;
temp.Free;
SetMeta(old_meta, True);
end;
{ TVCMIJsonString }
procedure TVCMIJsonString.SetMeta(AValue: AnsiString);
begin
if FMeta=AValue then Exit;
FMeta:=AValue;
end;
function TVCMIJsonString.Clone: TJSONData;
begin
Result:=inherited Clone;
TVCMIJsonString(Result).Meta:=Meta;
end;
{ TJSONObjectHelper }
function TJSONObjectHelper.GetOrCreateObject(AName: AnsiString): TJSONObject;
var
idx: SizeInt;
begin
idx := IndexofName(AName);
if idx < 0 then
begin
Result := CreateJSONObject([]);
add(AName, Result);
end
else begin
Result := Objects[AName];
end;
end;
procedure TJSONObjectHelper.Assign(AValue: TJSONObject);
var
iter: TJSONEnum;
begin
if (Self is TVCMIJsonObject) and (AValue is TVCMIJsonObject) then
begin
TVCMIJsonObject(Self).FMeta:=TVCMIJsonObject(AValue).Meta;
end;
Self.Clear;
for iter in AValue do
begin
Self.Add(iter.Key, iter.Value.Clone);
end;
end;
{ TJsonResource }
constructor TJsonResource.Create(APath: AnsiString);
begin
inherited Create(TResourceType.Json, APath);
destreamer := TVCMIJSONDestreamer.Create(nil);
end;
procedure TJsonResource.DestreamTo(AObject: TObject; AFieldName: string);
begin
if AFieldName = '' then
begin
destreamer.JSONToObject(FRoot,AObject);
end
else begin
destreamer.JSONToObject(FRoot.Objects[AFieldName],AObject);
end;
end;
destructor TJsonResource.Destroy;
begin
FreeAndNil(FRoot);
destreamer.Free;
end;
procedure TJsonResource.LoadFromStream(AFileName: AnsiString; AStream: TStream);
begin
FreeAndNil(FRoot);
FRoot := destreamer.JSONStreamToJSONObject(AStream,'');
Assert(FRoot is TVCMIJsonObject);
end;
{ TVCMIJSONStreamer }
constructor TVCMIJSONStreamer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
function TVCMIJSONStreamer.ObjectToJsonEx(const AObject: TObject): TJSONData;
begin
if AObject is ISerializeNotify then
begin
(AObject as ISerializeNotify).BeforeSerialize(Self);
end;
if AObject is ISerializeSpecial then
begin
Result := (AObject as ISerializeSpecial).Serialize(Self);
end
else if AObject is IEmbeddedValue then
begin
Result := EmbeddedValueToJson(AObject);
end
else begin
Result := ObjectToJSON(AObject);
end;
if AObject is ISerializeNotify then
begin
(AObject as ISerializeNotify).AfterSerialize(Self, Result);
end;
end;
function TVCMIJSONStreamer.EmbeddedValueToJson(Aobject: TObject): TJSONData;
var
PIL: TPropInfoList;
saved: Boolean;
I: Integer;
begin
PIL:=TPropInfoList.Create(AObject,tkProperties);
saved:=false;
try
For I:=0 to PIL.Count-1 do
begin
if NeedToStreamProperty(AObject, PIL.Items[i]) then
begin
if saved then
begin
Error('Invalid embedded value');
end;
Result:=StreamProperty(AObject,PIL.Items[i]);
saved:=true;
end;
end;
finally
FReeAndNil(Pil);
end;
end;
procedure TVCMIJSONStreamer.DoBeforeStreamProperty(const AObject: TObject;
PropertyInfo: PPropInfo; var Skip: boolean);
begin
inherited DoBeforeStreamProperty(AObject, PropertyInfo, Skip);
if not IsStoredProp(AObject,PropertyInfo) then
begin
Skip := True;
Exit;
end;
Skip := IsDefaultValue(AObject, PropertyInfo);
end;
procedure TVCMIJSONStreamer.DoPreparePropName(var PropName: AnsiString);
begin
MakeCamelCase(PropName);
end;
function TVCMIJSONStreamer.StreamCollection(const ACollection: TCollection
): TJSONData;
begin
if ACollection is ISerializeNotify then
begin
(ACollection as ISerializeNotify).BeforeSerialize(Self);
end;
if ACollection is ISerializeSpecial then
begin
result := (ACollection as ISerializeSpecial).Serialize(Self);
end
else
Result := DoStreamCollection(ACollection);
if ACollection is ISerializeNotify then
begin
(ACollection as ISerializeNotify).AfterSerialize(Self, Result);
end;
end;
function TVCMIJSONStreamer.DoStreamCollection(const ACollection: TCollection
): TJSONData;
var
O: TJSONObject;
A: TJSONArray;
elem: TCollectionItem;
begin
if ACollection is INamedCollection then
begin
o := CreateJSONObject([]);
for elem in ACollection do
begin
o.Add(TNamedCollectionItem(Elem).Identifier, ObjectToJsonEx(elem));
end;
result := o;
end
else if ACollection is IArrayCollection then
begin
a:=CreateJSONArray([]);
for elem in ACollection do
begin
a.Add(ObjectToJsonEx(elem));
end;
result := a;
end
else begin
Result := inherited StreamCollection(ACollection);
end;
end;
{ TVCMIJSONDestreamer }
procedure TVCMIJSONDestreamer.CollectionArrayCallback(Item: TJSONData;
Data: TObject; var Continue: Boolean);
var
ACollection: TCollection;
new_item: TCollectionItem;
begin
ACollection:=TCollection(Data);
new_item := ACollection.Add;
DestreamCollectionItem(ACollection, Item, new_item);
Continue := True;
end;
procedure TVCMIJSONDestreamer.PostProcessTags(AObject: TVCMIJsonObject);
var
substitution_map: TSubstMap;
iter: TJSONEnum;
key: TJSONStringType;
parts: TStrings;
i: Integer;
merge_override: Boolean;
data: TJSONData;
old_key, new_key: String;
procedure EnsureMap();
begin
if not Assigned(substitution_map) then
begin
substitution_map := TSubstMap.Create;
end;
end;
begin
substitution_map := nil;
parts := TStringList.Create;
for iter in AObject do
begin
key := iter.Key;
parts.Clear;
rexp_tags.Split(key, parts);
if parts.Count > 1 then
begin
EnsureMap();
key := parts[0];
merge_override := false;
for i := 1 to parts.Count - 1 do
begin
if parts[i] = 'override' then
begin
merge_override := true;
end;
end;
if merge_override then
begin
data := iter.Value;
(data as TVCMIJsonObject).MergeOverride:=true;
substitution_map.Add(iter.Key, key);
end;
end;
end;
if Assigned(substitution_map) then
begin
for i := 0 to Pred(substitution_map.Count) do
begin
old_key := substitution_map.Keys[i];
new_key := substitution_map.Data[i];
data := AObject.Extract(old_key);
AObject.Add(new_key, data);
end;
FreeAndNil(substitution_map);
end;
for iter in AObject do
begin
if iter.Value is TVCMIJsonObject then
begin
PostProcessTags(TVCMIJsonObject(iter.Value));
end;
end;
FreeAndNil(parts);
end;
procedure TVCMIJSONDestreamer.DestreamEmbeddedValue(ASrc: TJSONData;
AObject: TObject);
var
PIL: TPropInfoList;
info: PPropInfo;
begin
PIL:=TPropInfoList.Create(AObject,tkProperties);
if PIL.Count <> 1 then
begin
Error('Invallid embedded value');
end;
try
info := PIL[0];
RestoreProperty(AObject, info, ASrc);
finally
FReeAndNil(Pil);
end;
end;
procedure TVCMIJSONDestreamer.DestreamCollectionItem(ACollection: TCollection;
ASrc: TJSONData; AItem: TCollectionItem);
var
O: TJSONObject;
begin
if AItem is IEmbeddedValue then
begin
DestreamEmbeddedValue(ASrc, AItem);
end
else if AItem is ISerializeSpecial then
begin
(AItem as ISerializeSpecial).Deserialize(Self, ASrc);
end
else if ASrc.JSONType in [jtObject]then
begin
O := TJSONObject(ASrc);
JSONToObjectEx(o,AItem);
end
else if ASrc.JSONType in [jtArray] then
begin
if AItem is IEmbeddedCollection then
begin
JSONToCollection(ASrc,(AItem as IEmbeddedCollection).GetCollection);
end
else
Error('Not supported collection element type for item');
end
else if ASrc.JSONType in [jtString] then
begin
SetStrProp(AItem, 'Value', ASrc.AsString);
end
else begin
Error('Not supported collection element type for item');
end;
end;
procedure TVCMIJSONDestreamer.CollectionObjCallback(const AName: TJSONStringType;
Item: TJSONData; Data: TObject; var Continue: Boolean);
var
ACollection: THashedCollection;
new_item: TNamedCollectionItem;
meta: string;
begin
ACollection:=THashedCollection(Data);
if item.JSONType <> jtNull then
begin
new_item := TNamedCollectionItem(ACollection.Add);
if {new_item.UseMeta and} (item is TVCMIJsonObject) then
begin
meta := TVCMIJsonObject(Item).Meta;
new_item.Meta:=Meta;
end
else if {new_item.UseMeta and} (item is TVCMIJsonArray) then
begin
meta := TVCMIJsonArray(Item).Meta;
new_item.Meta:=Meta;
end
else
begin
new_item.Identifier := AName;
end;
if new_item.UseMeta and (meta <> '') then
begin
new_item.Identifier := NormalizeIndentifier(EncodeIdentifier(Meta, AName));
end
else
begin
new_item.Identifier := NormalizeIndentifier(AName);
end;
DestreamCollectionItem(ACollection, Item, new_item);
end;
Continue := True;
end;
constructor TVCMIJSONDestreamer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
destructor TVCMIJSONDestreamer.Destroy;
begin
inherited Destroy;
end;
procedure TVCMIJSONDestreamer.DoDeStreamCollection(const JSON: TJSONData;
ACollection: TCollection);
var
O : TJSONObject;
A: TJSONArray;
begin
if (json.JSONType = jtNull) then
begin
ACollection.Clear;
end
else if (JSON.JSONType = jtObject) and (ACollection is INamedCollection) then
begin
ACollection.Clear;
O := JSON as TJSONObject;
O.Iterate(@CollectionObjCallback,ACollection);
end
else if (JSON.JSONType = jtArray) and (ACollection is IArrayCollection) then
begin
ACollection.Clear;
A := JSON as TJSONArray;
A.Iterate(@CollectionArrayCallback,ACollection);
end
else
begin
inherited JSONToCollection(JSON, ACollection);
end;
end;
procedure TVCMIJSONDestreamer.DoPreparePropName(var PropName: AnsiString);
begin
MakeCamelCase(PropName);
end;
procedure TVCMIJSONDestreamer.DoRestoreProperty(AObject: TObject;
PropInfo: PPropInfo; PropData: TJSONData);
Var
PI : PPropInfo;
TI : PTypeInfo;
str_data : TVCMIJsonString;
value: TJSONStringType;
//idx: SizeInt;
//local_scope: TJSONStringType;
begin
PI:=PropInfo;
TI:=PropInfo^.PropType;
if (TI = TypeInfo(TIdentifier)) then
begin
value := '';
//local_scope := '';
if Assigned(PropData) and (PropData is TVCMIJsonString) then
begin
str_data := TVCMIJsonString(PropData);
value := str_data.AsString;
//local_scope := str_data.Meta;
end;
//todo: may be move identifier resolving here
//if local_scope <> '' then
//begin
// DebugLn(['Loading ident: ', value, '; local scope: ', local_scope]);
//
// idx := pos(':', value);
//
// if idx = 0 then
// begin
// value := local_scope +':'+value;//todo: remove
// end;
//end;
SetStrProp(AObject,PI,value);
end
else if not Assigned(PropData) then
//SetDefault(AObject, PropInfo)