2017-03-01 21:40:57 +01:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
|
// Copyright (c) 2010-2020 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.
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
|
2019-05-19 11:06:03 +02:00
|
|
|
|
unit Serializers.JsonDataObjectsTestU;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2017-08-20 02:36:22 +02:00
|
|
|
|
DUnitX.TestFramework,
|
2017-03-01 21:40:57 +01:00
|
|
|
|
System.Rtti,
|
|
|
|
|
System.Classes,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.DateUtils,
|
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
MVCFramework.Serializer.Commons,
|
|
|
|
|
MVCFramework.Serializer.Intf,
|
|
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
2017-03-02 13:27:19 +01:00
|
|
|
|
MVCFramework.Tests.Serializer.Intf,
|
2017-03-01 21:40:57 +01:00
|
|
|
|
MVCFramework.Tests.Serializer.Entities,
|
2017-03-29 14:49:35 +02:00
|
|
|
|
MVCFramework.Tests.Serializer.EntitiesModule,
|
2019-03-13 14:16:33 +01:00
|
|
|
|
JsonDataObjects,
|
|
|
|
|
MVCFramework.DataSet.Utils;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[TestFixture]
|
|
|
|
|
TMVCTestSerializerJsonDataObjects = class(TObject)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
private
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer: IMVCSerializer;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
public
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Setup]
|
2018-10-14 18:23:20 +02:00
|
|
|
|
procedure Setup;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[TearDown]
|
|
|
|
|
procedure TearDown;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
|
2017-03-29 14:49:35 +02:00
|
|
|
|
{ serialize declarations }
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2020-11-04 19:06:54 +01:00
|
|
|
|
// [Category('this')]
|
2019-11-05 14:40:39 +01:00
|
|
|
|
procedure TestSerializeAllTypes;
|
|
|
|
|
[Test]
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TestSerializeDateTimeProperty;
|
|
|
|
|
[Test]
|
2020-02-03 12:11:42 +01:00
|
|
|
|
procedure TestSerializeAllNullableTypes;
|
|
|
|
|
[Test]
|
2019-11-05 14:40:39 +01:00
|
|
|
|
procedure TestSerializeAllTypesInList;
|
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestSerializeEntity;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-05-18 00:02:22 +02:00
|
|
|
|
procedure TestSerializeNil;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestSerializeEntityUpperCaseNames;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2020-11-27 15:13:17 +01:00
|
|
|
|
// [Category('this')]
|
2019-05-19 14:35:34 +02:00
|
|
|
|
procedure TestSerializeEntityWithArray;
|
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestSerializeEntityLowerCaseNames;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestSerializeEntityNameAs;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestSerializeEntityCustomSerializer;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestSerializeEntityCustomMemberSerializer;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-28 14:52:13 +02:00
|
|
|
|
procedure TestSerializeEntitySerializationType;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestSerializeCollection;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-29 14:49:35 +02:00
|
|
|
|
procedure TestSerializeDataSet;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
[Test]
|
2020-11-27 15:13:17 +01:00
|
|
|
|
[Category('datasets')]
|
2020-04-19 12:23:55 +02:00
|
|
|
|
procedure TestDataSetHelpers;
|
2017-03-29 14:49:35 +02:00
|
|
|
|
{ deserialize declarations }
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestDeserializeEntity;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestDeserializeEntityCustomSerializer;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2018-10-14 18:23:20 +02:00
|
|
|
|
procedure TestDeserializeEntityCustomValueTypeSerializer;
|
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestDeserializeEntityCustomMemberSerializer;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-28 14:52:13 +02:00
|
|
|
|
procedure TestDeserializeEntitySerializationType;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestDeserializeCollection;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-03-30 15:56:24 +02:00
|
|
|
|
procedure TestDeserializeDataSet;
|
2019-05-17 16:15:18 +02:00
|
|
|
|
[Test]
|
2019-05-19 14:35:34 +02:00
|
|
|
|
procedure TestSerializeEmptyDataSet;
|
|
|
|
|
[Test]
|
2020-11-27 15:13:17 +01:00
|
|
|
|
// [Category('this')]
|
2019-05-17 16:15:18 +02:00
|
|
|
|
procedure TestDeserializeEntityWithArray;
|
2017-08-21 15:31:31 +02:00
|
|
|
|
{ full cycle }
|
2017-08-20 02:36:22 +02:00
|
|
|
|
[Test]
|
2017-07-12 11:39:16 +02:00
|
|
|
|
procedure TestSerializeDeSerializeEntityWithEnums;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestStringDictionary;
|
2019-06-26 19:31:53 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestSerializeDeserializeGuid;
|
2019-09-18 01:14:54 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestSerializeDeserializeEntityWithInterface;
|
2019-10-14 23:11:08 +02:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
procedure TestSerializeDeserializeGenericEntity;
|
2019-10-15 09:33:38 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestSerializeDeserializeMultipleGenericEntity;
|
2020-05-04 12:39:54 +02:00
|
|
|
|
[Test]
|
|
|
|
|
procedure TestDoNotSerializeDoNotDeSerialize;
|
2020-09-21 12:42:38 +02:00
|
|
|
|
[Test]
|
2020-11-04 19:06:54 +01:00
|
|
|
|
[Category('serializers')]
|
2020-09-21 12:42:38 +02:00
|
|
|
|
procedure TestSerializeListOfSomething;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
TMVCEntityCustomSerializerJsonDataObjects = class(TInterfacedObject,
|
|
|
|
|
IMVCTypeSerializer)
|
2017-03-01 21:40:57 +01:00
|
|
|
|
private
|
|
|
|
|
{ private declarations }
|
|
|
|
|
protected
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure Serialize(const AElementValue: TValue;
|
|
|
|
|
var ASerializerObject: TObject;
|
2019-05-19 11:06:03 +02:00
|
|
|
|
const AAttributes: TArray<TCustomAttribute>);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure Deserialize(const ASerializedObject: TObject;
|
|
|
|
|
var AElementValue: TValue; const AAttributes: TArray<TCustomAttribute>);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
public
|
2018-10-30 13:53:01 +01:00
|
|
|
|
procedure SerializeRoot(const AObject: TObject;
|
|
|
|
|
out ASerializerObject: TObject;
|
2019-03-13 14:16:33 +01:00
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>;
|
|
|
|
|
const ASerializationAction: TMVCSerializationAction = nil);
|
2018-10-30 13:53:01 +01:00
|
|
|
|
procedure SerializeAttribute(const AElementValue: TValue;
|
|
|
|
|
const APropertyName: string; const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure DeserializeRoot(const ASerializerObject: TObject;
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
procedure DeserializeAttribute(var AElementValue: TValue;
|
|
|
|
|
const APropertyName: string; const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
TMVCNullableIntegerSerializerJsonDataObjects = class(TInterfacedObject,
|
|
|
|
|
IMVCTypeSerializer)
|
2018-10-31 01:07:23 +01:00
|
|
|
|
public
|
|
|
|
|
procedure DeserializeAttribute(var AElementValue: TValue;
|
|
|
|
|
const APropertyName: string; const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
procedure DeserializeRoot(const ASerializerObject: TObject;
|
|
|
|
|
const AObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
procedure SerializeAttribute(const AElementValue: TValue;
|
|
|
|
|
const APropertyName: string; const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
procedure SerializeRoot(const AObject: TObject;
|
|
|
|
|
out ASerializerObject: TObject;
|
2019-03-13 14:16:33 +01:00
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>;
|
|
|
|
|
const ASerializationAction: TMVCSerializationAction = nil);
|
2018-10-14 18:23:20 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-06-23 14:00:39 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// When using nested generic types it is necessary to declare explicitly for delphi's RTTI to recognize them.
|
|
|
|
|
/// </summary>
|
|
|
|
|
TNestedGenericEntity = TGenericEntity<TGenericEntity<TNote>>;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
implementation
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
uses
|
|
|
|
|
MVCFramework.Serializer.JsonDataObjects.CustomTypes,
|
2020-02-03 12:11:42 +01:00
|
|
|
|
MVCFramework.Commons, System.TypInfo, BOs, BusinessObjectsU;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
|
|
|
|
LINE_BREAK = #$A;
|
|
|
|
|
TAB_SPACE = #9;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
{ TMVCTestSerializerJsonDataObjects }
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2018-10-14 18:23:20 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.Setup;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer := TMVCJsonDataObjectsSerializer.Create;
|
2020-11-04 19:06:54 +01:00
|
|
|
|
fSerializer.RegisterTypeSerializer(System.TypeInfo(TStream),
|
|
|
|
|
TMVCStreamSerializerJsonDataObject.Create);
|
|
|
|
|
fSerializer.RegisterTypeSerializer(System.TypeInfo(TStringStream),
|
|
|
|
|
TMVCStreamSerializerJsonDataObject.Create);
|
|
|
|
|
fSerializer.RegisterTypeSerializer(System.TypeInfo(TMemoryStream),
|
|
|
|
|
TMVCStreamSerializerJsonDataObject.Create);
|
|
|
|
|
fSerializer.RegisterTypeSerializer(System.TypeInfo(TEntityCustom),
|
|
|
|
|
TMVCEntityCustomSerializerJsonDataObjects.Create);
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.RegisterTypeSerializer(System.TypeInfo(TMVCNullable<Integer>),
|
2019-05-19 11:06:03 +02:00
|
|
|
|
TMVCNullableIntegerSerializerJsonDataObjects.Create);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TearDown;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer := nil;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestDataSetHelpers;
|
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id":1,' + '"Code":2,' + '"Name":"Ezequiel Juliano M<>ller",' +
|
|
|
|
|
'"Salary":100,' + '"Birthday":"1987-10-15",' +
|
|
|
|
|
'"AccessDateTime":"2017-02-17T16:37:50.000+01:00",' + '"AccessTime":"16:40:50",'
|
|
|
|
|
+ '"Active":true,' + '"Amount":100,' +
|
2020-04-19 12:23:55 +02:00
|
|
|
|
'"BlobFld":"PGh0bWw+PGJvZHk+PGgxPkJMT0I8L2gxPjwvYm9keT48L2h0bWw+",' +
|
2020-11-04 19:06:54 +01:00
|
|
|
|
'"Items":[' + '{' + '"Id":1,' + '"Name":"Ezequiel Juliano M<>ller"' + '},' +
|
|
|
|
|
'{' + '"Id":2,' + '"Name":"Juliano"' + '}' + '],' + '"Departament":{' +
|
|
|
|
|
'"Name":"Depto1"' + '},' +
|
|
|
|
|
'"GUID":"{9386C957-5379-4370-8492-8FA464A9CF0C}"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_LOWERCASE = '{' + '"id":1,' + '"name":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_UPPERCASE = '{' + '"ID":1,' + '"NAME":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_ASIS = '{' + '"Id":1,' + '"Name":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_LIST = '[' + '{' + '"Id_Id":1,' + '"Name_Name":"Ezequiel Juliano M<>ller"'
|
|
|
|
|
+ '},' + '{' + '"Id_Id":2,' + '"Name_Name":"Ezequiel Juliano M<>ller"' +
|
|
|
|
|
'}' + ']';
|
2020-04-19 12:23:55 +02:00
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
Dm: TEntitiesModule;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
Dm := TEntitiesModule.Create(nil);
|
|
|
|
|
try
|
|
|
|
|
Dm.Entity.Insert;
|
|
|
|
|
Dm.EntityId.AsLargeInt := 1;
|
|
|
|
|
Dm.EntityCode.AsInteger := 2;
|
|
|
|
|
Dm.EntityName.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.EntityBirthday.AsDateTime := StrToDate('15/10/1987');
|
|
|
|
|
Dm.EntityAccessDateTime.AsDateTime := StrToDateTime('17/02/2017 16:37:50');
|
|
|
|
|
Dm.EntityAccessTime.AsDateTime := StrToTime('16:40:50');
|
|
|
|
|
Dm.EntityActive.AsBoolean := True;
|
|
|
|
|
Dm.EntitySalary.AsCurrency := 100;
|
|
|
|
|
Dm.EntityAmount.AsFloat := 100;
|
|
|
|
|
Dm.EntityBlobFld.AsString := '<html><body><h1>BLOB</h1></body></html>';
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Dm.EntityGUID.AsGuid :=
|
|
|
|
|
StringToGUID('{9386C957-5379-4370-8492-8FA464A9CF0C}');
|
2020-04-19 12:23:55 +02:00
|
|
|
|
|
|
|
|
|
Dm.Item.Insert;
|
|
|
|
|
Dm.ItemId.AsLargeInt := 1;
|
|
|
|
|
Dm.ItemName.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.Item.Post;
|
|
|
|
|
|
|
|
|
|
Dm.Item.Insert;
|
|
|
|
|
Dm.ItemId.AsLargeInt := 2;
|
|
|
|
|
Dm.ItemName.AsString := 'Juliano';
|
|
|
|
|
Dm.Item.Post;
|
|
|
|
|
|
|
|
|
|
Dm.Departament.Insert;
|
|
|
|
|
Dm.DepartamentName.AsString := 'Depto1';
|
|
|
|
|
Dm.Departament.Post;
|
|
|
|
|
|
|
|
|
|
S := Dm.Entity.AsJSONObject(ncAsIs, ['Ignored']);
|
|
|
|
|
Assert.areEqual(JSON, S, False);
|
|
|
|
|
|
|
|
|
|
Dm.Item.First;
|
|
|
|
|
S := Dm.Item.AsJSONObject(ncAsIs);
|
|
|
|
|
Assert.areEqual(JSON_ASIS, S, False);
|
|
|
|
|
|
|
|
|
|
Dm.Item.First;
|
|
|
|
|
S := Dm.Item.AsJSONObject(ncUpperCase);
|
|
|
|
|
Assert.areEqual(JSON_UPPERCASE, S, False);
|
|
|
|
|
|
|
|
|
|
Dm.Item.First;
|
|
|
|
|
S := Dm.Item.AsJSONObject(ncLowerCase);
|
|
|
|
|
Assert.areEqual(JSON_LOWERCASE, S, False);
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
Dm.Free;
|
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestDeserializeCollection;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
procedure CheckObjectList(const AList: TObjectList<TNote>);
|
|
|
|
|
begin
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(AList.Count = 4);
|
|
|
|
|
Assert.isTrue(AList.Items[0].Description = 'Description 1');
|
|
|
|
|
Assert.isTrue(AList.Items[1].Description = 'Description 2');
|
|
|
|
|
Assert.isTrue(AList.Items[2].Description = 'Description 3');
|
|
|
|
|
Assert.isTrue(AList.Items[3].Description = 'Description 4');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON_PROPERTIES = '[' + '{' + '"Description":"Description 1"' + '},' + '{' +
|
|
|
|
|
'"Description":"Description 2"' + '},' + '{' +
|
|
|
|
|
'"Description":"Description 3"' + '},' + '{' +
|
|
|
|
|
'"Description":"Description 4"' + '}' + ']';
|
|
|
|
|
|
|
|
|
|
JSON_FIELDS = '[' + '{' + '"FDescription":"Description 1"' + '},' + '{' +
|
|
|
|
|
'"FDescription":"Description 2"' + '},' + '{' +
|
|
|
|
|
'"FDescription":"Description 3"' + '},' + '{' +
|
|
|
|
|
'"FDescription":"Description 4"' + '}' + ']';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TObjectList<TNote>;
|
|
|
|
|
begin
|
|
|
|
|
O := TObjectList<TNote>.Create(True);
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeCollection(JSON_PROPERTIES, O, TNote);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
CheckObjectList(O);
|
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
O := TObjectList<TNote>.Create(True);
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeCollection(JSON_FIELDS, O, TNote, stFields);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
CheckObjectList(O);
|
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-30 15:56:24 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestDeserializeDataSet;
|
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id":1,' + '"Code":2,' + '"Name":"Ezequiel Juliano M<>ller",' +
|
|
|
|
|
'"Salary":100,' + '"Birthday":"1987-10-15",' +
|
|
|
|
|
'"AccessDateTime":"2017-02-17 16:37:50",' + '"AccessTime":"16:40:50",'
|
|
|
|
|
+ '"Active":true,' + '"Amount":100,' +
|
2017-08-21 15:31:31 +02:00
|
|
|
|
'"BlobFld":"PGh0bWw+PGJvZHk+PGgxPkJMT0I8L2gxPjwvYm9keT48L2h0bWw+",' +
|
2020-11-04 19:06:54 +01:00
|
|
|
|
'"Items":[' + '{' + '"Id":1,' + '"Name":"Ezequiel"' + '},' + '{' + '"Id":2,'
|
|
|
|
|
+ '"Name":"Juliano"' + '}' + '],' + '"Departament":{' + '"Name":"Depto1"' +
|
|
|
|
|
'},' + '"GUID":"{9386C957-5379-4370-8492-8FA464A9CF0C}"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_LOWERCASE = '{' + '"id":1,' + '"name":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_UPPERCASE = '{' + '"ID":1,' + '"NAME":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_ASIS = '{' + '"Id_Id":1,' +
|
|
|
|
|
'"Name_Name":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_LIST = '[' + '{' + '"Id_Id":1,' + '"Name_Name":"Ezequiel Juliano M<>ller"'
|
|
|
|
|
+ '},' + '{' + '"Id_Id":2,' + '"Name_Name":"Ezequiel Juliano M<>ller"' +
|
|
|
|
|
'}' + ']';
|
|
|
|
|
|
|
|
|
|
JSON_ITEMS = '{' + '"items":[' + '{' + '"Id_Id":1,' +
|
|
|
|
|
'"Name_Name":"Pedro Henrique de Oliveira"' + '},' + '{' + '"Id_Id":2,' +
|
|
|
|
|
'"Name_Name":"Rogers Abe"' + '}' + '],' + '"meta":{"count":"2"}}';
|
2017-03-30 15:56:24 +02:00
|
|
|
|
var
|
|
|
|
|
Dm: TEntitiesModule;
|
|
|
|
|
begin
|
|
|
|
|
Dm := TEntitiesModule.Create(nil);
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeDataSetRecord(JSON, Dm.Entity, ['Ignored']);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityId.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.EntityCode.AsInteger = 2);
|
|
|
|
|
Assert.isTrue(Dm.EntityName.AsString = 'Ezequiel Juliano M<>ller');
|
|
|
|
|
Assert.isTrue(Dm.EntityBirthday.AsDateTime = StrToDate('15/10/1987'));
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.isTrue(Dm.EntityAccessDateTime.AsDateTime = StrToDateTime
|
|
|
|
|
('17/02/2017 16:37:50'));
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityAccessTime.AsDateTime = StrToTime('16:40:50'));
|
|
|
|
|
Assert.isTrue(Dm.EntityActive.AsBoolean = True);
|
|
|
|
|
Assert.isTrue(Dm.EntitySalary.AsCurrency = 100);
|
|
|
|
|
Assert.isTrue(Dm.EntityAmount.AsFloat = 100);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.isTrue(Dm.EntityBlobFld.AsString =
|
|
|
|
|
'<html><body><h1>BLOB</h1></body></html>');
|
|
|
|
|
Assert.isTrue(GUIDToString(Dm.EntityGUID.AsGuid)
|
|
|
|
|
= '{9386C957-5379-4370-8492-8FA464A9CF0C}');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
Dm.Item.First;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.ItemId.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.ItemName.AsString = 'Ezequiel');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
Dm.Item.Next;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.ItemId.AsLargeInt = 2);
|
|
|
|
|
Assert.isTrue(Dm.ItemName.AsString = 'Juliano');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
Dm.Departament.First;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.DepartamentName.AsString = 'Depto1');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeDataSetRecord(JSON_LOWERCASE, Dm.EntityLowerCase);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityLowerCaseId.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.EntityLowerCaseName.AsString = 'Ezequiel Juliano M<>ller');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeDataSetRecord(JSON_UPPERCASE, Dm.EntityUpperCase);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityUpperCaseId.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.EntityUpperCaseName.AsString = 'Ezequiel Juliano M<>ller');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
fSerializer.DeserializeDataSetRecord(JSON_UPPERCASE, Dm.EntityUpperCase2,
|
|
|
|
|
[], ncUpperCase);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityUpperCase2Id.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.EntityUpperCase2Name.AsString = 'Ezequiel Juliano M<>ller');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeDataSetRecord(JSON_ASIS, Dm.EntityAsIs);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityAsIsId.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.EntityAsIsName.AsString = 'Ezequiel Juliano M<>ller');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
Dm.EntityAsIs.EmptyDataSet;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeDataSet(JSON_LIST, Dm.EntityAsIs);
|
2017-03-30 15:56:24 +02:00
|
|
|
|
Dm.EntityAsIs.First;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityAsIsId.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.EntityAsIsName.AsString = 'Ezequiel Juliano M<>ller');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
|
|
|
|
|
Dm.EntityAsIs.Next;
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(Dm.EntityAsIsId.AsLargeInt = 2);
|
|
|
|
|
Assert.isTrue(Dm.EntityAsIsName.AsString = 'Ezequiel Juliano M<>ller');
|
2019-03-13 14:16:33 +01:00
|
|
|
|
|
|
|
|
|
Dm.EntityAsIs.EmptyDataSet;
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Dm.EntityAsIs.LoadJSONArrayFromJSONObjectProperty('items',
|
|
|
|
|
JSON_ITEMS, ncAsIs);
|
2019-03-13 14:16:33 +01:00
|
|
|
|
Dm.EntityAsIs.First;
|
|
|
|
|
Assert.isTrue(Dm.EntityAsIsId.AsLargeInt = 1);
|
|
|
|
|
Assert.isTrue(Dm.EntityAsIsName.AsString = 'Pedro Henrique de Oliveira');
|
|
|
|
|
|
|
|
|
|
Dm.EntityAsIs.Next;
|
|
|
|
|
Assert.isTrue(Dm.EntityAsIsId.AsLargeInt = 2);
|
|
|
|
|
Assert.isTrue(Dm.EntityAsIsName.AsString = 'Rogers Abe');
|
2017-03-30 15:56:24 +02:00
|
|
|
|
finally
|
|
|
|
|
Dm.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestDeserializeEntity;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
procedure CheckObject(const AEntity: TEntity);
|
|
|
|
|
begin
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(AEntity.Id = 1);
|
|
|
|
|
Assert.isTrue(AEntity.Code = 2);
|
|
|
|
|
Assert.isTrue(AEntity.Name = 'Ezequiel Juliano M<>ller');
|
|
|
|
|
Assert.isTrue(AEntity.Salary = 100);
|
|
|
|
|
Assert.isTrue(DateToStr(AEntity.Birthday) = '15/10/1987');
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.isTrue(DateTimeToStr(AEntity.AccessDateTime)
|
|
|
|
|
= '17/02/2017 16:37:50');
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(TimeToStr(AEntity.AccessTime) = '16:40:50');
|
|
|
|
|
Assert.isTrue(AEntity.Active = True);
|
|
|
|
|
Assert.isTrue(AEntity.Role = TRole.roGuest);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.isTrue(DateTimeToStr(TimeStampToDateTime(AEntity.Teporization))
|
|
|
|
|
= '17/02/2017 16:37:50');
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(AEntity.Department <> nil);
|
|
|
|
|
Assert.isTrue(AEntity.Department.Id = 1);
|
|
|
|
|
Assert.isTrue(AEntity.Department.Name = 'Development');
|
|
|
|
|
Assert.isTrue(AEntity.DepartmentNull = nil);
|
|
|
|
|
Assert.isTrue(AEntity.Notes.Count = 2);
|
|
|
|
|
Assert.isTrue(AEntity.Notes[0].Description = 'EntNote1');
|
|
|
|
|
Assert.isTrue(AEntity.Notes[1].Description = 'EntNote2');
|
|
|
|
|
Assert.isTrue(AEntity.NotesEmpty.Count = 0);
|
|
|
|
|
Assert.isTrue(AEntity.AppreciationAs.AsString = 'Yes');
|
|
|
|
|
Assert.isTrue(AEntity.Appreciation.AsString = 'Yes');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON_PROPERTIES = '{' + '"Id":1,' + '"Code":2,' +
|
|
|
|
|
'"Name":"Ezequiel Juliano M<>ller",' + '"Salary":100,' +
|
|
|
|
|
'"Birthday":"1987-10-15",' + '"AccessDateTime":"2017-02-17T16:37:50",'
|
|
|
|
|
+ '"AccessTime":"16:40:50",' + '"Active":true,' + '"Role":"roGuest",' +
|
|
|
|
|
'"Teporization":63623032670000,' + '"Department":{' + '"Id":1,' +
|
|
|
|
|
'"Name":"Development",' + '"Notes":[' + '{' + '"Description":"DepNote1"' +
|
|
|
|
|
'},' + '{' + '"Description":"DepNote2"' + '}' + ']' + '},' +
|
|
|
|
|
'"DepartmentNull":null,' + '"Notes":[' + '{' + '"Description":"EntNote1"' +
|
|
|
|
|
'},' + '{' + '"Description":"EntNote2"' + '}' + '],' + '"NotesEmpty":[],' +
|
|
|
|
|
'"AppreciationAs":"Yes",' + '"Appreciation":{' + '"type":"ustring",' +
|
|
|
|
|
'"value":"Yes"' + '}' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_FIELDS = '{' + '"FId":1,' + '"FCode":2,' +
|
|
|
|
|
'"FName":"Ezequiel Juliano M<>ller",' + '"FSalary":100,' +
|
2017-08-21 15:31:31 +02:00
|
|
|
|
'"FBirthday":"1987-10-15",' +
|
2020-11-04 19:06:54 +01:00
|
|
|
|
'"FAccessDateTime":"2017-02-17T16:37:50",' +
|
|
|
|
|
'"FAccessTime":"16:40:50",' + '"FActive":true,' + '"FRole":"roGuest",' +
|
|
|
|
|
'"FTeporization":63623032670000,' + '"FDepartment":{' + '"FId":1,' +
|
|
|
|
|
'"FName":"Development",' + '"FNotes":[' + '{' + '"FDescription":"DepNote1"'
|
|
|
|
|
+ '},' + '{' + '"FDescription":"DepNote2"' + '}' + ']' + '},' +
|
|
|
|
|
'"FDepartmentNull":null,' + '"FNotes":[' + '{' + '"FDescription":"EntNote1"'
|
|
|
|
|
+ '},' + '{' + '"FDescription":"EntNote2"' + '}' + '],' +
|
|
|
|
|
'"FNotesEmpty":[],' + '"FAppreciationAs":"Yes",' + '"FAppreciation":{' +
|
|
|
|
|
'"type":"ustring",' + '"value":"Yes"' + '}' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TEntity;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntity.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON_PROPERTIES, O);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
CheckObject(O);
|
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
O := TEntity.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON_FIELDS, O, stFields);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
CheckObject(O);
|
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestDeserializeEntityCustomMemberSerializer;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Entity":{' + '"Id":1,' + '"Code":2,' +
|
|
|
|
|
'"Name":"Ezequiel Juliano M<>ller"' + '},' +
|
2017-08-21 15:31:31 +02:00
|
|
|
|
'"Notes":"RXplcXVpZWwgSnVsaWFubyBN/GxsZXI=",' +
|
2020-11-04 19:06:54 +01:00
|
|
|
|
'"NotesAsString":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TSale;
|
|
|
|
|
begin
|
|
|
|
|
O := TSale.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON, O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(O.Entity.Id = 1);
|
|
|
|
|
Assert.isTrue(O.Entity.Code = 2);
|
|
|
|
|
Assert.isTrue(O.Entity.Name = 'Ezequiel Juliano M<>ller');
|
|
|
|
|
Assert.isTrue(O.Notes.DataString = 'Ezequiel Juliano M<>ller');
|
|
|
|
|
Assert.isTrue(O.NotesAsString.DataString = 'Ezequiel Juliano M<>ller');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestDeserializeEntityCustomSerializer;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id":1,' + '"Code":2,' +
|
|
|
|
|
'"Name":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TEntityCustom;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityCustom.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON, O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(O.Id = 1);
|
|
|
|
|
Assert.isTrue(O.Code = 2);
|
|
|
|
|
Assert.isTrue(O.Name = 'Ezequiel Juliano M<>ller');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestDeserializeEntityCustomValueTypeSerializer;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id":1,' + '"Code":2,' + '"Name":"Ezequiel Juliano M<>ller",' +
|
|
|
|
|
'"NullableInteger":3' + '}';
|
2018-10-14 18:23:20 +02:00
|
|
|
|
var
|
|
|
|
|
O: TEntityCustomWithNullables;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityCustomWithNullables.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON, O);
|
2018-10-14 18:23:20 +02:00
|
|
|
|
Assert.isTrue(O.Id = 1);
|
|
|
|
|
Assert.isTrue(O.Code = 2);
|
|
|
|
|
Assert.isTrue(O.Name = 'Ezequiel Juliano M<>ller');
|
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestDeserializeEntitySerializationType;
|
2017-03-28 14:52:13 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON_FIELDS = '{' + '"FId":1,' + '"FCode":2,' +
|
|
|
|
|
'"FName":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_PROPERTIES = '{' + '"Id":1,' + '"Code":2,' +
|
|
|
|
|
'"Name":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-28 14:52:13 +02:00
|
|
|
|
var
|
|
|
|
|
OFields: TEntitySerializeFields;
|
|
|
|
|
OProperties: TEntitySerializeProperties;
|
|
|
|
|
begin
|
|
|
|
|
OFields := TEntitySerializeFields.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON_FIELDS, OFields);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(OFields.Id = 1);
|
|
|
|
|
Assert.isTrue(OFields.Code = 2);
|
|
|
|
|
Assert.isTrue(OFields.Name = 'Ezequiel Juliano M<>ller');
|
2017-03-28 14:52:13 +02:00
|
|
|
|
finally
|
|
|
|
|
OFields.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
OProperties := TEntitySerializeProperties.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON_PROPERTIES, OProperties);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.isTrue(OProperties.Id = 1);
|
|
|
|
|
Assert.isTrue(OProperties.Code = 2);
|
|
|
|
|
Assert.isTrue(OProperties.Name = 'Ezequiel Juliano M<>ller');
|
2017-03-28 14:52:13 +02:00
|
|
|
|
finally
|
|
|
|
|
OProperties.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-05-17 16:15:18 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestDeserializeEntityWithArray;
|
|
|
|
|
procedure CheckObject(const AEntity: TEntityWithArray);
|
|
|
|
|
begin
|
|
|
|
|
Assert.isTrue(AEntity.Id = 1);
|
|
|
|
|
Assert.isTrue(AEntity.Names[0] = 'Pedro');
|
|
|
|
|
Assert.isTrue(AEntity.Names[1] = 'Oliveira');
|
|
|
|
|
Assert.isTrue(AEntity.Values[0] = 1);
|
|
|
|
|
Assert.isTrue(AEntity.Values[1] = 2);
|
2020-11-27 15:13:17 +01:00
|
|
|
|
Assert.isTrue(AEntity.Booleans[0] = true);
|
|
|
|
|
Assert.isTrue(AEntity.Booleans[1] = false);
|
|
|
|
|
Assert.isTrue(AEntity.Booleans[2] = true);
|
2019-05-17 16:15:18 +02:00
|
|
|
|
end;
|
2019-05-19 14:35:34 +02:00
|
|
|
|
|
2019-05-17 16:15:18 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON_WITH_ARRAY = '{' + '"Id":1,' + '"Names":["Pedro","Oliveira"],' +
|
2020-11-27 15:13:17 +01:00
|
|
|
|
'"Values":[1,2],"Booleans":[true,false,true]}';
|
2019-05-17 16:15:18 +02:00
|
|
|
|
var
|
|
|
|
|
O: TEntityWithArray;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityWithArray.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(JSON_WITH_ARRAY, O);
|
2019-05-17 16:15:18 +02:00
|
|
|
|
CheckObject(O);
|
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-05-04 12:39:54 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestDoNotSerializeDoNotDeSerialize;
|
|
|
|
|
var
|
|
|
|
|
lObj: TPartialSerializableType;
|
|
|
|
|
lStr: string;
|
|
|
|
|
begin
|
|
|
|
|
lObj := TPartialSerializableType.Create;
|
|
|
|
|
try
|
|
|
|
|
lStr := fSerializer.SerializeObject(lObj);
|
|
|
|
|
Assert.DoesNotContain(lStr, 'prop1', False);
|
|
|
|
|
Assert.Contains(lStr, 'prop2', False);
|
|
|
|
|
Assert.DoesNotContain(lStr, 'prop3', False);
|
|
|
|
|
Assert.Contains(lStr, 'prop4', False);
|
|
|
|
|
finally
|
|
|
|
|
lObj.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
lObj := TPartialSerializableType.Create;
|
|
|
|
|
try
|
2020-11-04 19:06:54 +01:00
|
|
|
|
fSerializer.DeserializeObject
|
|
|
|
|
('{"prop1":"x1","prop2":"x2","prop3":"x3","prop4":"x4"}', lObj);
|
2020-05-04 12:39:54 +02:00
|
|
|
|
Assert.areEqual('x1', lObj.Prop1);
|
|
|
|
|
Assert.areEqual('prop2', lObj.Prop2);
|
|
|
|
|
Assert.areEqual('prop3', lObj.Prop3);
|
|
|
|
|
Assert.areEqual('x4', lObj.Prop4);
|
|
|
|
|
finally
|
|
|
|
|
lObj.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2020-02-03 12:11:42 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeAllNullableTypes;
|
|
|
|
|
var
|
|
|
|
|
lObj1, lObj2: BusinessObjectsU.TNullablesTest;
|
|
|
|
|
lSer: string;
|
|
|
|
|
begin
|
|
|
|
|
lObj1 := BusinessObjectsU.TNullablesTest.Create;
|
|
|
|
|
try
|
|
|
|
|
lObj1.LoadSomeData;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
lSer := fSerializer.SerializeObject(lObj1);
|
2020-02-03 12:11:42 +01:00
|
|
|
|
lObj2 := BusinessObjectsU.TNullablesTest.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(lSer, lObj2);
|
|
|
|
|
Assert.isTrue(lObj1.Equals(lObj2));
|
2020-02-03 12:11:42 +01:00
|
|
|
|
finally
|
|
|
|
|
lObj2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lObj1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-11-05 14:40:39 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeAllTypes;
|
|
|
|
|
var
|
|
|
|
|
lObj1, lObj2: TMyObject;
|
|
|
|
|
lSer: string;
|
|
|
|
|
begin
|
|
|
|
|
lObj1 := GetMyObject;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
lSer := fSerializer.SerializeObject(lObj1);
|
2019-11-05 14:40:39 +01:00
|
|
|
|
lObj2 := TMyObject.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(lSer, lObj2);
|
|
|
|
|
Assert.isTrue(lObj1.Equals(lObj2));
|
2019-11-05 14:40:39 +01:00
|
|
|
|
finally
|
|
|
|
|
lObj2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lObj1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeAllTypesInList;
|
|
|
|
|
var
|
|
|
|
|
lList1, lList2: TObjectList<TMyObject>;
|
|
|
|
|
lSer: string;
|
|
|
|
|
I: Integer;
|
|
|
|
|
lObj: TMyObject;
|
|
|
|
|
begin
|
|
|
|
|
lList1 := TObjectList<TMyObject>.Create;
|
|
|
|
|
try
|
|
|
|
|
for I := 0 to 9 do
|
|
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
|
lObj := GetMyObject;
|
2019-11-05 14:40:39 +01:00
|
|
|
|
lObj.PropJSONObject.I['value'] := I;
|
|
|
|
|
lList1.Add(lObj);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
lSer := fSerializer.SerializeCollection(lList1);
|
2019-11-05 14:40:39 +01:00
|
|
|
|
|
|
|
|
|
lList2 := TObjectList<TMyObject>.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeCollection(lSer, lList2, TMyObject);
|
2019-11-05 14:40:39 +01:00
|
|
|
|
for I := 0 to 9 do
|
|
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.isTrue(lList1[I].Equals(lList2[I]));
|
2019-11-05 14:40:39 +01:00
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lList2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lList1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeCollection;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '[' + '{' + '"Description":"Description 1"' + '},' + '{' +
|
|
|
|
|
'"Description":"Description 2"' + '},' + '{' +
|
|
|
|
|
'"Description":"Description 3"' + '},' + '{' +
|
|
|
|
|
'"Description":"Description 4"' + '}' + ']';
|
|
|
|
|
|
|
|
|
|
JSON_FIELDS = '[' + '{' + '"FDescription":"Description 1"' + '},' + '{' +
|
|
|
|
|
'"FDescription":"Description 2"' + '},' + '{' +
|
|
|
|
|
'"FDescription":"Description 3"' + '},' + '{' +
|
|
|
|
|
'"FDescription":"Description 4"' + '}' + ']';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TObjectList<TNote>;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TObjectList<TNote>.Create(True);
|
|
|
|
|
try
|
|
|
|
|
O.Add(TNote.Create('Description 1'));
|
|
|
|
|
O.Add(TNote.Create('Description 2'));
|
|
|
|
|
O.Add(TNote.Create('Description 3'));
|
|
|
|
|
O.Add(TNote.Create('Description 4'));
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeCollection(O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeCollection(O, stFields);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON_FIELDS, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-29 14:49:35 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeDataSet;
|
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id":1,' + '"Code":2,' + '"Name":"Ezequiel Juliano M<>ller",' +
|
|
|
|
|
'"Salary":100,' + '"Birthday":"1987-10-15",' +
|
|
|
|
|
'"AccessDateTime":"2017-02-17T16:37:50.000+01:00",' + '"AccessTime":"16:40:50",'
|
|
|
|
|
+ '"Active":true,' + '"Amount":100,' +
|
2017-08-21 15:31:31 +02:00
|
|
|
|
'"BlobFld":"PGh0bWw+PGJvZHk+PGgxPkJMT0I8L2gxPjwvYm9keT48L2h0bWw+",' +
|
2020-11-04 19:06:54 +01:00
|
|
|
|
'"Items":[' + '{' + '"Id":1,' + '"Name":"Ezequiel"' + '},' + '{' + '"Id":2,'
|
|
|
|
|
+ '"Name":"Juliano"' + '}' + '],' + '"Departament":{' + '"Name":"Depto1"' +
|
|
|
|
|
'},' + '"GUID":"{9386C957-5379-4370-8492-8FA464A9CF0C}"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_LOWERCASE = '{' + '"id":1,' + '"name":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_UPPERCASE = '{' + '"ID":1,' + '"NAME":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_ASIS = '{' + '"Id_Id":1,' +
|
|
|
|
|
'"Name_Name":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_LIST = '[' + '{' + '"Id_Id":1,' + '"Name_Name":"Ezequiel Juliano M<>ller"'
|
|
|
|
|
+ '},' + '{' + '"Id_Id":2,' + '"Name_Name":"Ezequiel Juliano M<>ller"' +
|
|
|
|
|
'}' + ']';
|
2019-03-13 14:16:33 +01:00
|
|
|
|
|
2017-03-29 14:49:35 +02:00
|
|
|
|
var
|
|
|
|
|
Dm: TEntitiesModule;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
Dm := TEntitiesModule.Create(nil);
|
|
|
|
|
try
|
|
|
|
|
Dm.Entity.Insert;
|
|
|
|
|
Dm.EntityId.AsLargeInt := 1;
|
|
|
|
|
Dm.EntityCode.AsInteger := 2;
|
|
|
|
|
Dm.EntityName.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.EntityBirthday.AsDateTime := StrToDate('15/10/1987');
|
|
|
|
|
Dm.EntityAccessDateTime.AsDateTime := StrToDateTime('17/02/2017 16:37:50');
|
|
|
|
|
Dm.EntityAccessTime.AsDateTime := StrToTime('16:40:50');
|
|
|
|
|
Dm.EntityActive.AsBoolean := True;
|
|
|
|
|
Dm.EntitySalary.AsCurrency := 100;
|
|
|
|
|
Dm.EntityAmount.AsFloat := 100;
|
|
|
|
|
Dm.EntityBlobFld.AsString := '<html><body><h1>BLOB</h1></body></html>';
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Dm.EntityGUID.AsGuid :=
|
|
|
|
|
StringToGUID('{9386C957-5379-4370-8492-8FA464A9CF0C}');
|
2017-03-29 14:49:35 +02:00
|
|
|
|
|
|
|
|
|
Dm.Item.Insert;
|
|
|
|
|
Dm.ItemId.AsLargeInt := 1;
|
|
|
|
|
Dm.ItemName.AsString := 'Ezequiel';
|
|
|
|
|
Dm.Item.Post;
|
|
|
|
|
|
|
|
|
|
Dm.Item.Insert;
|
|
|
|
|
Dm.ItemId.AsLargeInt := 2;
|
|
|
|
|
Dm.ItemName.AsString := 'Juliano';
|
|
|
|
|
Dm.Item.Post;
|
|
|
|
|
|
|
|
|
|
Dm.Departament.Insert;
|
|
|
|
|
Dm.DepartamentName.AsString := 'Depto1';
|
|
|
|
|
Dm.Departament.Post;
|
|
|
|
|
|
|
|
|
|
Dm.Entity.Post;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeDataSetRecord(Dm.Entity, ['Ignored'], ncAsIs);
|
|
|
|
|
Assert.areEqual(JSON, S, False);
|
2017-03-29 14:49:35 +02:00
|
|
|
|
|
|
|
|
|
Dm.EntityLowerCase.Insert;
|
|
|
|
|
Dm.EntityLowerCaseId.AsLargeInt := 1;
|
|
|
|
|
Dm.EntityLowerCaseName.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.EntityLowerCase.Post;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeDataSetRecord(Dm.EntityLowerCase);
|
|
|
|
|
Assert.areEqual(JSON_LOWERCASE, S, False, 'json lowercase');
|
2017-03-29 14:49:35 +02:00
|
|
|
|
|
|
|
|
|
Dm.EntityUpperCase.Insert;
|
|
|
|
|
Dm.EntityUpperCaseId.AsLargeInt := 1;
|
|
|
|
|
Dm.EntityUpperCaseName.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.EntityUpperCase.Post;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeDataSetRecord(Dm.EntityUpperCase);
|
|
|
|
|
Assert.areEqual(JSON_UPPERCASE, S, False, 'json uppercase (1)');
|
2017-03-29 14:49:35 +02:00
|
|
|
|
|
|
|
|
|
Dm.EntityUpperCase2.Insert;
|
|
|
|
|
Dm.EntityUpperCase2Id.AsLargeInt := 1;
|
|
|
|
|
Dm.EntityUpperCase2Name.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.EntityUpperCase2.Post;
|
2020-11-04 19:06:54 +01:00
|
|
|
|
S := fSerializer.SerializeDataSetRecord(Dm.EntityUpperCase2, [],
|
|
|
|
|
ncUpperCase);
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual(JSON_UPPERCASE, S, False, 'json uppercase (2)');
|
2017-03-29 14:49:35 +02:00
|
|
|
|
|
|
|
|
|
Dm.EntityAsIs.Insert;
|
|
|
|
|
Dm.EntityAsIsId.AsLargeInt := 1;
|
|
|
|
|
Dm.EntityAsIsName.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.EntityAsIs.Post;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeDataSetRecord(Dm.EntityAsIs);
|
|
|
|
|
Assert.areEqual(JSON_ASIS, S, False, 'json as is');
|
2017-03-29 14:49:35 +02:00
|
|
|
|
|
|
|
|
|
Dm.EntityAsIs.Append;
|
|
|
|
|
Dm.EntityAsIsId.AsLargeInt := 2;
|
|
|
|
|
Dm.EntityAsIsName.AsString := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
Dm.EntityAsIs.Post;
|
2019-02-24 20:43:06 +01:00
|
|
|
|
|
2019-05-19 11:06:03 +02:00
|
|
|
|
// serialize dataset
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeDataSet(Dm.EntityAsIs);
|
|
|
|
|
Assert.areEqual(JSON_LIST, S, False, 'json list');
|
2019-02-24 20:43:06 +01:00
|
|
|
|
|
2019-05-19 11:06:03 +02:00
|
|
|
|
// serialize dataset as object
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(Dm.EntityAsIs);
|
|
|
|
|
Assert.areEqual(JSON_LIST, S, False, 'json list');
|
2019-02-24 20:43:06 +01:00
|
|
|
|
|
2017-03-29 14:49:35 +02:00
|
|
|
|
finally
|
|
|
|
|
Dm.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeDateTimeProperty;
|
|
|
|
|
var
|
|
|
|
|
lObj1, lObj2: TMyObjectWithUTC;
|
|
|
|
|
lSer: string;
|
|
|
|
|
begin
|
|
|
|
|
lObj1 := TMyObjectWithUTC.Create;
|
|
|
|
|
try
|
|
|
|
|
lObj1.MyDateTime := EncodeDateTime(2020, 11, 4, 12, 12, 12, 0);
|
|
|
|
|
lSer := fSerializer.SerializeObject(lObj1);
|
|
|
|
|
lObj2 := TMyObjectWithUTC.Create;
|
|
|
|
|
try
|
|
|
|
|
fSerializer.DeserializeObject(lSer, lObj2);
|
|
|
|
|
Assert.isTrue(lObj1.Equals(lObj2));
|
|
|
|
|
finally
|
|
|
|
|
lObj2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lObj1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestSerializeDeSerializeEntityWithEnums;
|
2017-07-12 11:39:16 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id":1,' + '"Code":2,' + '"Name":"Daniele Teti",' +
|
|
|
|
|
'"Color":"RED",' + '"MonthName":"January",' + '"MonthName2":"meFebruary",' +
|
|
|
|
|
'"MonthOrder":0' + '}';
|
2017-07-12 11:39:16 +02:00
|
|
|
|
var
|
|
|
|
|
O: TEntityWithEnums;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityWithEnums.Create;
|
|
|
|
|
try
|
|
|
|
|
O.Id := 1;
|
|
|
|
|
O.Code := 2;
|
|
|
|
|
O.Name := 'Daniele Teti';
|
|
|
|
|
O.Color := TColorEnum.RED;
|
2019-08-12 22:27:36 +02:00
|
|
|
|
O.MonthName := TMonthEnum.meJanuary;
|
2020-03-12 18:24:20 +01:00
|
|
|
|
O.MonthName2 := TMonthEnum.meFebruary;
|
2019-08-12 22:27:36 +02:00
|
|
|
|
O.MonthOrder := TMonthEnum.meJanuary;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-07-12 11:39:16 +02:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
O := TEntityWithEnums.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(S, O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(int64(1), O.Id);
|
|
|
|
|
Assert.areEqual(2, O.Code);
|
|
|
|
|
Assert.areEqual('Daniele Teti', O.Name);
|
2019-08-12 22:27:36 +02:00
|
|
|
|
Assert.areEqual(Ord(TMonthEnum.meJanuary), Ord(O.MonthName));
|
2020-03-12 18:24:20 +01:00
|
|
|
|
Assert.areEqual(Ord(TMonthEnum.meFebruary), Ord(O.MonthName2));
|
2019-08-12 22:27:36 +02:00
|
|
|
|
Assert.areEqual(Ord(TMonthEnum.meJanuary), Ord(O.MonthOrder));
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(Ord(TColorEnum.RED), Ord(O.Color));
|
2017-07-12 11:39:16 +02:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-05-19 11:06:03 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEmptyDataSet;
|
|
|
|
|
var
|
|
|
|
|
Dm: TEntitiesModule;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
Dm := TEntitiesModule.Create(nil);
|
|
|
|
|
try
|
|
|
|
|
Dm.Entity.EmptyDataSet;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeDataSet(Dm.Entity);
|
2019-05-19 14:35:34 +02:00
|
|
|
|
Assert.areEqual('[]', S);
|
2019-05-19 11:06:03 +02:00
|
|
|
|
finally
|
|
|
|
|
Dm.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEntity;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON_PROPERTIES = '{' + '"Id":1,' + '"Code":2,' +
|
|
|
|
|
'"Name":"Ezequiel Juliano M<>ller",' + '"Salary":100,' +
|
|
|
|
|
'"Birthday":"1987-10-15",' + '"AccessDateTime":"2017-02-17T16:37:50.000+01:00",'
|
|
|
|
|
+ '"AccessTime":"16:40:50",' + '"Active":true,' + '"Role":"roGuest",' +
|
|
|
|
|
'"Teporization":63623032670000,' + '"Department":{' + '"Id":1,' +
|
|
|
|
|
'"Name":"Development",' + '"Notes":[' + '{' + '"Description":"DepNote1"' +
|
|
|
|
|
'},' + '{' + '"Description":"DepNote2"' + '}' + ']' + '},' +
|
|
|
|
|
'"DepartmentNull":null,' + '"Notes":[' + '{' + '"Description":"EntNote1"' +
|
|
|
|
|
'},' + '{' + '"Description":"EntNote2"' + '}' + '],' + '"NotesEmpty":[],' +
|
|
|
|
|
'"AppreciationAs":"Yes",' + '"Appreciation":{' + '"type":"ustring",' +
|
|
|
|
|
'"value":"Yes"' + '}' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_FIELDS = '{' + '"FId":1,' + '"FCode":2,' +
|
|
|
|
|
'"FName":"Ezequiel Juliano M<>ller",' + '"FSalary":100,' +
|
2017-08-21 15:31:31 +02:00
|
|
|
|
'"FBirthday":"1987-10-15",' +
|
2020-11-04 19:06:54 +01:00
|
|
|
|
'"FAccessDateTime":"2017-02-17T16:37:50.000+01:00",' +
|
|
|
|
|
'"FAccessTime":"16:40:50",' + '"FActive":true,' + '"FRole":"roGuest",' +
|
|
|
|
|
'"FTeporization":63623032670000,' + '"FDepartment":{' + '"FId":1,' +
|
|
|
|
|
'"FName":"Development",' + '"FNotes":[' + '{' + '"FDescription":"DepNote1"'
|
|
|
|
|
+ '},' + '{' + '"FDescription":"DepNote2"' + '}' + ']' + '},' +
|
|
|
|
|
'"FDepartmentNull":null,' + '"FNotes":[' + '{' + '"FDescription":"EntNote1"'
|
|
|
|
|
+ '},' + '{' + '"FDescription":"EntNote2"' + '}' + '],' +
|
|
|
|
|
'"FNotesEmpty":[],' + '"FAppreciationAs":"Yes",' + '"FAppreciation":{' +
|
|
|
|
|
'"type":"ustring",' + '"value":"Yes"' + '}' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_NULLS = '{' + '"Id":1,' + '"Code":2,' +
|
|
|
|
|
'"Name":"Ezequiel Juliano M<>ller",' + '"Salary":100,' + '"Birthday":null,' +
|
|
|
|
|
'"AccessDateTime":null,' + '"AccessTime":null,' + '"Active":true,' +
|
|
|
|
|
'"Role":"roGuest",' + '"Teporization":63623032670000,' + '"Department":{' +
|
|
|
|
|
'"Id":1,' + '"Name":"Development",' + '"Notes":[' + '{' +
|
|
|
|
|
'"Description":"DepNote1"' + '},' + '{' + '"Description":"DepNote2"' + '}' +
|
|
|
|
|
']' + '},' + '"DepartmentNull":null,' + '"Notes":[' + '{' +
|
|
|
|
|
'"Description":"EntNote1"' + '},' + '{' + '"Description":"EntNote2"' + '}' +
|
|
|
|
|
'],' + '"NotesEmpty":[],' + '"AppreciationAs":"Yes",' + '"Appreciation":{' +
|
|
|
|
|
'"type":"ustring",' + '"value":"Yes"' + '}' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TEntity;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntity.Create;
|
|
|
|
|
try
|
|
|
|
|
O.Id := 1;
|
|
|
|
|
O.Code := 2;
|
|
|
|
|
O.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
O.Salary := 100;
|
|
|
|
|
O.Birthday := StrToDate('15/10/1987');
|
|
|
|
|
O.AccessDateTime := StrToDateTime('17/02/2017 16:37:50');
|
|
|
|
|
O.AccessTime := StrToTime('16:40:50');
|
|
|
|
|
O.Active := True;
|
|
|
|
|
O.Role := roGuest;
|
|
|
|
|
O.Teporization := DateTimeToTimeStamp(StrToDateTime('17/02/2017 16:37:50'));
|
|
|
|
|
O.Appreciation := 'Yes';
|
|
|
|
|
O.AppreciationAs := 'Yes';
|
|
|
|
|
O.Ignored := 'Yes';
|
|
|
|
|
O.Transient := 'Yes';
|
|
|
|
|
O.Notes.Add(TNote.Create('EntNote1'));
|
|
|
|
|
O.Notes.Add(TNote.Create('EntNote2'));
|
|
|
|
|
O.Department.Id := 1;
|
|
|
|
|
O.Department.Name := 'Development';
|
|
|
|
|
O.Department.Notes.Add(TNote.Create('DepNote1'));
|
|
|
|
|
O.Department.Notes.Add(TNote.Create('DepNote2'));
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O, stProperties, ['Ignored']);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON_PROPERTIES, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O, stFields, ['FIgnored']);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON_FIELDS, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
O.Birthday := 0;
|
|
|
|
|
O.AccessDateTime := 0;
|
|
|
|
|
O.AccessTime := 0;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O, stProperties, ['Ignored']);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON_NULLS, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestSerializeEntityCustomMemberSerializer;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Entity":{' + '"AId":1,' + '"ACode":2,' +
|
|
|
|
|
'"AName":"Ezequiel Juliano M<>ller"' + '},' +
|
2017-08-21 15:31:31 +02:00
|
|
|
|
'"Notes":"RXplcXVpZWwgSnVsaWFubyBN/GxsZXI=",' +
|
2020-11-04 19:06:54 +01:00
|
|
|
|
'"NotesAsString":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TSale;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TSale.Create;
|
|
|
|
|
try
|
|
|
|
|
O.Entity.Id := 1;
|
|
|
|
|
O.Entity.Code := 2;
|
|
|
|
|
O.Entity.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
O.Notes.WriteString('Ezequiel Juliano M<>ller');
|
|
|
|
|
O.NotesAsString.WriteString('Ezequiel Juliano M<>ller');
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEntityCustomSerializer;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"AId":1,' + '"ACode":2,' +
|
|
|
|
|
'"AName":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TEntityCustom;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityCustom.Create;
|
|
|
|
|
try
|
|
|
|
|
O.Id := 1;
|
|
|
|
|
O.Code := 2;
|
|
|
|
|
O.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEntityLowerCaseNames;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"id":1,' + '"code":2,' +
|
|
|
|
|
'"name":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TEntityLowerCase;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityLowerCase.Create;
|
|
|
|
|
try
|
|
|
|
|
O.Id := 1;
|
|
|
|
|
O.Code := 2;
|
|
|
|
|
O.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEntityNameAs;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id_Id":1,' + '"Code_Code":2,' +
|
|
|
|
|
'"Name_Name":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TEntityNameAs;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityNameAs.Create;
|
|
|
|
|
try
|
|
|
|
|
O.Id := 1;
|
|
|
|
|
O.Code := 2;
|
|
|
|
|
O.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O, stFields);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestSerializeEntitySerializationType;
|
2017-03-28 14:52:13 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON_FIELDS = '{' + '"FId":1,' + '"FCode":2,' +
|
|
|
|
|
'"FName":"Ezequiel Juliano M<>ller"' + '}';
|
|
|
|
|
|
|
|
|
|
JSON_PROPERTIES = '{' + '"Id":1,' + '"Code":2,' +
|
|
|
|
|
'"Name":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-28 14:52:13 +02:00
|
|
|
|
var
|
|
|
|
|
OFields: TEntitySerializeFields;
|
|
|
|
|
OProperties: TEntitySerializeProperties;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
OFields := TEntitySerializeFields.Create;
|
|
|
|
|
try
|
|
|
|
|
OFields.Id := 1;
|
|
|
|
|
OFields.Code := 2;
|
|
|
|
|
OFields.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(OFields);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON_FIELDS, S);
|
2017-03-28 14:52:13 +02:00
|
|
|
|
finally
|
|
|
|
|
OFields.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
OProperties := TEntitySerializeProperties.Create;
|
|
|
|
|
try
|
|
|
|
|
OProperties.Id := 1;
|
|
|
|
|
OProperties.Code := 2;
|
|
|
|
|
OProperties.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(OProperties);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON_PROPERTIES, S);
|
2017-03-28 14:52:13 +02:00
|
|
|
|
finally
|
|
|
|
|
OProperties.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEntityUpperCaseNames;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"ID":1,' + '"CODE":2,' +
|
|
|
|
|
'"NAME":"Ezequiel Juliano M<>ller"' + '}';
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
O: TEntityUpperCase;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityUpperCase.Create;
|
|
|
|
|
try
|
|
|
|
|
O.Id := 1;
|
|
|
|
|
O.Code := 2;
|
|
|
|
|
O.Name := 'Ezequiel Juliano M<>ller';
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O);
|
2017-08-20 02:36:22 +02:00
|
|
|
|
Assert.areEqual(JSON, S);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-05-17 16:15:18 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEntityWithArray;
|
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON_WITH_ARRAY = '{' + '"Id":1,' + '"Names":["Pedro","Oliveira"],' +
|
2020-11-27 15:13:17 +01:00
|
|
|
|
'"Values":[1,2],"Booleans":[true,false,true]' + '}';
|
2019-05-17 16:15:18 +02:00
|
|
|
|
var
|
|
|
|
|
O: TEntityWithArray;
|
|
|
|
|
S: string;
|
|
|
|
|
begin
|
|
|
|
|
O := TEntityWithArray.Create;
|
|
|
|
|
try
|
2019-05-19 14:35:34 +02:00
|
|
|
|
O.Id := 1;
|
|
|
|
|
O.Names := ['Pedro', 'Oliveira'];
|
|
|
|
|
O.Values := [1, 2];
|
2020-11-27 15:13:17 +01:00
|
|
|
|
O.Booleans := [true, false, true];
|
2020-04-19 12:23:55 +02:00
|
|
|
|
S := fSerializer.SerializeObject(O);
|
2019-05-17 16:15:18 +02:00
|
|
|
|
Assert.areEqual(JSON_WITH_ARRAY, S);
|
|
|
|
|
finally
|
|
|
|
|
O.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-09-21 12:42:38 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeListOfSomething;
|
|
|
|
|
var
|
|
|
|
|
lList, lList2: TListOfSomething;
|
|
|
|
|
lData: string;
|
|
|
|
|
begin
|
|
|
|
|
lList := TListOfSomething.Create;
|
|
|
|
|
try
|
|
|
|
|
lData := fSerializer.SerializeObject(lList);
|
|
|
|
|
|
|
|
|
|
lList2 := TListOfSomething.Create;
|
|
|
|
|
try
|
|
|
|
|
lList2.ListOfString.Clear;
|
|
|
|
|
lList2.ListOfInteger.Clear;
|
|
|
|
|
lList2.ListOfBoolean.Clear;
|
|
|
|
|
lList2.ListOfDouble.Clear;
|
|
|
|
|
|
|
|
|
|
fSerializer.DeserializeObject(lData, lList2);
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual(2, lList2.ListOfString.Count);
|
|
|
|
|
Assert.areEqual(2, lList2.ListOfInteger.Count);
|
|
|
|
|
Assert.areEqual(2, lList2.ListOfBoolean.Count);
|
|
|
|
|
Assert.areEqual(2, lList2.ListOfDouble.Count);
|
2020-09-21 12:42:38 +02:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual(lList.ListOfString[0], lList2.ListOfString[0]);
|
|
|
|
|
Assert.areEqual(lList.ListOfString[1], lList2.ListOfString[1]);
|
2020-09-21 12:42:38 +02:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual(lList.ListOfInteger[0], lList2.ListOfInteger[0]);
|
|
|
|
|
Assert.areEqual(lList.ListOfInteger[1], lList2.ListOfInteger[1]);
|
2020-09-21 12:42:38 +02:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual(lList.ListOfBoolean[0], lList2.ListOfBoolean[0]);
|
|
|
|
|
Assert.areEqual(lList.ListOfBoolean[1], lList2.ListOfBoolean[1]);
|
2020-09-21 12:42:38 +02:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual(lList.ListOfDouble[0], lList2.ListOfDouble[0], 0.0001);
|
|
|
|
|
Assert.areEqual(lList.ListOfDouble[1], lList2.ListOfDouble[1], 0.0001);
|
2020-09-21 12:42:38 +02:00
|
|
|
|
finally
|
|
|
|
|
lList2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lList.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestSerializeDeserializeEntityWithInterface;
|
2019-09-18 01:14:54 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Id":1,' + '"Name":"Jo<4A>o Ant<6E>nio Duarte",' + '"ChildEntity":{' +
|
|
|
|
|
'"Code":10,' + '"Description":"Child Entity"' + '}' + '}';
|
2019-09-18 01:14:54 +02:00
|
|
|
|
var
|
|
|
|
|
LEntity: IEntityWithInterface;
|
|
|
|
|
LJson: string;
|
|
|
|
|
begin
|
|
|
|
|
LEntity := TEntityWithInterface.Create;
|
|
|
|
|
LEntity.Id := 1;
|
|
|
|
|
LEntity.Name := 'Jo<4A>o Ant<6E>nio Duarte';
|
2019-09-18 13:30:50 +02:00
|
|
|
|
LEntity.ChildEntity.Code := 10;
|
|
|
|
|
LEntity.ChildEntity.Description := 'Child Entity';
|
2019-09-18 01:14:54 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LJson := fSerializer.SerializeObject(LEntity);
|
|
|
|
|
Assert.areEqual(JSON, LJson);
|
2019-09-18 01:14:54 +02:00
|
|
|
|
|
|
|
|
|
LEntity := TEntityWithInterface.Create;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(LJson, LEntity);
|
|
|
|
|
Assert.areEqual(Integer(1), LEntity.Id);
|
|
|
|
|
Assert.areEqual('Jo<4A>o Ant<6E>nio Duarte', LEntity.Name);
|
|
|
|
|
Assert.areEqual(Integer(10), LEntity.ChildEntity.Code);
|
|
|
|
|
Assert.areEqual('Child Entity', LEntity.ChildEntity.Description);
|
2019-09-18 01:14:54 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestSerializeDeserializeGenericEntity;
|
2019-10-14 23:11:08 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Code":1,' + '"Description":"General Description",' +
|
|
|
|
|
'"Items":[' + '{"Description":"Description 01"},' +
|
|
|
|
|
'{"Description":"Description 02"},' + '{"Description":"Description 03"},' +
|
|
|
|
|
'{"Description":"Description 04"},' + '{"Description":"Description 05"}' +
|
|
|
|
|
']' + '}';
|
|
|
|
|
|
|
|
|
|
NESTED_JSON = '{' + '"Code":1,' + '"Description":"General Description",' +
|
|
|
|
|
'"Items":[' + '{' + '"Code":10,' + '"Description":"Item_01",' + '"Items":['
|
|
|
|
|
+ '{"Description":"Description 01"}' + ']' + '},' + '{' + '"Code":11,' +
|
|
|
|
|
'"Description":"Item_02",' + '"Items":[' +
|
|
|
|
|
'{"Description":"Description 02"}' + ']' + '},' + '{' + '"Code":12,' +
|
|
|
|
|
'"Description":"Item_03",' + '"Items":[' +
|
|
|
|
|
'{"Description":"Description 03"}' + ']' + '}' + ']' + '}';
|
2019-10-14 23:11:08 +02:00
|
|
|
|
var
|
|
|
|
|
LGenericEntity: TGenericEntity<TNote>;
|
2020-06-23 14:00:39 +02:00
|
|
|
|
LNestedGenericEntity: TNestedGenericEntity;
|
2019-10-14 23:11:08 +02:00
|
|
|
|
LJson: string;
|
|
|
|
|
begin
|
|
|
|
|
LGenericEntity := TGenericEntity<TNote>.Create;
|
|
|
|
|
try
|
|
|
|
|
LGenericEntity.Code := 1;
|
|
|
|
|
LGenericEntity.Description := 'General Description';
|
|
|
|
|
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 01'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 02'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 03'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 04'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 05'));
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LJson := fSerializer.SerializeObject(LGenericEntity);
|
2019-10-14 23:11:08 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual(JSON, LJson);
|
2019-10-14 23:11:08 +02:00
|
|
|
|
finally
|
|
|
|
|
LGenericEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
LGenericEntity := TGenericEntity<TNote>.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(LJson, LGenericEntity);
|
2019-10-14 23:11:08 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual(Integer(1), LGenericEntity.Code);
|
|
|
|
|
Assert.areEqual('General Description', LGenericEntity.Description);
|
|
|
|
|
Assert.areEqual(Integer(5), LGenericEntity.Items.Count);
|
|
|
|
|
Assert.areEqual('Description 01', LGenericEntity.Items[0].Description);
|
|
|
|
|
Assert.areEqual('Description 02', LGenericEntity.Items[1].Description);
|
|
|
|
|
Assert.areEqual('Description 03', LGenericEntity.Items[2].Description);
|
|
|
|
|
Assert.areEqual('Description 04', LGenericEntity.Items[3].Description);
|
|
|
|
|
Assert.areEqual('Description 05', LGenericEntity.Items[4].Description);
|
2019-10-14 23:11:08 +02:00
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
LGenericEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-06-23 14:00:39 +02:00
|
|
|
|
LNestedGenericEntity := TNestedGenericEntity.Create;
|
|
|
|
|
try
|
|
|
|
|
LNestedGenericEntity.Code := 1;
|
|
|
|
|
LNestedGenericEntity.Description := 'General Description';
|
|
|
|
|
|
|
|
|
|
LGenericEntity := TGenericEntity<TNote>.Create;
|
|
|
|
|
LGenericEntity.Code := 10;
|
|
|
|
|
LGenericEntity.Description := 'Item_01';
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 01'));
|
|
|
|
|
LNestedGenericEntity.Items.Add(LGenericEntity);
|
|
|
|
|
|
|
|
|
|
LGenericEntity := TGenericEntity<TNote>.Create;
|
|
|
|
|
LGenericEntity.Code := 11;
|
|
|
|
|
LGenericEntity.Description := 'Item_02';
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 02'));
|
|
|
|
|
LNestedGenericEntity.Items.Add(LGenericEntity);
|
|
|
|
|
|
|
|
|
|
LGenericEntity := TGenericEntity<TNote>.Create;
|
|
|
|
|
LGenericEntity.Code := 12;
|
|
|
|
|
LGenericEntity.Description := 'Item_03';
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 03'));
|
|
|
|
|
LNestedGenericEntity.Items.Add(LGenericEntity);
|
|
|
|
|
|
|
|
|
|
LJson := fSerializer.SerializeObject(LNestedGenericEntity);
|
|
|
|
|
|
|
|
|
|
Assert.areEqual(NESTED_JSON, LJson);
|
|
|
|
|
finally
|
|
|
|
|
LNestedGenericEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
LNestedGenericEntity := TNestedGenericEntity.Create;;
|
|
|
|
|
try
|
|
|
|
|
fSerializer.DeserializeObject(LJson, LNestedGenericEntity);
|
|
|
|
|
|
2020-09-21 12:42:38 +02:00
|
|
|
|
Assert.areEqual(Integer(1), LNestedGenericEntity.Code);
|
|
|
|
|
Assert.areEqual('General Description', LNestedGenericEntity.Description);
|
|
|
|
|
Assert.areEqual(Integer(3), LNestedGenericEntity.Items.Count);
|
2020-06-23 14:00:39 +02:00
|
|
|
|
|
2020-09-21 12:42:38 +02:00
|
|
|
|
Assert.areEqual(Integer(10), LNestedGenericEntity.Items[0].Code);
|
|
|
|
|
Assert.areEqual('Item_01', LNestedGenericEntity.Items[0].Description);
|
|
|
|
|
Assert.areEqual(Integer(1), LNestedGenericEntity.Items[0].Items.Count);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual('Description 01', LNestedGenericEntity.Items[0].Items[0]
|
|
|
|
|
.Description);
|
2020-06-23 14:00:39 +02:00
|
|
|
|
|
2020-09-21 12:42:38 +02:00
|
|
|
|
Assert.areEqual(Integer(11), LNestedGenericEntity.Items[1].Code);
|
|
|
|
|
Assert.areEqual('Item_02', LNestedGenericEntity.Items[1].Description);
|
|
|
|
|
Assert.areEqual(Integer(1), LNestedGenericEntity.Items[1].Items.Count);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual('Description 02', LNestedGenericEntity.Items[1].Items[0]
|
|
|
|
|
.Description);
|
2020-06-23 14:00:39 +02:00
|
|
|
|
|
2020-09-21 12:42:38 +02:00
|
|
|
|
Assert.areEqual(Integer(12), LNestedGenericEntity.Items[2].Code);
|
|
|
|
|
Assert.areEqual('Item_03', LNestedGenericEntity.Items[2].Description);
|
|
|
|
|
Assert.areEqual(Integer(1), LNestedGenericEntity.Items[2].Items.Count);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual('Description 03', LNestedGenericEntity.Items[2].Items[0]
|
|
|
|
|
.Description);
|
2020-06-23 14:00:39 +02:00
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
LNestedGenericEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
2019-10-14 23:11:08 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2019-06-26 19:31:53 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeDeserializeGuid;
|
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"GuidValue":"{AEED1A0F-9061-40F0-9FDA-D69AE7F20222}",' +
|
|
|
|
|
'"Id":1,' + '"Code":2,' + '"Name":"Jo<4A>o Ant<6E>nio"' + '}';
|
2019-06-26 19:31:53 +02:00
|
|
|
|
var
|
|
|
|
|
LEntity: TEntityCustomWithGuid;
|
|
|
|
|
LJson: string;
|
|
|
|
|
begin
|
|
|
|
|
LEntity := TEntityCustomWithGuid.Create;
|
|
|
|
|
try
|
|
|
|
|
LEntity.Id := 1;
|
|
|
|
|
LEntity.Code := 2;
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LEntity.Name := 'Jo<4A>o Ant<6E>nio';
|
|
|
|
|
LEntity.GuidValue := StringToGUID('{AEED1A0F-9061-40F0-9FDA-D69AE7F20222}');
|
2019-06-26 19:31:53 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LJson := fSerializer.SerializeObject(LEntity);
|
|
|
|
|
Assert.areEqual(JSON, LJson);
|
2019-06-26 19:31:53 +02:00
|
|
|
|
finally
|
|
|
|
|
LEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
LEntity := TEntityCustomWithGuid.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(LJson, LEntity);
|
|
|
|
|
Assert.areEqual(int64(1), LEntity.Id);
|
|
|
|
|
Assert.areEqual(Integer(2), LEntity.Code);
|
|
|
|
|
Assert.areEqual('Jo<4A>o Ant<6E>nio', LEntity.Name);
|
2020-11-04 19:06:54 +01:00
|
|
|
|
Assert.areEqual(StringToGUID('{AEED1A0F-9061-40F0-9FDA-D69AE7F20222}'),
|
|
|
|
|
LEntity.GuidValue);
|
2019-06-26 19:31:53 +02:00
|
|
|
|
finally
|
|
|
|
|
LEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.
|
|
|
|
|
TestSerializeDeserializeMultipleGenericEntity;
|
2019-10-15 09:33:38 +02:00
|
|
|
|
const
|
2020-11-04 19:06:54 +01:00
|
|
|
|
JSON = '{' + '"Code":1,' + '"Description":"General Description",' +
|
|
|
|
|
'"Items":[' + '{"Description":"Description 01"},' +
|
|
|
|
|
'{"Description":"Description 02"},' + '{"Description":"Description 03"},' +
|
|
|
|
|
'{"Description":"Description 04"},' + '{"Description":"Description 05"}' +
|
|
|
|
|
'],' + '"Items2":[' + '{"Description":"Description2 01"},' +
|
|
|
|
|
'{"Description":"Description2 02"},' + '{"Description":"Description2 03"},'
|
|
|
|
|
+ '{"Description":"Description2 04"},' + '{"Description":"Description2 05"}'
|
|
|
|
|
+ ']' + '}';
|
2019-10-15 09:33:38 +02:00
|
|
|
|
var
|
|
|
|
|
LGenericEntity: TMultipleGenericEntity<TNote, TNote>;
|
|
|
|
|
LJson: string;
|
|
|
|
|
begin
|
|
|
|
|
LGenericEntity := TMultipleGenericEntity<TNote, TNote>.Create;
|
|
|
|
|
try
|
|
|
|
|
LGenericEntity.Code := 1;
|
|
|
|
|
LGenericEntity.Description := 'General Description';
|
|
|
|
|
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 01'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 02'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 03'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 04'));
|
|
|
|
|
LGenericEntity.Items.Add(TNote.Create('Description 05'));
|
|
|
|
|
|
|
|
|
|
LGenericEntity.Items2.Add(TNote.Create('Description2 01'));
|
|
|
|
|
LGenericEntity.Items2.Add(TNote.Create('Description2 02'));
|
|
|
|
|
LGenericEntity.Items2.Add(TNote.Create('Description2 03'));
|
|
|
|
|
LGenericEntity.Items2.Add(TNote.Create('Description2 04'));
|
|
|
|
|
LGenericEntity.Items2.Add(TNote.Create('Description2 05'));
|
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LJson := fSerializer.SerializeObject(LGenericEntity);
|
2019-10-15 09:33:38 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual(JSON, LJson);
|
2019-10-15 09:33:38 +02:00
|
|
|
|
finally
|
|
|
|
|
LGenericEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
LGenericEntity := TMultipleGenericEntity<TNote, TNote>.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(LJson, LGenericEntity);
|
2019-10-15 09:33:38 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual(Integer(1), LGenericEntity.Code);
|
|
|
|
|
Assert.areEqual('General Description', LGenericEntity.Description);
|
2019-10-15 09:33:38 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual(Integer(5), LGenericEntity.Items.Count);
|
|
|
|
|
Assert.areEqual('Description 01', LGenericEntity.Items[0].Description);
|
|
|
|
|
Assert.areEqual('Description 02', LGenericEntity.Items[1].Description);
|
|
|
|
|
Assert.areEqual('Description 03', LGenericEntity.Items[2].Description);
|
|
|
|
|
Assert.areEqual('Description 04', LGenericEntity.Items[3].Description);
|
|
|
|
|
Assert.areEqual('Description 05', LGenericEntity.Items[4].Description);
|
2019-10-15 09:33:38 +02:00
|
|
|
|
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual(Integer(5), LGenericEntity.Items2.Count);
|
|
|
|
|
Assert.areEqual('Description2 01', LGenericEntity.Items2[0].Description);
|
|
|
|
|
Assert.areEqual('Description2 02', LGenericEntity.Items2[1].Description);
|
|
|
|
|
Assert.areEqual('Description2 03', LGenericEntity.Items2[2].Description);
|
|
|
|
|
Assert.areEqual('Description2 04', LGenericEntity.Items2[3].Description);
|
|
|
|
|
Assert.areEqual('Description2 05', LGenericEntity.Items2[4].Description);
|
2019-10-15 09:33:38 +02:00
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
LGenericEntity.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2017-05-18 00:02:22 +02:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestSerializeNil;
|
|
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
|
Assert.areEqual('null', fSerializer.SerializeObject(nil));
|
2017-05-18 00:02:22 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure TMVCTestSerializerJsonDataObjects.TestStringDictionary;
|
|
|
|
|
var
|
|
|
|
|
lDict: TMVCStringDictionary;
|
|
|
|
|
lSerString: string;
|
|
|
|
|
lDict2: TMVCStringDictionary;
|
|
|
|
|
begin
|
|
|
|
|
lDict := TMVCStringDictionary.Create;
|
|
|
|
|
try
|
|
|
|
|
lDict['prop1'] := 'value1';
|
|
|
|
|
lDict['prop2'] := 'value2';
|
|
|
|
|
lDict['prop3'] := 'value3';
|
2020-04-19 12:23:55 +02:00
|
|
|
|
lSerString := fSerializer.SerializeObject(lDict);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
lDict2 := TMVCStringDictionary.Create;
|
|
|
|
|
try
|
2020-04-19 12:23:55 +02:00
|
|
|
|
fSerializer.DeserializeObject(lSerString, lDict2);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
Assert.isTrue(lDict2.ContainsKey('prop1'));
|
|
|
|
|
Assert.isTrue(lDict2.ContainsKey('prop2'));
|
|
|
|
|
Assert.isTrue(lDict2.ContainsKey('prop3'));
|
|
|
|
|
Assert.areEqual(lDict['prop1'], lDict2['prop1']);
|
|
|
|
|
Assert.areEqual(lDict['prop2'], lDict2['prop2']);
|
|
|
|
|
Assert.areEqual(lDict['prop3'], lDict2['prop3']);
|
|
|
|
|
finally
|
|
|
|
|
lDict2.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
lDict.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-02 13:27:19 +01:00
|
|
|
|
{ TMVCEntityCustomSerializerJsonDataObjects }
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCEntityCustomSerializerJsonDataObjects.Deserialize
|
|
|
|
|
(const ASerializedObject: TObject; var AElementValue: TValue;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const AAttributes: TArray<TCustomAttribute>);
|
|
|
|
|
var
|
|
|
|
|
JsonObject: TJsonObject;
|
|
|
|
|
EntityCustom: TEntityCustom;
|
|
|
|
|
begin
|
|
|
|
|
JsonObject := ASerializedObject as TJsonObject;
|
|
|
|
|
if Assigned(JsonObject) then
|
|
|
|
|
begin
|
|
|
|
|
EntityCustom := AElementValue.AsObject as TEntityCustom;
|
|
|
|
|
if Assigned(EntityCustom) then
|
|
|
|
|
begin
|
|
|
|
|
EntityCustom.Id := JsonObject['AId'].LongValue;
|
|
|
|
|
EntityCustom.Code := JsonObject['ACode'].IntValue;
|
|
|
|
|
EntityCustom.Name := JsonObject['AName'].Value;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCEntityCustomSerializerJsonDataObjects.DeserializeAttribute
|
|
|
|
|
(var AElementValue: TValue; const APropertyName: string;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
begin
|
|
|
|
|
DeserializeRoot(ASerializerObject, AElementValue.AsObject, AAttributes);
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCEntityCustomSerializerJsonDataObjects.DeserializeRoot
|
|
|
|
|
(const ASerializerObject, AObject: TObject;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
var
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LEntity: TEntityCustom;
|
|
|
|
|
LJson: TJDOJsonObject;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
lAttr: TCustomAttribute;
|
|
|
|
|
lAsLowerCase: Boolean;
|
|
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LEntity := TEntityCustom(AObject);
|
|
|
|
|
LJson := ASerializerObject as TJDOJsonObject;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
lAsLowerCase := False;
|
|
|
|
|
for lAttr in AAttributes do
|
|
|
|
|
begin
|
|
|
|
|
if lAttr is MVCNameCaseAttribute then
|
|
|
|
|
begin
|
|
|
|
|
lAsLowerCase := MVCNameCaseAttribute(lAttr).KeyCase = ncLowerCase;
|
|
|
|
|
break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if lAsLowerCase then
|
|
|
|
|
begin
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LEntity.Id := LJson.I['id'];
|
|
|
|
|
LEntity.Code := LJson.I['code'];
|
|
|
|
|
LEntity.Name := LJson.S['name'];
|
2018-10-31 01:07:23 +01:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
// as is (upper case is not supported by the custom type serializer)
|
2020-04-19 12:23:55 +02:00
|
|
|
|
LEntity.Id := LJson.I['Id'];
|
|
|
|
|
LEntity.Code := LJson.I['Code'];
|
|
|
|
|
LEntity.Name := LJson.S['Name'];
|
2018-10-31 01:07:23 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCEntityCustomSerializerJsonDataObjects.Serialize
|
|
|
|
|
(const AElementValue: TValue; var ASerializerObject: TObject;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
const AAttributes: TArray<TCustomAttribute>);
|
|
|
|
|
var
|
|
|
|
|
EntityCustom: TEntityCustom;
|
|
|
|
|
begin
|
|
|
|
|
EntityCustom := AElementValue.AsObject as TEntityCustom;
|
|
|
|
|
if Assigned(EntityCustom) then
|
|
|
|
|
begin
|
|
|
|
|
ASerializerObject := TJsonObject.Create;
|
|
|
|
|
TJsonObject(ASerializerObject).L['AId'] := EntityCustom.Id;
|
|
|
|
|
TJsonObject(ASerializerObject).I['ACode'] := EntityCustom.Code;
|
|
|
|
|
TJsonObject(ASerializerObject).S['AName'] := EntityCustom.Name;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCEntityCustomSerializerJsonDataObjects.SerializeAttribute
|
|
|
|
|
(const AElementValue: TValue; const APropertyName: string;
|
2018-10-30 13:53:01 +01:00
|
|
|
|
const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
2018-10-14 18:23:20 +02:00
|
|
|
|
var
|
2018-10-30 13:53:01 +01:00
|
|
|
|
lEntityCustom: TEntityCustom;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
begin
|
2018-10-30 13:53:01 +01:00
|
|
|
|
lEntityCustom := AElementValue.AsObject as TEntityCustom;
|
|
|
|
|
if Assigned(lEntityCustom) then
|
2018-10-14 18:23:20 +02:00
|
|
|
|
begin
|
2020-11-04 19:06:54 +01:00
|
|
|
|
TJsonObject(ASerializerObject).O[APropertyName].L['AId'] :=
|
|
|
|
|
lEntityCustom.Id;
|
|
|
|
|
TJsonObject(ASerializerObject).O[APropertyName].I['ACode'] :=
|
|
|
|
|
lEntityCustom.Code;
|
|
|
|
|
TJsonObject(ASerializerObject).O[APropertyName].S['AName'] :=
|
|
|
|
|
lEntityCustom.Name;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
2018-10-30 13:53:01 +01:00
|
|
|
|
TJsonObject(ASerializerObject).Values[APropertyName] := nil;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCEntityCustomSerializerJsonDataObjects.SerializeRoot
|
|
|
|
|
(const AObject: TObject; out ASerializerObject: TObject;
|
2019-03-13 14:16:33 +01:00
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>;
|
|
|
|
|
const ASerializationAction: TMVCSerializationAction);
|
2018-10-30 13:53:01 +01:00
|
|
|
|
var
|
|
|
|
|
lEntityCustom: TEntityCustom;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
begin
|
2018-10-30 13:53:01 +01:00
|
|
|
|
ASerializerObject := nil;
|
|
|
|
|
lEntityCustom := AObject as TEntityCustom;
|
|
|
|
|
if Assigned(lEntityCustom) then
|
|
|
|
|
begin
|
|
|
|
|
ASerializerObject := TJsonObject.Create;
|
|
|
|
|
TJsonObject(ASerializerObject).L['AId'] := lEntityCustom.Id;
|
|
|
|
|
TJsonObject(ASerializerObject).I['ACode'] := lEntityCustom.Code;
|
|
|
|
|
TJsonObject(ASerializerObject).S['AName'] := lEntityCustom.Name;
|
|
|
|
|
end;
|
2018-10-14 18:23:20 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
{ TMVCNullableIntegerSerializerJsonDataObjects }
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCNullableIntegerSerializerJsonDataObjects.DeserializeAttribute
|
|
|
|
|
(var AElementValue: TValue; const APropertyName: string;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCNullableIntegerSerializerJsonDataObjects.DeserializeRoot
|
|
|
|
|
(const ASerializerObject, AObject: TObject;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCNullableIntegerSerializerJsonDataObjects.SerializeAttribute
|
|
|
|
|
(const AElementValue: TValue; const APropertyName: string;
|
2018-10-31 01:07:23 +01:00
|
|
|
|
const ASerializerObject: TObject;
|
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>);
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2020-11-04 19:06:54 +01:00
|
|
|
|
procedure TMVCNullableIntegerSerializerJsonDataObjects.SerializeRoot
|
|
|
|
|
(const AObject: TObject; out ASerializerObject: TObject;
|
2019-03-13 14:16:33 +01:00
|
|
|
|
const AAttributes: System.TArray<System.TCustomAttribute>;
|
|
|
|
|
const ASerializationAction: TMVCSerializationAction);
|
2018-10-31 01:07:23 +01:00
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
initialization
|
|
|
|
|
|
2017-08-20 02:36:22 +02:00
|
|
|
|
TDUnitX.RegisterTestFixture(TMVCTestSerializerJsonDataObjects);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
end.
|