Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit TestSerialiser;
- interface
- uses
- MVCFramework.Serializer.Intf,
- MVCFramework.Serializer.Commons,
- System.Rtti;
- type
- TTestRecord = class
- end;
- TTestString = class
- private
- FValue: string;
- FIsNull: boolean;
- procedure SetValue(const Value: string);
- public
- constructor Create;
- property Value: string Read FValue write SetValue;
- property IsNull: boolean read FIsNull write FIsNull;
- end;
- TTestStringSerializer = class(TInterfacedObject, IMVCTypeSerializer)
- public
- procedure SerializeAttribute(
- const AElementValue: TValue;
- const APropertyName: string;
- const ASerializerObject: TObject;
- const AAttributes: TArray<TCustomAttribute>
- );
- procedure SerializeRoot(
- const AObject: TObject;
- out ASerializerObject: TObject;
- const AAttributes: TArray<TCustomAttribute>;
- const ASerializationAction: TMVCSerializationAction = nil
- );
- procedure DeserializeAttribute(
- var AElementValue: TValue;
- const APropertyName: string;
- const ASerializerObject: TObject;
- const AAttributes: TArray<TCustomAttribute>
- );
- procedure DeserializeRoot(
- const ASerializerObject: TObject;
- const AObject: TObject;
- const AAttributes: TArray<TCustomAttribute>
- );
- end;
- TTestRecordSerializer = class(TInterfacedObject, IMVCTypeSerializer)
- public
- procedure SerializeAttribute(
- const AElementValue: TValue;
- const APropertyName: string;
- const ASerializerObject: TObject;
- const AAttributes: TArray<TCustomAttribute>
- );
- procedure SerializeRoot(
- const AObject: TObject;
- out ASerializerObject: TObject;
- const AAttributes: TArray<TCustomAttribute>;
- const ASerializationAction: TMVCSerializationAction = nil
- );
- procedure DeserializeAttribute(
- var AElementValue: TValue;
- const APropertyName: string;
- const ASerializerObject: TObject;
- const AAttributes: TArray<TCustomAttribute>
- );
- procedure DeserializeRoot(
- const ASerializerObject: TObject;
- const AObject: TObject;
- const AAttributes: TArray<TCustomAttribute>
- );
- end;
- TComplex1 = class(TTestRecord)
- private
- FFld3: TTestString;
- FFld4: TTestString;
- public
- constructor Create;
- destructor Destroy; override;
- property Fld3: TTestString read FFld3 write FFld3;
- property Fld4: TTestString read FFld4 write FFld4;
- end;
- TComplex = class(TTestRecord)
- private
- FFld2: TTestString;
- FFld1: TTestString;
- FC1: TComplex1;
- public
- constructor Create;
- destructor Destroy; override;
- property C1: TComplex1 read FC1 write FC1;
- property Fld1: TTestString read FFld1 write FFld1;
- property Fld2: TTestString read FFld2 write FFld2;
- end;
- implementation
- uses
- JsonDataObjects;
- { TTestStringSerializer }
- procedure TTestStringSerializer.DeserializeAttribute(var AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
- var
- lJson: TJsonObject;
- begin
- lJson := aSerializerObject as TJsonObject;
- var lValue := lJson.S[aPropertyName];
- var lTestString := aElementValue.AsObject as TTestString;
- lTestString.Value := lValue;
- end;
- procedure TTestStringSerializer.DeserializeRoot(const ASerializerObject, AObject: TObject; const AAttributes: TArray<TCustomAttribute>);
- begin
- end;
- procedure TTestStringSerializer.SerializeAttribute(const AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
- begin
- var aTest := aElementValue.AsObject as TTestString;
- if aTest.IsNull then
- begin
- // assign nil to get a value of null, or dont assign it at all and it wont be output
- (aSerializerObject as TJDOJSONObject).O['Zap'] := nil;
- end
- else
- begin
- //aSerializerObject := TJDOJSONObject.Create;
- var lms := aElementValue.AsObject as TTestString;
- (aSerializerObject as TJDOJSONObject).S[aPropertyName] := lms.Value;
- end;
- end;
- procedure TTestStringSerializer.SerializeRoot(const AObject: TObject; out ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>; const ASerializationAction: TMVCSerializationAction);
- begin
- var aTest := aObject as TTestString;
- if aTest.IsNull then
- begin
- end
- else
- begin
- aSerializerObject := TJDOJSONObject.Create;
- (aSerializerObject as TJDOJSONObject).S['Zap'] := 'Geeee';
- end;
- end;
- { TTestString }
- constructor TTestString.Create;
- begin
- FIsNull := true;
- end;
- procedure TTestString.SetValue(const Value: string);
- begin
- FIsNull := false;
- FValue := Value;
- end;
- { TComplex }
- constructor TComplex.Create;
- begin
- FFld2 := TTestString.Create;
- FFld1 := TTestString.Create;
- FC1 := TComplex1.Create;
- end;
- destructor TComplex.Destroy;
- begin
- FFld1.Free;
- FFld2.Free;
- FC1.Free;
- inherited;
- end;
- { TTestRecordSerializer }
- procedure TTestRecordSerializer.DeserializeAttribute(var AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
- begin
- end;
- procedure TTestRecordSerializer.DeserializeRoot(const ASerializerObject, AObject: TObject; const AAttributes: TArray<TCustomAttribute>);
- begin
- end;
- procedure TTestRecordSerializer.SerializeAttribute(const AElementValue: TValue; const APropertyName: string; const ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>);
- begin
- var aTest := aElementValue.AsObject as TComplex;
- //aSerializerObject := TJDOJSONObject.Create;
- var lTestString := aElementValue.AsObject as TTestString;
- (aSerializerObject as TJDOJSONObject).S[aPropertyName] := lTestString.Value;
- end;
- procedure TTestRecordSerializer.SerializeRoot(const AObject: TObject; out ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>; const ASerializationAction: TMVCSerializationAction);
- begin
- aSerializerObject := TJDOJSONObject.Create;
- var c := aObject as TComplex;
- SerializeAttribute(AObject, 'C1', ASerializerObject, AAttributes);
- (aSerializerObject as TJDOJSONObject).O['C1'];
- (aSerializerObject as TJDOJSONObject).O['Fld1'];
- (aSerializerObject as TJDOJSONObject).O['Fld2'];
- end;
- { TComplex1 }
- constructor TComplex1.Create;
- begin
- FFld4 := TTestString.Create;
- FFld3 := TTestString.Create;
- end;
- destructor TComplex1.Destroy;
- begin
- FFld3.Free;
- FFld4.Free;
- inherited;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement