delphimvcframework/sources/MVCFramework.Serializer.Commons.pas

558 lines
14 KiB
ObjectPascal
Raw Normal View History

2017-02-09 19:33:59 +01:00
// ***************************************************************************
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// Collaborators on this file: Ezequiel Juliano M<>ller (ezequieljuliano@gmail.com)
//
2017-02-09 19:33:59 +01:00
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ***************************************************************************
2017-02-09 19:33:59 +01:00
2017-02-07 14:08:36 +01:00
unit MVCFramework.Serializer.Commons;
2017-02-10 14:19:55 +01:00
{$I dmvcframework.inc}
interface
2017-02-07 14:08:36 +01:00
uses
System.Rtti,
System.Classes,
System.SysUtils,
System.DateUtils,
System.TypInfo,
2017-02-07 14:08:36 +01:00
{$IFDEF SYSTEMNETENCODING}
2017-02-07 14:08:36 +01:00
System.NetEncoding,
2017-02-07 14:08:36 +01:00
{$ELSE}
2017-02-07 14:08:36 +01:00
Soap.EncdDecd,
2017-02-07 14:08:36 +01:00
{$ENDIF}
MVCFramework.Commons;
2017-02-07 14:08:36 +01:00
type
TMVCSerializationType = (stDefault, stProperties, stFields);
TMVCNameCase = (ncAsIs, ncUpperCase, ncLowerCase);
TMVCDataType = (dtObject, dtArray);
TMVCIgnoredList = array of string;
EMVCSerializationException = class(Exception)
2017-03-02 12:57:40 +01:00
private
{ private declarations }
2017-03-02 12:57:40 +01:00
protected
{ protected declarations }
2017-03-02 12:57:40 +01:00
public
{ public declarations }
2017-03-02 12:57:40 +01:00
end;
EMVCDeserializationException = class(Exception)
private
{ private declarations }
protected
{ protected declarations }
2017-02-08 11:42:05 +01:00
public
{ public declarations }
2017-02-08 11:42:05 +01:00
end;
MVCValueAsTypeAttribute = class(TCustomAttribute)
2017-02-09 11:24:18 +01:00
private
FValueTypeInfo: PTypeInfo;
protected
{ protected declarations }
2017-02-09 11:24:18 +01:00
public
constructor Create(AValueTypeInfo: PTypeInfo);
function ValueTypeInfo: PTypeInfo;
2017-02-09 11:24:18 +01:00
end;
MVCDoNotSerializeAttribute = class(TCustomAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
2017-02-10 14:19:55 +01:00
MVCSerializeAsStringAttribute = class(TCustomAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCNameCaseAttribute = class(TCustomAttribute)
private
FKeyCase: TMVCNameCase;
function GetKeyCase: TMVCNameCase;
protected
{ protected declarations }
public
constructor Create(const AKeyCase: TMVCNameCase);
property KeyCase: TMVCNameCase read GetKeyCase;
2017-02-10 14:19:55 +01:00
end;
MVCNameAsAttribute = class(TCustomAttribute)
private
FName: string;
function GetName: string;
protected
{ protected declarations }
public
constructor Create(const AName: string);
property Name: string read GetName;
end;
2017-02-09 19:33:59 +01:00
MVCListOfAttribute = class(TCustomAttribute)
2017-02-09 19:33:59 +01:00
private
FValue: TClass;
protected
{ protected declarations }
2017-02-09 19:33:59 +01:00
public
constructor Create(const AValue: TClass);
property Value: TClass read FValue;
2017-02-09 19:33:59 +01:00
end;
MVCDataSetFieldAttribute = class(TCustomAttribute)
private
FDataType: TMVCDataType;
protected
{ protected declarations }
public
constructor Create(const ADataType: TMVCDataType);
property DataType: TMVCDataType read FDataType;
end;
MVCSerializeAttribute = class(TCustomAttribute)
private
FSerializationType: TMVCSerializationType;
protected
{ protected declarations }
public
constructor Create(const ASerializationType: TMVCSerializationType);
property SerializationType: TMVCSerializationType read FSerializationType;
end;
TMVCSerializerHelpful = record
private
{ private declarations }
public
class function GetKeyName(const AField: TRttiField; const AType: TRttiType): string; overload; static;
class function GetKeyName(const AProperty: TRttiProperty; const AType: TRttiType): string; overload; static;
class function HasAttribute<T: class>(const AMember: TRttiNamedObject): Boolean; overload; static;
class function HasAttribute<T: class>(const AMember: TRttiNamedObject; out AAttribute: T): Boolean; overload; static;
class function AttributeExists<T: TCustomAttribute>(const AAttributes: TArray<TCustomAttribute>; out AAttribute: T): Boolean; overload; static;
class function AttributeExists<T: TCustomAttribute>(const AAttributes: TArray<TCustomAttribute>): Boolean; overload; static;
class procedure EncodeStream(AInput, AOutput: TStream); static;
class procedure DecodeStream(AInput, AOutput: TStream); static;
class function EncodeString(const AInput: string): string; static;
class function DecodeString(const AInput: string): string; static;
class procedure DeSerializeStringStream(AStream: TStream; const ASerializedString: string; const AEncoding: string); static;
class procedure DeSerializeBase64StringStream(AStream: TStream; const ABase64SerializedString: string); static;
class function GetTypeKindAsString(const ATypeKind: TTypeKind): String; static;
class function StringToTypeKind(const AValue: String): TTypeKind; static;
class function CreateObject(const AObjectType: TRttiType): TObject; overload; static;
class function CreateObject(const AQualifiedClassName: string): TObject; overload; static;
end;
function DateTimeToISOTimeStamp(const ADateTime: TDateTime): string;
function DateToISODate(const ADate: TDateTime): string;
function TimeToISOTime(const ATime: TTime): string;
function ISOTimeStampToDateTime(const ADateTime: string): TDateTime;
function ISODateToDate(const ADate: string): TDate;
function ISOTimeToTime(const ATime: string): TTime;
2017-02-07 14:08:36 +01:00
implementation
function DateTimeToISOTimeStamp(const ADateTime: TDateTime): string;
var
fs: TFormatSettings;
begin
fs.TimeSeparator := ':';
Result := FormatDateTime('yyyy-mm-dd hh:nn:ss', ADateTime, fs);
end;
2017-02-07 14:08:36 +01:00
function DateToISODate(const ADate: TDateTime): string;
begin
Result := FormatDateTime('YYYY-MM-DD', ADate);
end;
function TimeToISOTime(const ATime: TTime): string;
var
fs: TFormatSettings;
begin
fs.TimeSeparator := ':';
Result := FormatDateTime('hh:nn:ss', ATime, fs);
end;
function ISOTimeStampToDateTime(const ADateTime: string): TDateTime;
begin
Result := EncodeDateTime(StrToInt(Copy(ADateTime, 1, 4)), StrToInt(Copy(ADateTime, 6, 2)), StrToInt(Copy(ADateTime, 9, 2)),
StrToInt(Copy(ADateTime, 12, 2)), StrToInt(Copy(ADateTime, 15, 2)), StrToInt(Copy(ADateTime, 18, 2)), 0);
end;
function ISODateToDate(const ADate: string): TDate;
begin
Result := EncodeDate(StrToInt(Copy(ADate, 1, 4)), StrToInt(Copy(ADate, 6, 2)), StrToInt(Copy(ADate, 9, 2)));
end;
function ISOTimeToTime(const ATime: string): TTime;
begin
Result := EncodeTime(StrToInt(Copy(ATime, 1, 2)), StrToInt(Copy(ATime, 4, 2)), StrToInt(Copy(ATime, 7, 2)), 0);
end;
{ TMVCSerializerHelpful }
2017-02-07 14:08:36 +01:00
class procedure TMVCSerializerHelpful.DeSerializeBase64StringStream(
AStream: TStream; const ABase64SerializedString: string);
2017-02-07 14:08:36 +01:00
var
SS: TStringStream;
begin
AStream.Size := 0;
SS := TStringStream.Create(ABase64SerializedString, TEncoding.ASCII);
2017-02-07 14:08:36 +01:00
try
SS.Position := 0;
DecodeStream(SS, AStream);
2017-02-07 14:08:36 +01:00
finally
SS.Free;
end;
end;
class procedure TMVCSerializerHelpful.DeSerializeStringStream(AStream: TStream; const ASerializedString: string; const AEncoding: string);
2017-02-07 14:08:36 +01:00
var
Encoding: TEncoding;
2017-02-07 14:08:36 +01:00
SS: TStringStream;
begin
AStream.Position := 0;
Encoding := TEncoding.GetEncoding(AEncoding);
SS := TStringStream.Create(ASerializedString, Encoding);
2017-02-07 14:08:36 +01:00
try
SS.Position := 0;
AStream.CopyFrom(SS, SS.Size);
2017-02-07 14:08:36 +01:00
finally
SS.Free;
end;
end;
class function TMVCSerializerHelpful.GetKeyName(const AField: TRttiField; const AType: TRttiType): string;
2017-02-07 14:08:36 +01:00
var
Attrs: TArray<TCustomAttribute>;
Attr: TCustomAttribute;
2017-02-07 14:08:36 +01:00
begin
Result := AField.Name;
Attrs := AField.GetAttributes;
for Attr in Attrs do
if Attr is MVCNameAsAttribute then
Exit(MVCNameAsAttribute(Attr).Name);
2017-02-07 14:08:36 +01:00
Attrs := AType.GetAttributes;
for Attr in Attrs do
if Attr is MVCNameCaseAttribute then
2017-02-07 14:08:36 +01:00
begin
case MVCNameCaseAttribute(Attr).KeyCase of
ncUpperCase:
2017-02-07 14:08:36 +01:00
begin
Exit(UpperCase(AField.Name));
2017-02-07 14:08:36 +01:00
end;
ncLowerCase:
2017-02-07 14:08:36 +01:00
begin
Exit(LowerCase(AField.Name));
2017-02-07 14:08:36 +01:00
end;
end;
end;
end;
class function TMVCSerializerHelpful.AttributeExists<T>(const AAttributes: TArray<TCustomAttribute>; out AAttribute: T): Boolean;
var
Att: TCustomAttribute;
begin
AAttribute := nil;
for Att in AAttributes do
if Att is T then
begin
AAttribute := T(Att);
Break;
end;
Result := (AAttribute <> nil);
end;
class function TMVCSerializerHelpful.AttributeExists<T>(
const AAttributes: TArray<TCustomAttribute>): Boolean;
var
Att: TCustomAttribute;
begin
Result := False;
for Att in AAttributes do
if Att is T then
Exit(True);
end;
class function TMVCSerializerHelpful.CreateObject(const AObjectType: TRttiType): TObject;
var
MetaClass: TClass;
Method: TRttiMethod;
begin
MetaClass := nil;
Method := nil;
for Method in AObjectType.GetMethods do
if Method.HasExtendedInfo and Method.IsConstructor then
if Length(Method.GetParameters) = 0 then
begin
MetaClass := AObjectType.AsInstance.MetaclassType;
Break;
end;
if Assigned(MetaClass) then
Result := Method.Invoke(MetaClass, []).AsObject
else
raise EMVCException.CreateFmt('Cannot find a propert constructor for %s', [AObjectType.ToString]);
end;
class function TMVCSerializerHelpful.CreateObject(const AQualifiedClassName: string): TObject;
var
Context: TRttiContext;
ObjectType: TRttiType;
begin
Result := nil;
Context := TRttiContext.Create;
try
ObjectType := Context.FindType(AQualifiedClassName);
if Assigned(ObjectType) then
Result := CreateObject(ObjectType)
else
raise Exception.CreateFmt('Cannot find Rtti for %s. Hint: Is the specified classtype linked in the module?', [AQualifiedClassName]);
finally
Context.Free;
end;
end;
class procedure TMVCSerializerHelpful.DecodeStream(AInput, AOutput: TStream);
2017-02-07 14:08:36 +01:00
begin
{$IFDEF SYSTEMNETENCODING}
TNetEncoding.Base64.Decode(AInput, AOutput);
{$ELSE}
Soap.EncdDecd.DecodeStream(AInput, AOutput);
{$ENDIF}
2017-02-07 14:08:36 +01:00
end;
class function TMVCSerializerHelpful.DecodeString(const AInput: string): string;
2017-02-10 14:19:55 +01:00
begin
{$IFDEF SYSTEMNETENCODING}
Result := TNetEncoding.Base64.Decode(AInput);
{$ELSE}
Result := Soap.EncdDecd.DecodeString(AInput);
{$ENDIF}
2017-02-10 14:19:55 +01:00
end;
class procedure TMVCSerializerHelpful.EncodeStream(AInput, AOutput: TStream);
2017-02-07 14:08:36 +01:00
begin
{$IFDEF SYSTEMNETENCODING}
TNetEncoding.Base64.Encode(AInput, AOutput);
{$ELSE}
Soap.EncdDecd.EncodeStream(AInput, AOutput);
{$ENDIF}
2017-02-07 14:08:36 +01:00
end;
class function TMVCSerializerHelpful.EncodeString(const AInput: string): string;
2017-02-10 14:19:55 +01:00
begin
{$IFDEF SYSTEMNETENCODING}
Result := TNetEncoding.Base64.Encode(AInput);
{$ELSE}
Result := Soap.EncdDecd.EncodeString(AInput);
{$ENDIF}
2017-02-10 14:19:55 +01:00
end;
class function TMVCSerializerHelpful.GetKeyName(const AProperty: TRttiProperty; const AType: TRttiType): string;
2017-02-07 14:08:36 +01:00
var
Attrs: TArray<TCustomAttribute>;
Attr: TCustomAttribute;
2017-02-07 14:08:36 +01:00
begin
Result := AProperty.Name;
Attrs := AProperty.GetAttributes;
for Attr in Attrs do
if Attr is MVCNameAsAttribute then
Exit(MVCNameAsAttribute(Attr).Name);
2017-02-07 14:08:36 +01:00
Attrs := AType.GetAttributes;
for Attr in Attrs do
if Attr is MVCNameCaseAttribute then
2017-02-07 14:08:36 +01:00
begin
case MVCNameCaseAttribute(Attr).KeyCase of
ncUpperCase:
2017-02-07 14:08:36 +01:00
begin
Exit(UpperCase(AProperty.Name));
2017-02-07 14:08:36 +01:00
end;
ncLowerCase:
2017-02-07 14:08:36 +01:00
begin
Exit(LowerCase(AProperty.Name));
2017-02-07 14:08:36 +01:00
end;
end;
end;
end;
class function TMVCSerializerHelpful.GetTypeKindAsString(const ATypeKind: TTypeKind): String;
2017-02-09 11:24:18 +01:00
begin
Result := GetEnumName(TypeInfo(TTypeKind), Ord(ATypeKind));
Result := Result.Remove(0, 2).ToLower;
end;
class function TMVCSerializerHelpful.HasAttribute<T>(const AMember: TRttiNamedObject): Boolean;
2017-02-07 14:08:36 +01:00
var
Attrs: TArray<TCustomAttribute>;
Attr: TCustomAttribute;
2017-02-07 14:08:36 +01:00
begin
Result := False;
Attrs := AMember.GetAttributes;
if Length(Attrs) = 0 then
Exit(False);
for Attr in Attrs do
if Attr is T then
2017-02-07 14:08:36 +01:00
Exit(True);
end;
class function TMVCSerializerHelpful.HasAttribute<T>(const AMember: TRttiNamedObject; out AAttribute: T): Boolean;
2017-02-07 14:08:36 +01:00
var
Attrs: TArray<TCustomAttribute>;
Attr: TCustomAttribute;
2017-02-07 14:08:36 +01:00
begin
AAttribute := nil;
Result := False;
Attrs := AMember.GetAttributes;
for Attr in Attrs do
if Attr is T then
2017-02-07 14:08:36 +01:00
begin
AAttribute := T(Attr);
2017-02-07 14:08:36 +01:00
Exit(True);
end;
end;
class function TMVCSerializerHelpful.StringToTypeKind(const AValue: String): TTypeKind;
2017-02-09 11:24:18 +01:00
begin
Result := TTypeKind(GetEnumValue(TypeInfo(TTypeKind), 'tk' + AValue));
end;
{ MVCValueAsTypeAttribute }
2017-02-08 11:42:05 +01:00
constructor MVCValueAsTypeAttribute.Create(AValueTypeInfo: PTypeInfo);
2017-02-08 11:42:05 +01:00
begin
inherited Create;
FValueTypeInfo := AValueTypeInfo;
2017-02-08 11:42:05 +01:00
end;
function MVCValueAsTypeAttribute.ValueTypeInfo: PTypeInfo;
2017-02-08 11:42:05 +01:00
begin
Result := FValueTypeInfo;
2017-02-09 19:33:59 +01:00
end;
{ MVCNameCaseAttribute }
2017-02-09 19:33:59 +01:00
constructor MVCNameCaseAttribute.Create(const AKeyCase: TMVCNameCase);
2017-02-09 19:33:59 +01:00
begin
inherited Create;
FKeyCase := AKeyCase;
2017-02-09 19:33:59 +01:00
end;
function MVCNameCaseAttribute.GetKeyCase: TMVCNameCase;
2017-02-09 19:33:59 +01:00
begin
Result := FKeyCase;
2017-02-08 11:42:05 +01:00
end;
{ MVCNameAsAttribute }
2017-02-09 11:24:18 +01:00
constructor MVCNameAsAttribute.Create(const AName: string);
2017-02-09 11:24:18 +01:00
begin
inherited Create;
FName := AName;
2017-02-09 11:24:18 +01:00
end;
function MVCNameAsAttribute.GetName: string;
2017-02-09 11:24:18 +01:00
begin
Result := FName;
2017-02-09 11:24:18 +01:00
end;
{ MVCListOfAttribute }
2017-02-09 19:33:59 +01:00
constructor MVCListOfAttribute.Create(const AValue: TClass);
2017-02-09 19:33:59 +01:00
begin
inherited Create;
FValue := AValue;
2017-02-09 19:33:59 +01:00
end;
{ MVCDataSetFieldAttribute }
2017-03-02 12:57:40 +01:00
constructor MVCDataSetFieldAttribute.Create(const ADataType: TMVCDataType);
2017-03-02 12:57:40 +01:00
begin
inherited Create;
FDataType := ADataType;
2017-03-02 12:57:40 +01:00
end;
{ MVCSerializeAttribute }
2017-03-02 12:57:40 +01:00
constructor MVCSerializeAttribute.Create(const ASerializationType: TMVCSerializationType);
2017-03-02 12:57:40 +01:00
begin
inherited Create;
FSerializationType := ASerializationType;
2017-03-02 12:57:40 +01:00
end;
2017-02-07 14:08:36 +01:00
end.