2017-03-01 21:40:57 +01:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2018-01-29 17:30:53 +01:00
|
|
|
|
// Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
|
2017-03-01 21:40:57 +01:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
|
|
|
|
// Collaborators with this file: Ezequiel Juliano M<>ller (ezequieljuliano@gmail.com)
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
|
|
|
|
|
unit MVCFramework.Serializer.JsonDataObjects;
|
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Classes,
|
|
|
|
|
System.Rtti,
|
|
|
|
|
System.TypInfo,
|
|
|
|
|
System.Variants,
|
|
|
|
|
System.Generics.Collections,
|
2017-03-29 14:49:35 +02:00
|
|
|
|
Data.SqlTimSt,
|
|
|
|
|
Data.FmtBcd,
|
2017-03-01 21:40:57 +01:00
|
|
|
|
Data.DB,
|
|
|
|
|
MVCFramework.Serializer.Intf,
|
2017-03-28 14:52:13 +02:00
|
|
|
|
MVCFramework.Serializer.Abstract,
|
2017-03-01 21:40:57 +01:00
|
|
|
|
MVCFramework.Serializer.Commons,
|
|
|
|
|
MVCFramework.DuckTyping,
|
2017-05-18 00:02:22 +02:00
|
|
|
|
MVCFramework.TypesAliases,
|
|
|
|
|
JsonDataObjects {JsonDataObjects must be after MVCFramework.TypesAliases};
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
|
|
TJsonValue = class
|
|
|
|
|
private
|
|
|
|
|
FValue: string;
|
|
|
|
|
protected
|
|
|
|
|
{ protected declarations }
|
|
|
|
|
public
|
|
|
|
|
constructor Create; overload;
|
|
|
|
|
constructor Create(const AValue: string); overload;
|
|
|
|
|
property Value: string read FValue write FValue;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 12:57:40 +01:00
|
|
|
|
TMVCJsonDataObjectsSerializer = class(TMVCAbstractSerializer, IMVCSerializer)
|
2017-09-07 00:10:21 +02:00
|
|
|
|
public
|
|
|
|
|
// procedure SerializeDynamicArray(
|
|
|
|
|
// const AValue: TValue;
|
|
|
|
|
// const AArray: TJsonArray;
|
|
|
|
|
// const AType: TMVCSerializationType;
|
|
|
|
|
// const AIgnoredAttributes: TMVCIgnoredList
|
|
|
|
|
// );
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure ObjectToJsonObject(
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList
|
2017-03-01 21:40:57 +01:00
|
|
|
|
);
|
|
|
|
|
procedure AttributeToJsonDataValue(
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AName: string;
|
|
|
|
|
const AValue: TValue;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnored: TMVCIgnoredList;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const ACustomAttributes: TArray<TCustomAttribute>
|
|
|
|
|
);
|
|
|
|
|
procedure JsonObjectToObject(
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList
|
2017-03-01 21:40:57 +01:00
|
|
|
|
);
|
|
|
|
|
procedure JsonDataValueToAttribute(
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AName: string;
|
|
|
|
|
var AValue: TValue;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnored: TMVCIgnoredList;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const ACustomAttributes: TArray<TCustomAttribute>
|
|
|
|
|
);
|
|
|
|
|
procedure JsonArrayToList(
|
|
|
|
|
const AJsonArray: TJsonArray;
|
|
|
|
|
const AList: IMVCList;
|
|
|
|
|
const AClazz: TClass;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList
|
|
|
|
|
);
|
|
|
|
|
procedure DataSetToJsonObject(
|
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const ANameCase: TMVCNameCase;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList
|
2017-03-01 21:40:57 +01:00
|
|
|
|
);
|
2017-09-07 00:10:21 +02:00
|
|
|
|
procedure DataSetToJsonArray(
|
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AJsonArray: TJsonArray;
|
|
|
|
|
const ANameCase: TMVCNameCase;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList
|
|
|
|
|
);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
procedure JsonObjectToDataSet(
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList;
|
|
|
|
|
const ANameCase: TMVCNameCase
|
|
|
|
|
);
|
|
|
|
|
procedure JsonArrayToDataSet(
|
|
|
|
|
const AJsonArray: TJsonArray;
|
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList;
|
|
|
|
|
const ANameCase: TMVCNameCase
|
|
|
|
|
);
|
2017-09-07 00:10:21 +02:00
|
|
|
|
{ IMVCSerializer }
|
2017-03-28 14:52:13 +02:00
|
|
|
|
function SerializeObject(
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AType: TMVCSerializationType = stDefault;
|
2017-04-29 23:56:56 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList = [];
|
|
|
|
|
const ASerializationAction: TMVCSerializationAction = nil
|
2017-03-28 14:52:13 +02:00
|
|
|
|
): string;
|
|
|
|
|
|
|
|
|
|
function SerializeCollection(
|
|
|
|
|
const AList: TObject;
|
|
|
|
|
const AType: TMVCSerializationType = stDefault;
|
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList = []
|
|
|
|
|
): string;
|
|
|
|
|
|
|
|
|
|
function SerializeDataSet(
|
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList = [];
|
|
|
|
|
const ANameCase: TMVCNameCase = ncAsIs
|
|
|
|
|
): string;
|
|
|
|
|
|
|
|
|
|
function SerializeDataSetRecord(
|
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList = [];
|
|
|
|
|
const ANameCase: TMVCNameCase = ncAsIs
|
|
|
|
|
): string;
|
|
|
|
|
|
|
|
|
|
procedure DeserializeObject(
|
|
|
|
|
const ASerializedObject: string;
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AType: TMVCSerializationType = stDefault;
|
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList = []
|
|
|
|
|
);
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
2017-03-28 14:52:13 +02:00
|
|
|
|
procedure DeserializeCollection(
|
|
|
|
|
const ASerializedList: string;
|
|
|
|
|
const AList: TObject;
|
|
|
|
|
const AClazz: TClass;
|
|
|
|
|
const AType: TMVCSerializationType = stDefault;
|
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList = []
|
|
|
|
|
);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-28 14:52:13 +02:00
|
|
|
|
procedure DeserializeDataSet(
|
|
|
|
|
const ASerializedDataSet: string;
|
|
|
|
|
const ADataSet: TDataSet;
|
2017-03-30 15:56:24 +02:00
|
|
|
|
const AIgnoredFields: TMVCIgnoredList = [];
|
|
|
|
|
const ANameCase: TMVCNameCase = ncAsIs
|
2017-03-28 14:52:13 +02:00
|
|
|
|
);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-28 14:52:13 +02:00
|
|
|
|
procedure DeserializeDataSetRecord(
|
|
|
|
|
const ASerializedDataSetRecord: string;
|
|
|
|
|
const ADataSet: TDataSet;
|
2017-03-30 15:56:24 +02:00
|
|
|
|
const AIgnoredFields: TMVCIgnoredList = [];
|
|
|
|
|
const ANameCase: TMVCNameCase = ncAsIs
|
2017-03-28 14:52:13 +02:00
|
|
|
|
);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
public
|
2017-03-02 12:57:40 +01:00
|
|
|
|
procedure AfterConstruction; override;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
MVCFramework.Serializer.JsonDataObjects.CustomTypes;
|
|
|
|
|
|
|
|
|
|
{ TMVCJsonDataObjectsSerializer }
|
|
|
|
|
|
2017-03-02 12:57:40 +01:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.AfterConstruction;
|
|
|
|
|
begin
|
|
|
|
|
inherited AfterConstruction;
|
|
|
|
|
GetTypeSerializers.Add(System.TypeInfo(TStream), TStreamSerializerJsonDataObject.Create);
|
|
|
|
|
GetTypeSerializers.Add(System.TypeInfo(TStringStream), TStreamSerializerJsonDataObject.Create);
|
|
|
|
|
GetTypeSerializers.Add(System.TypeInfo(TMemoryStream), TStreamSerializerJsonDataObject.Create);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.AttributeToJsonDataValue(
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AName: string;
|
|
|
|
|
const AValue: TValue;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnored: TMVCIgnoredList;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const ACustomAttributes: TArray<TCustomAttribute>);
|
|
|
|
|
var
|
|
|
|
|
ChildJsonObject: TJsonObject;
|
|
|
|
|
ChildJsonArray: TJsonArray;
|
|
|
|
|
ChildValue: TValue;
|
|
|
|
|
ChildObject, Obj: TObject;
|
|
|
|
|
ChildList: IMVCList;
|
|
|
|
|
ChildJsonValue: TObject;
|
|
|
|
|
ValueTypeAtt: MVCValueAsTypeAttribute;
|
|
|
|
|
CastValue, CastedValue: TValue;
|
|
|
|
|
begin
|
|
|
|
|
if AValue.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
AJsonObject[AName] := Null;
|
|
|
|
|
Exit;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 12:57:40 +01:00
|
|
|
|
if GetTypeSerializers.ContainsKey(AValue.TypeInfo) then
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
ChildJsonValue := nil;
|
2017-03-02 12:57:40 +01:00
|
|
|
|
GetTypeSerializers.Items[AValue.TypeInfo].Serialize(AValue, ChildJsonValue, ACustomAttributes);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
if Assigned(ChildJsonValue) then
|
|
|
|
|
begin
|
|
|
|
|
if ChildJsonValue is TJsonObject then
|
2017-05-17 22:32:45 +02:00
|
|
|
|
AJsonObject.O[AName] := TJsonObject(ChildJsonValue)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
else if ChildJsonValue is TJsonArray then
|
2017-05-17 22:32:45 +02:00
|
|
|
|
AJsonObject.A[AName] := TJsonArray(ChildJsonValue)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
else if ChildJsonValue is TJsonBaseObject then
|
|
|
|
|
AJsonObject[AName] := ChildJsonValue
|
|
|
|
|
else if ChildJsonValue is TJsonValue then
|
|
|
|
|
begin
|
|
|
|
|
AJsonObject[AName] := TJsonValue(ChildJsonValue).Value;
|
|
|
|
|
ChildJsonValue.Free;
|
|
|
|
|
end
|
|
|
|
|
else
|
2017-05-17 22:32:45 +02:00
|
|
|
|
raise EMVCSerializationException.CreateFmt('Cannot serialize %s the serializer does not have a valid TJsonBaseObject type.', [AName]);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
Exit;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
case AValue.Kind of
|
|
|
|
|
tkInteger:
|
|
|
|
|
AJsonObject.I[AName] := AValue.AsInteger;
|
|
|
|
|
|
|
|
|
|
tkInt64:
|
|
|
|
|
AJsonObject.L[AName] := AValue.AsInt64;
|
|
|
|
|
|
|
|
|
|
tkChar, tkString, tkWChar, tkLString, tkWString, tkUString:
|
|
|
|
|
AJsonObject.S[AName] := AValue.AsString;
|
|
|
|
|
|
|
|
|
|
tkFloat:
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.TypeInfo = System.TypeInfo(TDate)) then
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.AsExtended = 0) then
|
|
|
|
|
AJsonObject[AName] := Null
|
|
|
|
|
else
|
|
|
|
|
AJsonObject.S[AName] := DateToISODate(AValue.AsExtended);
|
|
|
|
|
end
|
|
|
|
|
else if (AValue.TypeInfo = System.TypeInfo(TDateTime)) then
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.AsExtended = 0) then
|
|
|
|
|
AJsonObject[AName] := Null
|
|
|
|
|
else
|
|
|
|
|
AJsonObject.S[AName] := DateTimeToISOTimeStamp(AValue.AsExtended);
|
|
|
|
|
end
|
|
|
|
|
else if (AValue.TypeInfo = System.TypeInfo(TTime)) then
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.AsExtended = 0) then
|
|
|
|
|
AJsonObject[AName] := Null
|
|
|
|
|
else
|
|
|
|
|
AJsonObject.S[AName] := TimeToISOTime(AValue.AsExtended);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
AJsonObject.F[AName] := AValue.AsExtended;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
tkVariant:
|
|
|
|
|
AJsonObject[AName] := AValue.AsVariant;
|
|
|
|
|
|
|
|
|
|
tkEnumeration:
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.TypeInfo = System.TypeInfo(Boolean)) then
|
|
|
|
|
begin
|
|
|
|
|
if AValue.AsBoolean then
|
|
|
|
|
AJsonObject.B[AName] := True
|
|
|
|
|
else
|
|
|
|
|
AJsonObject.B[AName] := False
|
|
|
|
|
end
|
|
|
|
|
else
|
2017-07-12 11:39:16 +02:00
|
|
|
|
AJsonObject.S[AName] := GetEnumName(AValue.TypeInfo, AValue.AsOrdinal);
|
2017-07-16 19:36:44 +02:00
|
|
|
|
// AJSONObject.AddPair(AName, GetEnumName(AValue.TypeInfo, AValue.AsOrdinal));
|
|
|
|
|
// AJsonObject.L[AName] := AValue.AsOrdinal;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
tkClass:
|
|
|
|
|
begin
|
|
|
|
|
ChildObject := AValue.AsObject;
|
|
|
|
|
if Assigned(ChildObject) then
|
|
|
|
|
begin
|
|
|
|
|
ChildList := TDuckTypedList.Wrap(ChildObject);
|
|
|
|
|
if Assigned(ChildList) then
|
|
|
|
|
begin
|
|
|
|
|
ChildJsonArray := AJsonObject.A[AName];
|
|
|
|
|
for Obj in ChildList do
|
|
|
|
|
if Assigned(Obj) then
|
2017-03-28 14:52:13 +02:00
|
|
|
|
ObjectToJsonObject(Obj, ChildJsonArray.AddObject, GetSerializationType(Obj, AType), AIgnored);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
ChildJsonObject := AJsonObject.O[AName];
|
2017-03-28 14:52:13 +02:00
|
|
|
|
ObjectToJsonObject(ChildObject, ChildJsonObject, GetSerializationType(ChildObject, AType), AIgnored);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
if TMVCSerializerHelpful.AttributeExists<MVCSerializeAsStringAttribute>(ACustomAttributes) then
|
|
|
|
|
AJsonObject.S[AName] := EmptyStr
|
|
|
|
|
else
|
|
|
|
|
AJsonObject[AName] := Null;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
tkRecord:
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.TypeInfo = System.TypeInfo(TTimeStamp)) then
|
|
|
|
|
begin
|
|
|
|
|
AJsonObject.F[AName] := TimeStampToMsecs(AValue.AsType<TTimeStamp>);
|
|
|
|
|
end
|
|
|
|
|
else if (AValue.TypeInfo = System.TypeInfo(TValue)) then
|
|
|
|
|
begin
|
|
|
|
|
if TMVCSerializerHelpful.AttributeExists<MVCValueAsTypeAttribute>(ACustomAttributes, ValueTypeAtt) then
|
|
|
|
|
begin
|
|
|
|
|
CastValue := AValue.AsType<TValue>;
|
|
|
|
|
if CastValue.TryCast(ValueTypeAtt.ValueTypeInfo, CastedValue) then
|
|
|
|
|
AttributeToJsonDataValue(AJsonObject, AName, CastedValue, stDefault, [], [])
|
|
|
|
|
else
|
2017-05-17 22:32:45 +02:00
|
|
|
|
raise EMVCSerializationException.CreateFmt('Cannot serialize %s of TypeKind tkRecord (TValue with MVCValueAsTypeAttribute).', [AName]);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
ChildValue := AValue.AsType<TValue>;
|
|
|
|
|
ChildJsonObject := AJsonObject.O[AName];
|
|
|
|
|
ChildJsonObject.S['type'] := TMVCSerializerHelpful.GetTypeKindAsString(ChildValue.TypeInfo.Kind);
|
|
|
|
|
AttributeToJsonDataValue(ChildJsonObject, 'value', ChildValue, stDefault, [], []);
|
|
|
|
|
end;
|
|
|
|
|
end
|
|
|
|
|
else
|
2017-05-17 22:32:45 +02:00
|
|
|
|
raise EMVCSerializationException.CreateFmt('Cannot serialize %s of TypeKind tkRecord.', [AName]);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
tkSet:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
raise EMVCSerializationException.CreateFmt('Cannot serialize %s of TypeKind tkSet.', [AName]);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-07-16 19:36:44 +02:00
|
|
|
|
tkArray, tkDynArray:
|
|
|
|
|
begin
|
|
|
|
|
raise EMVCSerializationException.CreateFmt('Cannot serialize %s of TypeKind tkSet.', [AName]);
|
2017-09-07 00:10:21 +02:00
|
|
|
|
// ChildJsonArray := AJsonObject.A[AName];
|
|
|
|
|
// SerializeDynamicArray(AValue, ChildJsonArray, AType, AIgnored);
|
2017-07-16 19:36:44 +02:00
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
tkUnknown:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
raise EMVCSerializationException.CreateFmt('Cannot serialize %s of TypeKind tkUnknown.', [AName]);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-07 00:10:21 +02:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.DataSetToJsonArray(
|
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AJsonArray: TJsonArray;
|
|
|
|
|
const ANameCase: TMVCNameCase;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList
|
|
|
|
|
);
|
|
|
|
|
var
|
|
|
|
|
LJObj: TJsonObject;
|
|
|
|
|
begin
|
|
|
|
|
while not ADataSet.Eof do
|
|
|
|
|
begin
|
|
|
|
|
LJobj := AJsonArray.AddObject;
|
|
|
|
|
DataSetToJsonObject(ADataSet, lJObj, ANameCase, AIgnoredFields);
|
|
|
|
|
ADataSet.Next;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-29 14:49:35 +02:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.DataSetToJsonObject(
|
|
|
|
|
const ADataSet: TDataSet; const AJsonObject: TJsonObject;
|
|
|
|
|
const ANameCase: TMVCNameCase; const AIgnoredFields: TMVCIgnoredList);
|
|
|
|
|
var
|
|
|
|
|
I: Integer;
|
|
|
|
|
FieldName: string;
|
|
|
|
|
MS: TMemoryStream;
|
|
|
|
|
SS: TStringStream;
|
|
|
|
|
NestedDataSet: TDataSet;
|
|
|
|
|
ChildJsonArray: TJsonArray;
|
|
|
|
|
ChildJsonObject: TJsonObject;
|
|
|
|
|
begin
|
|
|
|
|
for I := 0 to ADataSet.FieldCount - 1 do
|
|
|
|
|
begin
|
|
|
|
|
FieldName := GetNameAs(ADataSet.Owner, ADataSet.Fields[I].Name, ADataSet.Fields[I].FieldName);
|
|
|
|
|
if (not IsIgnoredAttribute(AIgnoredFields, FieldName)) and (not IsIgnoredComponent(ADataSet.Owner, ADataSet.Fields[I].Name)) then
|
|
|
|
|
begin
|
|
|
|
|
case ANameCase of
|
|
|
|
|
ncUpperCase: FieldName := UpperCase(ADataSet.Fields[I].FieldName);
|
|
|
|
|
ncLowerCase: FieldName := LowerCase(ADataSet.Fields[I].FieldName);
|
|
|
|
|
end;
|
|
|
|
|
if ADataSet.Fields[I].IsNull then
|
|
|
|
|
AJsonObject[FieldName] := Null
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
case ADataSet.Fields[I].DataType of
|
|
|
|
|
ftBoolean:
|
|
|
|
|
AJsonObject.B[FieldName] := ADataSet.Fields[I].AsBoolean;
|
|
|
|
|
|
2018-01-29 17:30:53 +01:00
|
|
|
|
ftInteger, ftSmallint, ftShortint, ftByte:
|
2017-03-29 14:49:35 +02:00
|
|
|
|
AJsonObject.I[FieldName] := ADataSet.Fields[I].AsInteger;
|
|
|
|
|
|
2017-04-20 23:48:50 +02:00
|
|
|
|
ftLargeint, ftAutoInc:
|
2017-03-29 14:49:35 +02:00
|
|
|
|
AJsonObject.L[FieldName] := ADataSet.Fields[I].AsLargeInt;
|
2018-01-29 17:30:53 +01:00
|
|
|
|
{$IFDEF TOKYOORBETTER}
|
2017-12-12 20:04:01 +01:00
|
|
|
|
ftGuid:
|
|
|
|
|
AJsonObject.S[FieldName] := GUIDToString(ADataSet.Fields[I].AsGuid);
|
2018-01-29 17:30:53 +01:00
|
|
|
|
{$ENDIF}
|
2017-03-29 14:49:35 +02:00
|
|
|
|
ftSingle, ftFloat:
|
|
|
|
|
AJsonObject.F[FieldName] := ADataSet.Fields[I].AsFloat;
|
|
|
|
|
|
2017-09-28 00:14:34 +02:00
|
|
|
|
ftString, ftMemo:
|
|
|
|
|
AJsonObject.S[FieldName] := ADataSet.Fields[I].AsString;
|
|
|
|
|
|
|
|
|
|
ftWideString, ftWideMemo:
|
2017-03-29 14:49:35 +02:00
|
|
|
|
AJsonObject.S[FieldName] := ADataSet.Fields[I].AsWideString;
|
|
|
|
|
|
|
|
|
|
ftDate:
|
|
|
|
|
AJsonObject.S[FieldName] := DateToISODate(ADataSet.Fields[I].AsDateTime);
|
|
|
|
|
|
|
|
|
|
ftDateTime:
|
|
|
|
|
AJsonObject.S[FieldName] := DateTimeToISOTimeStamp(ADataSet.Fields[I].AsDateTime);
|
|
|
|
|
|
2017-09-07 00:10:21 +02:00
|
|
|
|
ftTime:
|
2017-03-29 14:49:35 +02:00
|
|
|
|
AJsonObject.S[FieldName] := SQLTimeStampToStr('hh:nn:ss', ADataSet.Fields[I].AsSQLTimeStamp);
|
|
|
|
|
|
2017-09-07 00:10:21 +02:00
|
|
|
|
ftTimeStamp:
|
|
|
|
|
AJsonObject.S[FieldName] := DateTimeToISOTimeStamp(SQLTimeStampToDateTime(ADataSet.Fields[I].AsSQLTimeStamp));
|
|
|
|
|
|
2017-03-29 14:49:35 +02:00
|
|
|
|
ftCurrency:
|
2017-03-29 23:13:20 +02:00
|
|
|
|
AJsonObject.F[FieldName] := ADataSet.Fields[I].AsCurrency;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
|
|
|
|
|
ftFMTBcd, ftBCD:
|
|
|
|
|
AJsonObject.F[FieldName] := BcdToDouble(ADataSet.Fields[I].AsBcd);
|
|
|
|
|
|
|
|
|
|
ftGraphic, ftBlob, ftStream:
|
|
|
|
|
begin
|
|
|
|
|
MS := TMemoryStream.Create;
|
|
|
|
|
try
|
|
|
|
|
TBlobField(ADataSet.Fields[I]).SaveToStream(MS);
|
|
|
|
|
MS.Position := 0;
|
|
|
|
|
SS := TStringStream.Create;
|
|
|
|
|
try
|
|
|
|
|
TMVCSerializerHelpful.EncodeStream(MS, SS);
|
|
|
|
|
AJsonObject.S[FieldName] := SS.DataString;
|
|
|
|
|
finally
|
|
|
|
|
SS.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
MS.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
ftDataSet:
|
|
|
|
|
begin
|
|
|
|
|
NestedDataSet := TDataSetField(ADataSet.Fields[I]).NestedDataSet;
|
|
|
|
|
case GetDataType(ADataSet.Owner, ADataSet.Fields[I].Name, dtArray) of
|
|
|
|
|
dtArray:
|
|
|
|
|
begin
|
|
|
|
|
ChildJsonArray := AJsonObject.A[FieldName];
|
|
|
|
|
NestedDataSet.First;
|
|
|
|
|
while not NestedDataSet.Eof do
|
|
|
|
|
begin
|
|
|
|
|
DataSetToJsonObject(NestedDataSet, ChildJsonArray.AddObject, GetNameCase(NestedDataSet, ANameCase), AIgnoredFields);
|
|
|
|
|
NestedDataSet.Next;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
dtObject:
|
|
|
|
|
begin
|
|
|
|
|
ChildJsonObject := AJsonObject.O[FieldName];
|
|
|
|
|
DataSetToJsonObject(NestedDataSet, ChildJsonObject, GetNameCase(NestedDataSet, ANameCase), AIgnoredFields);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
else
|
|
|
|
|
raise EMVCSerializationException.CreateFmt('Cannot find type for field "%s"', [FieldName]);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.DeserializeCollection(
|
|
|
|
|
const ASerializedList: string;
|
|
|
|
|
const AList: TObject;
|
|
|
|
|
const AClazz: TClass;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-28 14:52:13 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
JsonArray: TJsonArray;
|
|
|
|
|
ObjList: IMVCList;
|
|
|
|
|
begin
|
|
|
|
|
if (ASerializedList = EmptyStr) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
if not Assigned(AList) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
ObjList := TDuckTypedList.Wrap(AList);
|
|
|
|
|
if Assigned(ObjList) then
|
|
|
|
|
begin
|
|
|
|
|
JsonArray := TJsonArray.Parse(ASerializedList) as TJsonArray;
|
|
|
|
|
try
|
|
|
|
|
JsonArrayToList(JsonArray, ObjList, AClazz, AType, AIgnoredAttributes);
|
|
|
|
|
finally
|
|
|
|
|
JsonArray.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-28 14:52:13 +02:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.DeserializeDataSet(
|
|
|
|
|
const ASerializedDataSet: string;
|
|
|
|
|
const ADataSet: TDataSet;
|
2017-03-30 15:56:24 +02:00
|
|
|
|
const AIgnoredFields: TMVCIgnoredList;
|
|
|
|
|
const ANameCase: TMVCNameCase);
|
|
|
|
|
var
|
|
|
|
|
JsonArray: TJsonArray;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2017-03-30 15:56:24 +02:00
|
|
|
|
if (ASerializedDataSet = EmptyStr) or (not Assigned(ADataSet)) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
JsonArray := TJsonArray.Parse(ASerializedDataSet) as TJsonArray;
|
|
|
|
|
try
|
|
|
|
|
JsonArrayToDataSet(JsonArray, ADataSet, AIgnoredFields, ANameCase);
|
|
|
|
|
finally
|
|
|
|
|
JsonArray.Free;
|
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-28 14:52:13 +02:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.DeserializeDataSetRecord(
|
|
|
|
|
const ASerializedDataSetRecord: string;
|
|
|
|
|
const ADataSet: TDataSet;
|
2017-03-30 15:56:24 +02:00
|
|
|
|
const AIgnoredFields: TMVCIgnoredList;
|
|
|
|
|
const ANameCase: TMVCNameCase);
|
|
|
|
|
var
|
|
|
|
|
JsonObject: TJsonObject;
|
|
|
|
|
begin
|
|
|
|
|
if (ASerializedDataSetRecord = EmptyStr) or (not Assigned(ADataSet)) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
JsonObject := TJsonObject.Parse(ASerializedDataSetRecord) as TJsonObject;
|
|
|
|
|
try
|
|
|
|
|
ADataSet.Edit;
|
|
|
|
|
JsonObjectToDataSet(JsonObject, ADataSet, AIgnoredFields, ANameCase);
|
|
|
|
|
ADataSet.Post;
|
|
|
|
|
finally
|
|
|
|
|
JsonObject.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.JsonArrayToDataSet(
|
|
|
|
|
const AJsonArray: TJsonArray; const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
|
|
|
|
|
var
|
|
|
|
|
I: Integer;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
begin
|
2017-03-30 15:56:24 +02:00
|
|
|
|
for I := 0 to Pred(AJsonArray.Count) do
|
|
|
|
|
begin
|
|
|
|
|
ADataSet.Append;
|
|
|
|
|
JsonObjectToDataSet(AJsonArray.Items[I].ObjectValue, ADataSet, AIgnoredFields, ANameCase);
|
|
|
|
|
ADataSet.Post;
|
|
|
|
|
end;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.JsonArrayToList(
|
|
|
|
|
const AJsonArray: TJsonArray; const AList: IMVCList;
|
|
|
|
|
const AClazz: TClass; const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
I: Integer;
|
|
|
|
|
Obj: TObject;
|
|
|
|
|
begin
|
|
|
|
|
for I := 0 to Pred(AJsonArray.Count) do
|
|
|
|
|
begin
|
|
|
|
|
Obj := TMVCSerializerHelpful.CreateObject(AClazz.QualifiedClassName);
|
2017-03-28 14:52:13 +02:00
|
|
|
|
JsonObjectToObject(AJsonArray.Items[I].ObjectValue, Obj, GetSerializationType(Obj, AType), AIgnoredAttributes);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
AList.Add(Obj);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.JsonDataValueToAttribute(
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AName: string;
|
|
|
|
|
var AValue: TValue;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnored: TMVCIgnoredList;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const ACustomAttributes: TArray<TCustomAttribute>);
|
|
|
|
|
var
|
|
|
|
|
ChildObject: TObject;
|
|
|
|
|
ChildList: IMVCList;
|
|
|
|
|
ChildListOfAtt: MVCListOfAttribute;
|
|
|
|
|
ChildJsonValue: TJsonValue;
|
|
|
|
|
begin
|
2017-03-02 12:57:40 +01:00
|
|
|
|
if GetTypeSerializers.ContainsKey(AValue.TypeInfo) then
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
case AJsonObject[AName].Typ of
|
|
|
|
|
jdtNone:
|
|
|
|
|
Exit;
|
|
|
|
|
jdtObject:
|
2017-03-02 12:57:40 +01:00
|
|
|
|
GetTypeSerializers.Items[AValue.TypeInfo].Deserialize(AJsonObject[AName].ObjectValue, AValue, ACustomAttributes);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
jdtArray:
|
2017-03-02 12:57:40 +01:00
|
|
|
|
GetTypeSerializers.Items[AValue.TypeInfo].Deserialize(AJsonObject[AName].ArrayValue, AValue, ACustomAttributes);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
ChildJsonValue := TJsonValue.Create;
|
|
|
|
|
try
|
|
|
|
|
ChildJsonValue.Value := AJsonObject[AName].Value;
|
2017-03-02 12:57:40 +01:00
|
|
|
|
GetTypeSerializers.Items[AValue.TypeInfo].Deserialize(ChildJsonValue, AValue, ACustomAttributes);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
ChildJsonValue.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
Exit;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
case AJsonObject[AName].Typ of
|
|
|
|
|
jdtNone:
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
jdtString:
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.TypeInfo = System.TypeInfo(TDate)) then
|
|
|
|
|
AValue := TValue.From<TDate>(ISODateToDate(AJsonObject[AName].Value))
|
|
|
|
|
|
|
|
|
|
else if (AValue.TypeInfo = System.TypeInfo(TDateTime)) then
|
|
|
|
|
AValue := TValue.From<TDateTime>(ISOTimeStampToDateTime(AJsonObject[AName].Value))
|
|
|
|
|
|
|
|
|
|
else if (AValue.TypeInfo = System.TypeInfo(TTime)) then
|
|
|
|
|
AValue := TValue.From<TTime>(ISOTimeToTime(AJsonObject[AName].Value))
|
|
|
|
|
|
2017-07-12 11:39:16 +02:00
|
|
|
|
else if (AValue.Kind = tkEnumeration) then
|
|
|
|
|
TValue.Make(GetEnumValue(AValue.TypeInfo, AJsonObject[AName].Value), AValue.TypeInfo, AValue)
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
else
|
|
|
|
|
AValue := TValue.From<string>(AJsonObject[AName].Value);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
jdtInt:
|
|
|
|
|
begin
|
2017-07-16 19:36:44 +02:00
|
|
|
|
// if (AValue.Kind = tkEnumeration) then
|
|
|
|
|
// TValue.Make(AJsonObject[AName].IntValue, AValue.TypeInfo, AValue)
|
|
|
|
|
// else
|
|
|
|
|
AValue := TValue.From<Integer>(AJsonObject[AName].IntValue);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
jdtLong, jdtULong:
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.TypeInfo = System.TypeInfo(TTimeStamp)) then
|
|
|
|
|
AValue := TValue.From<TTimeStamp>(MSecsToTimeStamp(AJsonObject[AName].LongValue))
|
|
|
|
|
else
|
|
|
|
|
AValue := TValue.From<Int64>(AJsonObject[AName].LongValue);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
jdtFloat:
|
|
|
|
|
AValue := TValue.From<Double>(AJsonObject[AName].FloatValue);
|
|
|
|
|
|
|
|
|
|
jdtDateTime:
|
|
|
|
|
AValue := TValue.From<TDateTime>(AJsonObject[AName].DateTimeValue);
|
|
|
|
|
|
|
|
|
|
jdtBool:
|
|
|
|
|
AValue := TValue.From<Boolean>(AJsonObject[AName].BoolValue);
|
|
|
|
|
|
|
|
|
|
jdtObject:
|
|
|
|
|
begin
|
|
|
|
|
if (AValue.TypeInfo = System.TypeInfo(TValue)) then
|
|
|
|
|
AValue := TValue.FromVariant(AJsonObject[AName].O['value'].VariantValue)
|
|
|
|
|
else
|
|
|
|
|
begin
|
2017-05-17 22:32:45 +02:00
|
|
|
|
// dt: if a key is null, jsondataobjects assign it the type jdtObject
|
2017-05-18 00:02:22 +02:00
|
|
|
|
if AJsonObject[AName].ObjectValue <> nil then
|
2017-05-17 22:32:45 +02:00
|
|
|
|
begin
|
|
|
|
|
ChildObject := AValue.AsObject;
|
2017-05-18 00:02:22 +02:00
|
|
|
|
JsonObjectToObject(AJsonObject.O[AName], ChildObject, GetSerializationType(ChildObject, AType), AIgnored);
|
2017-05-17 22:32:45 +02:00
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
jdtArray:
|
|
|
|
|
begin
|
|
|
|
|
ChildObject := AValue.AsObject;
|
|
|
|
|
if Assigned(ChildObject) then
|
|
|
|
|
begin
|
|
|
|
|
ChildList := TDuckTypedList.Wrap(ChildObject);
|
|
|
|
|
if TMVCSerializerHelpful.AttributeExists<MVCListOfAttribute>(ACustomAttributes, ChildListOfAtt) then
|
|
|
|
|
JsonArrayToList(AJsonObject.A[AName], ChildList, ChildListOfAtt.Value, AType, AIgnored)
|
|
|
|
|
else
|
|
|
|
|
raise EMVCDeserializationException.CreateFmt('You can not deserialize a list %s without the attribute MVCListClassTypeAttribute.', [AName]);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-30 15:56:24 +02:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.JsonObjectToDataSet(
|
|
|
|
|
const AJsonObject: TJsonObject; const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
|
|
|
|
|
var
|
|
|
|
|
Field: TField;
|
|
|
|
|
Name: string;
|
|
|
|
|
SS: TStringStream;
|
|
|
|
|
SM: TMemoryStream;
|
|
|
|
|
NestedDataSet: TDataSet;
|
|
|
|
|
begin
|
|
|
|
|
if (ADataSet.State in [dsInsert, dsEdit]) then
|
|
|
|
|
begin
|
|
|
|
|
for Field in ADataSet.Fields do
|
|
|
|
|
begin
|
2017-05-17 22:32:45 +02:00
|
|
|
|
name := GetNameAs(ADataSet.Owner, Field.Name, Field.FieldName);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
2017-05-17 22:32:45 +02:00
|
|
|
|
if (IsIgnoredAttribute(AIgnoredFields, name)) or (IsIgnoredComponent(ADataSet.Owner, Field.Name)) then
|
2017-03-30 15:56:24 +02:00
|
|
|
|
Continue;
|
|
|
|
|
|
|
|
|
|
case GetNameCase(ADataSet, ANameCase) of
|
2017-05-17 22:32:45 +02:00
|
|
|
|
ncLowerCase: name := LowerCase(Field.FieldName);
|
|
|
|
|
ncUpperCase: name := UpperCase(Field.FieldName);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-05-17 22:32:45 +02:00
|
|
|
|
if not AJsonObject.Contains(name) then
|
2017-03-30 15:56:24 +02:00
|
|
|
|
Continue;
|
|
|
|
|
|
2017-05-17 22:32:45 +02:00
|
|
|
|
if (AJsonObject[name].Typ = jdtObject) and (AJsonObject.Values[name].ObjectValue = nil) then // Nullable Type
|
2017-03-30 15:56:24 +02:00
|
|
|
|
begin
|
|
|
|
|
Field.Clear;
|
|
|
|
|
Continue;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
case field.DataType of
|
|
|
|
|
TFieldType.ftBoolean:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsBoolean := AJsonObject.B[name];
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
2018-01-29 17:30:53 +01:00
|
|
|
|
TFieldType.ftInteger, TFieldType.ftSmallint, TFieldType.ftShortint, TFieldType.ftByte:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsInteger := AJsonObject.I[name];
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
TFieldType.ftLargeint:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsLargeInt := AJsonObject.L[name];
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
TFieldType.ftCurrency:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsCurrency := AJsonObject.F[name];
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
TFieldType.ftSingle:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsSingle := AJsonObject.F[name];
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
TFieldType.ftFloat, TFieldType.ftFMTBcd, TFieldType.ftBCD:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsFloat := AJsonObject.F[name];
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
ftString, ftWideString, ftMemo, ftWideMemo:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsWideString := AJsonObject.S[name];
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
TFieldType.ftDate:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsDateTime := ISODateToDate(AJsonObject.S[name]);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
TFieldType.ftDateTime:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsDateTime := ISOTimeStampToDateTime(AJsonObject.S[name]);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
TFieldType.ftTimeStamp, TFieldType.ftTime:
|
2017-05-17 22:32:45 +02:00
|
|
|
|
Field.AsDateTime := ISOTimeToTime(AJsonObject.S[name]);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
2018-01-29 17:30:53 +01:00
|
|
|
|
{$IFDEF TOKYOORBETTER}
|
2017-12-12 20:04:01 +01:00
|
|
|
|
TFieldType.ftGuid:
|
|
|
|
|
Field.AsGuid := StringToGUID(AJsonObject.S[name]);
|
2018-01-29 17:30:53 +01:00
|
|
|
|
{$ENDIF}
|
2017-12-12 20:04:01 +01:00
|
|
|
|
|
2017-03-30 15:56:24 +02:00
|
|
|
|
TFieldType.ftGraphic, TFieldType.ftBlob, TFieldType.ftStream:
|
|
|
|
|
begin
|
2017-05-17 22:32:45 +02:00
|
|
|
|
SS := TStringStream.Create(AJsonObject.S[name]);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
try
|
|
|
|
|
SS.Position := 0;
|
|
|
|
|
SM := TMemoryStream.Create;
|
|
|
|
|
try
|
|
|
|
|
TMVCSerializerHelpful.DecodeStream(SS, SM);
|
|
|
|
|
TBlobField(Field).LoadFromStream(SM);
|
|
|
|
|
finally
|
|
|
|
|
SM.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
SS.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
TFieldType.ftDataSet:
|
|
|
|
|
begin
|
|
|
|
|
NestedDataSet := TDataSetField(Field).NestedDataSet;
|
|
|
|
|
|
|
|
|
|
NestedDataSet.First;
|
|
|
|
|
while not NestedDataSet.Eof do
|
|
|
|
|
NestedDataSet.Delete;
|
|
|
|
|
|
|
|
|
|
case GetDataType(ADataSet.Owner, Field.Name, dtArray) of
|
|
|
|
|
dtArray:
|
|
|
|
|
begin
|
2017-05-17 22:32:45 +02:00
|
|
|
|
JsonArrayToDataSet(AJsonObject.A[name], NestedDataSet, AIgnoredFields, ANameCase);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
end;
|
|
|
|
|
dtObject:
|
|
|
|
|
begin
|
|
|
|
|
NestedDataSet.Edit;
|
2017-05-17 22:32:45 +02:00
|
|
|
|
JsonObjectToDataSet(AJsonObject.O[name], NestedDataSet, AIgnoredFields, ANameCase);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
NestedDataSet.Post;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
else
|
|
|
|
|
raise EMVCDeserializationException.CreateFmt('Cannot find type for field "%s"', [Field.FieldName]);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.JsonObjectToObject(
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
ObjType: TRttiType;
|
|
|
|
|
Prop: TRttiProperty;
|
|
|
|
|
Fld: TRttiField;
|
|
|
|
|
AttributeValue: TValue;
|
|
|
|
|
begin
|
2017-03-02 12:57:40 +01:00
|
|
|
|
ObjType := GetRttiContext.GetType(AObject.ClassType);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
case AType of
|
|
|
|
|
stDefault, stProperties:
|
|
|
|
|
begin
|
|
|
|
|
for Prop in ObjType.GetProperties do
|
2017-05-25 12:30:08 +02:00
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
{$IFDEF AUTOREFCOUNT}
|
|
|
|
|
|
|
|
|
|
if TMVCSerializerHelpful.IsAPropertyToSkip(Prop.Name) then
|
|
|
|
|
Continue;
|
|
|
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
if (Prop.IsWritable or Prop.GetValue(AObject).IsObject) and (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Prop)) and (not IsIgnoredAttribute(AIgnoredAttributes, Prop.Name)) then
|
|
|
|
|
begin
|
|
|
|
|
AttributeValue := Prop.GetValue(AObject);
|
|
|
|
|
JsonDataValueToAttribute(AJsonObject, TMVCSerializerHelpful.GetKeyName(Prop, ObjType), AttributeValue, AType, AIgnoredAttributes, Prop.GetAttributes);
|
|
|
|
|
if (not AttributeValue.IsEmpty) and Prop.IsWritable then
|
|
|
|
|
Prop.SetValue(AObject, AttributeValue);
|
|
|
|
|
end;
|
2017-05-25 12:30:08 +02:00
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
stFields:
|
|
|
|
|
begin
|
|
|
|
|
for Fld in ObjType.GetFields do
|
|
|
|
|
if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Fld)) and (not IsIgnoredAttribute(AIgnoredAttributes, Fld.Name)) then
|
|
|
|
|
begin
|
|
|
|
|
AttributeValue := Fld.GetValue(AObject);
|
|
|
|
|
JsonDataValueToAttribute(AJsonObject, TMVCSerializerHelpful.GetKeyName(Fld, ObjType), AttributeValue, AType, AIgnoredAttributes, Fld.GetAttributes);
|
|
|
|
|
if not AttributeValue.IsEmpty then
|
|
|
|
|
Fld.SetValue(AObject, AttributeValue);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.ObjectToJsonObject(
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AJsonObject: TJsonObject;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
ObjType: TRttiType;
|
|
|
|
|
Prop: TRttiProperty;
|
|
|
|
|
Fld: TRttiField;
|
|
|
|
|
begin
|
2017-03-02 12:57:40 +01:00
|
|
|
|
ObjType := GetRttiContext.GetType(AObject.ClassType);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
case AType of
|
|
|
|
|
stDefault, stProperties:
|
|
|
|
|
begin
|
|
|
|
|
for Prop in ObjType.GetProperties do
|
2017-05-25 12:30:08 +02:00
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
{$IFDEF AUTOREFCOUNT}
|
|
|
|
|
|
|
|
|
|
if TMVCSerializerHelpful.IsAPropertyToSkip(Prop.Name) then
|
|
|
|
|
Continue;
|
|
|
|
|
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Prop)) and (not IsIgnoredAttribute(AIgnoredAttributes, Prop.Name)) then
|
|
|
|
|
AttributeToJsonDataValue(AJsonObject, TMVCSerializerHelpful.GetKeyName(Prop, ObjType), Prop.GetValue(AObject), AType, AIgnoredAttributes, Prop.GetAttributes);
|
2017-05-25 12:30:08 +02:00
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
stFields:
|
|
|
|
|
begin
|
|
|
|
|
for Fld in ObjType.GetFields do
|
|
|
|
|
if (not TMVCSerializerHelpful.HasAttribute<MVCDoNotSerializeAttribute>(Fld)) and (not IsIgnoredAttribute(AIgnoredAttributes, Fld.Name)) then
|
|
|
|
|
AttributeToJsonDataValue(AJsonObject, TMVCSerializerHelpful.GetKeyName(Fld, ObjType), Fld.GetValue(AObject), AType, AIgnoredAttributes, Fld.GetAttributes);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCJsonDataObjectsSerializer.SerializeCollection(
|
|
|
|
|
const AList: TObject; const AType: TMVCSerializationType;
|
2017-03-28 14:52:13 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList): string;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
JsonArray: TJsonArray;
|
|
|
|
|
ObjList: IMVCList;
|
|
|
|
|
Obj: TObject;
|
|
|
|
|
begin
|
|
|
|
|
Result := EmptyStr;
|
|
|
|
|
|
|
|
|
|
if not Assigned(AList) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
if AList is TJsonBaseObject then
|
2017-05-25 10:31:24 +02:00
|
|
|
|
Exit(TJsonBaseObject(AList).ToJSON(True));
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
ObjList := TDuckTypedList.Wrap(AList);
|
|
|
|
|
if Assigned(ObjList) then
|
|
|
|
|
begin
|
|
|
|
|
JsonArray := TJsonArray.Create;
|
|
|
|
|
try
|
|
|
|
|
for Obj in ObjList do
|
|
|
|
|
if Assigned(Obj) then
|
2017-03-28 14:52:13 +02:00
|
|
|
|
ObjectToJsonObject(Obj, JsonArray.AddObject, GetSerializationType(Obj, AType), AIgnoredAttributes);
|
2017-05-25 10:31:24 +02:00
|
|
|
|
Result := JsonArray.ToJSON(True);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
JsonArray.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCJsonDataObjectsSerializer.SerializeDataSet(
|
2017-03-28 14:52:13 +02:00
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList;
|
|
|
|
|
const ANameCase: TMVCNameCase): string;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
var
|
|
|
|
|
JsonArray: TJsonArray;
|
2017-03-30 15:56:24 +02:00
|
|
|
|
BookMark: TBookmark;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2017-03-29 14:49:35 +02:00
|
|
|
|
Result := EmptyStr;
|
|
|
|
|
|
|
|
|
|
if (not Assigned(ADataSet)) or (ADataSet.IsEmpty) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
JsonArray := TJsonArray.Create;
|
|
|
|
|
try
|
2017-03-30 15:56:24 +02:00
|
|
|
|
BookMark := ADataSet.Bookmark;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
ADataSet.First;
|
|
|
|
|
while not ADataSet.Eof do
|
|
|
|
|
begin
|
|
|
|
|
DataSetToJsonObject(ADataSet, JsonArray.AddObject, GetNameCase(ADataSet, ANameCase), AIgnoredFields);
|
|
|
|
|
ADataSet.Next;
|
|
|
|
|
end;
|
2017-05-25 10:31:24 +02:00
|
|
|
|
Result := JsonArray.ToJSON(True);
|
2017-03-29 14:49:35 +02:00
|
|
|
|
finally
|
|
|
|
|
JsonArray.Free;
|
2017-03-30 15:56:24 +02:00
|
|
|
|
if ADataSet.BookmarkValid(BookMark) then
|
|
|
|
|
ADataSet.GotoBookmark(BookMark);
|
|
|
|
|
ADataSet.FreeBookmark(BookMark);
|
2017-03-29 14:49:35 +02:00
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCJsonDataObjectsSerializer.SerializeDataSetRecord(
|
2017-03-28 14:52:13 +02:00
|
|
|
|
const ADataSet: TDataSet;
|
|
|
|
|
const AIgnoredFields: TMVCIgnoredList;
|
|
|
|
|
const ANameCase: TMVCNameCase): string;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
var
|
|
|
|
|
JsonObject: TJsonObject;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
begin
|
2017-03-29 14:49:35 +02:00
|
|
|
|
Result := EmptyStr;
|
|
|
|
|
|
|
|
|
|
if (not Assigned(ADataSet)) or (ADataSet.IsEmpty) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
JsonObject := TJsonObject.Create;
|
|
|
|
|
try
|
|
|
|
|
DataSetToJsonObject(ADataSet, JsonObject, GetNameCase(ADataSet, ANameCase), AIgnoredFields);
|
2017-05-25 10:31:24 +02:00
|
|
|
|
Result := JsonObject.ToJSON(True);
|
2017-03-29 14:49:35 +02:00
|
|
|
|
finally
|
|
|
|
|
JsonObject.Free;
|
|
|
|
|
end;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-07 00:10:21 +02:00
|
|
|
|
// procedure TMVCJsonDataObjectsSerializer.SerializeDynamicArray(
|
|
|
|
|
// const AValue: TValue;
|
|
|
|
|
// const AArray: TJsonArray;
|
|
|
|
|
// const AType: TMVCSerializationType;
|
|
|
|
|
// const AIgnoredAttributes: TMVCIgnoredList
|
|
|
|
|
// );
|
|
|
|
|
// var
|
|
|
|
|
// I: Integer;
|
|
|
|
|
// Obj: TObject;
|
|
|
|
|
// begin
|
|
|
|
|
// if AValue.GetArrayLength > 0 then
|
|
|
|
|
// if not AValue.GetArrayElement(I).IsObject then
|
|
|
|
|
// raise EMVCSerializationException.Create('Cannot serialize non-object in dynamic (or static) arrays');
|
|
|
|
|
// for I := 0 to AValue.GetArrayLength - 1 do
|
|
|
|
|
// begin
|
|
|
|
|
// Obj := AValue.GetArrayElement(I).AsObject;
|
|
|
|
|
// if Assigned(Obj) then
|
|
|
|
|
// ObjectToJsonObject(Obj, AArray.AddObject, GetSerializationType(Obj, AType), AIgnoredAttributes);
|
|
|
|
|
// end;
|
|
|
|
|
// end;
|
2017-07-16 19:36:44 +02:00
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
function TMVCJsonDataObjectsSerializer.SerializeObject(
|
2017-04-29 23:56:56 +02:00
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AType: TMVCSerializationType;
|
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList;
|
|
|
|
|
const ASerializationAction: TMVCSerializationAction
|
|
|
|
|
): string;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
JsonObject: TJsonObject;
|
|
|
|
|
ChildJsonValue: TJsonBaseObject;
|
|
|
|
|
ObjType: TRttiType;
|
|
|
|
|
begin
|
|
|
|
|
Result := EmptyStr;
|
|
|
|
|
|
|
|
|
|
if not Assigned(AObject) then
|
2017-05-18 00:02:22 +02:00
|
|
|
|
Exit('null');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
if AObject is TJsonBaseObject then
|
2017-05-18 00:02:22 +02:00
|
|
|
|
Exit(TJsonBaseObject(AObject).ToJSON(True));
|
|
|
|
|
|
|
|
|
|
if AObject is MVCFramework.TypesAliases.TJSONValue then
|
|
|
|
|
Exit(MVCFramework.TypesAliases.TJSONValue(AObject).ToJSON);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2017-03-02 12:57:40 +01:00
|
|
|
|
ObjType := GetRttiContext.GetType(AObject.ClassType);
|
|
|
|
|
if GetTypeSerializers.ContainsKey(ObjType.Handle) then
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
ChildJsonValue := nil;
|
2017-03-02 12:57:40 +01:00
|
|
|
|
GetTypeSerializers.Items[ObjType.Handle].Serialize(AObject, TObject(ChildJsonValue), []);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
if Assigned(ChildJsonValue) then
|
|
|
|
|
begin
|
|
|
|
|
try
|
|
|
|
|
if ChildJsonValue is TJsonBaseObject then
|
2017-05-25 10:31:24 +02:00
|
|
|
|
Result := ChildJsonValue.ToJSON(True)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
else
|
2017-05-17 22:32:45 +02:00
|
|
|
|
raise EMVCSerializationException.Create('Cannot serialize, the serializer does not have a valid TJsonBaseObject type.');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
ChildJsonValue.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
Exit;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
JsonObject := TJsonObject.Create;
|
|
|
|
|
try
|
2017-03-28 14:52:13 +02:00
|
|
|
|
ObjectToJsonObject(AObject, JsonObject, GetSerializationType(AObject, AType), AIgnoredAttributes);
|
2017-05-25 10:31:24 +02:00
|
|
|
|
Result := JsonObject.ToJSON(True);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
JsonObject.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCJsonDataObjectsSerializer.DeserializeObject(
|
|
|
|
|
const ASerializedObject: string;
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AType: TMVCSerializationType;
|
2017-03-28 14:52:13 +02:00
|
|
|
|
const AIgnoredAttributes: TMVCIgnoredList);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
JsonObject: TJsonObject;
|
|
|
|
|
ObjType: TRttiType;
|
|
|
|
|
ObjValue: TValue;
|
|
|
|
|
begin
|
|
|
|
|
if (ASerializedObject = EmptyStr) then
|
2017-09-23 15:03:07 +02:00
|
|
|
|
raise Exception.Create('Invalid body');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
if not Assigned(AObject) then
|
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
|
|
JsonObject := TJsonObject.Parse(ASerializedObject) as TJsonObject;
|
|
|
|
|
try
|
2017-03-02 12:57:40 +01:00
|
|
|
|
ObjType := GetRttiContext.GetType(AObject.ClassType);
|
|
|
|
|
if GetTypeSerializers.ContainsKey(ObjType.Handle) then
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
ObjValue := TValue.From<TObject>(AObject);
|
2017-03-02 12:57:40 +01:00
|
|
|
|
GetTypeSerializers.Items[ObjType.Handle].Deserialize(JsonObject, ObjValue, []);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
Exit;
|
|
|
|
|
end;
|
2017-03-28 14:52:13 +02:00
|
|
|
|
JsonObjectToObject(JsonObject, AObject, GetSerializationType(AObject, AType), AIgnoredAttributes);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
JsonObject.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{ TJsonValue }
|
|
|
|
|
|
|
|
|
|
constructor TJsonValue.Create;
|
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
|
|
|
|
FValue := EmptyStr;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
constructor TJsonValue.Create(const AValue: string);
|
|
|
|
|
begin
|
|
|
|
|
Create;
|
|
|
|
|
FValue := AValue;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end.
|