Merge pull request #370 from wgorajek/master

Added serializer for Spring4D TNullableDouble
This commit is contained in:
Daniele Teti 2020-05-03 09:15:08 +02:00 committed by GitHub
commit c78b52f4d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 0 deletions

View File

@ -45,6 +45,7 @@ type
TNullableDate = Spring.Nullable<TDate>;
TNullableTime = Spring.Nullable<TTime>;
TNullableGuid = Spring.TNullableGuid;
TNullableDouble = Spring.TNullableDouble;
implementation

View File

@ -54,6 +54,21 @@ type
const AAttributes: TArray<TCustomAttribute>);
end;
TNullableDoubleSerializer = 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;
TNullableInt64Serializer = class(TInterfacedObject, IMVCTypeSerializer)
public
procedure SerializeAttribute(const AElementValue: TValue; const APropertyName: string;
@ -184,6 +199,7 @@ begin
ASerializer.RegisterTypeSerializer(TypeInfo(TNullableDate), TNullableDateSerializer.Create);
ASerializer.RegisterTypeSerializer(TypeInfo(TNullableTime), TNullableTimeSerializer.Create);
ASerializer.RegisterTypeSerializer(TypeInfo(TNullableGuid), TNullableGuidSerializer.Create);
ASerializer.RegisterTypeSerializer(TypeInfo(TNullableDouble), TNullableDoubleSerializer.Create);
end;
{ TNullableIntegerSerializer }
@ -561,4 +577,53 @@ begin
raise EMVCSerializationException.Create('Not implemented');
end;
{ TNullableDoubleSerializer }
procedure TNullableDoubleSerializer.DeserializeAttribute(
var AElementValue: TValue; const APropertyName: string;
const ASerializerObject: TObject;
const AAttributes: TArray<TCustomAttribute>);
var
LJSON: TJDOJsonObject;
LNullDouble: TNullableDouble;
begin
LJSON := ASerializerObject as TJDOJsonObject;
if LJSON.Values[APropertyName].Typ in [jdtNone, jdtObject] then { json nulls are recognized as jdtObject }
begin
LNullDouble := nil;
end
else
begin
LNullDouble := LJSON.F[APropertyName];
end;
AElementValue := TValue.From<TNullableDouble>(LNullDouble);
end;
procedure TNullableDoubleSerializer.DeserializeRoot(const ASerializerObject,
AObject: TObject; const AAttributes: TArray<TCustomAttribute>);
begin
raise EMVCSerializationException.Create('Not implemented');
end;
procedure TNullableDoubleSerializer.SerializeAttribute(
const AElementValue: TValue; const APropertyName: string;
const ASerializerObject: TObject;
const AAttributes: TArray<TCustomAttribute>);
var
LNullDouble: TNullableDouble;
begin
LNullDouble := AElementValue.AsType<TNullableDouble>;
if LNullDouble.HasValue then
(ASerializerObject as TJDOJsonObject).F[APropertyName] := LNullDouble.Value
else
(ASerializerObject as TJDOJsonObject).Values[APropertyName] := nil;
end;
procedure TNullableDoubleSerializer.SerializeRoot(const AObject: TObject;
out ASerializerObject: TObject; const AAttributes: TArray<TCustomAttribute>;
const ASerializationAction: TMVCSerializationAction);
begin
raise EMVCSerializationException.Create('Not implemented');
end;
end.