From 8eb55556bc6223a5ca7b1084b336444680e01e46 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Wed, 12 Jul 2017 01:02:23 +0200 Subject: [PATCH] ADDED support for enumeration types in System.JSON based serializer --- sources/MVCFramework.Serializer.JSON.pas | 7 +++- ...MVCFramework.Tests.Serializer.Entities.pas | 16 ++++++++ .../MVCFramework.Tests.Serializer.JSON.pas | 39 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/sources/MVCFramework.Serializer.JSON.pas b/sources/MVCFramework.Serializer.JSON.pas index e016a168..879b0f0a 100644 --- a/sources/MVCFramework.Serializer.JSON.pas +++ b/sources/MVCFramework.Serializer.JSON.pas @@ -262,7 +262,9 @@ begin AJSONObject.AddPair(AName, TJSONBool.Create(False)); end else - AJSONObject.AddPair(AName, TJSONNumber.Create(AValue.AsOrdinal)); + begin + AJSONObject.AddPair(AName, GetEnumName(AValue.TypeInfo, AValue.AsOrdinal)); + end; end; tkClass: @@ -602,6 +604,9 @@ begin else if (AValue.TypeInfo = System.TypeInfo(TTime)) then AValue := TValue.From(ISOTimeToTime(AJSONObject.Values[AName].Value)) + else if (AValue.Kind = tkEnumeration) then + TValue.Make(GetEnumValue(AValue.TypeInfo, AJsonObject.Values[AName].Value), AValue.TypeInfo, AValue) + else AValue := TValue.From(AJSONObject.Values[AName].Value); end; diff --git a/unittests/common/MVCFramework.Tests.Serializer.Entities.pas b/unittests/common/MVCFramework.Tests.Serializer.Entities.pas index 9a5b7634..3ce907e0 100644 --- a/unittests/common/MVCFramework.Tests.Serializer.Entities.pas +++ b/unittests/common/MVCFramework.Tests.Serializer.Entities.pas @@ -188,6 +188,22 @@ type property Name: string read FName write FName; end; + TColorEnum = (RED, GREEN, BLUE); + + TEntityWithEnums = class + private + FId: Int64; + FCode: Integer; + FName: string; + FColor: TColorEnum; + public + property Id: Int64 read FId write FId; + property Code: Integer read FCode write FCode; + property Name: string read FName write FName; + property Color: TColorEnum read FColor write FColor; + end; + + [MVCSerialize(stFields)] TEntitySerializeFields = class private diff --git a/unittests/serializer/systemjson/MVCFramework.Tests.Serializer.JSON.pas b/unittests/serializer/systemjson/MVCFramework.Tests.Serializer.JSON.pas index 396bf507..02e3827b 100644 --- a/unittests/serializer/systemjson/MVCFramework.Tests.Serializer.JSON.pas +++ b/unittests/serializer/systemjson/MVCFramework.Tests.Serializer.JSON.pas @@ -70,6 +70,8 @@ type procedure TestDeserializeEntitySerializationType; procedure TestDeserializeCollection; procedure TestDeserializeDataSet; + { full cycle } + procedure TestSerializeDeSerializeEntityWithEnums; end; TMVCEntityCustomSerializerJSON = class(TInterfacedObject, IMVCTypeSerializer) @@ -1029,6 +1031,43 @@ begin end; end; +procedure TMVCTestSerializerJSON.TestSerializeDeSerializeEntityWithEnums; +const + JSON = + '{' + + '"Id":1,' + + '"Code":2,' + + '"Name":"Daniele Teti",' + + '"Color":"RED"' + + '}'; +var + O: TEntityWithEnums; + S: string; +begin + O := TEntityWithEnums.Create; + try + O.Id := 1; + O.Code := 2; + O.Name := 'Daniele Teti'; + O.Color := TColorEnum.RED; + S := FSerializer.SerializeObject(O); + CheckEqualsString(JSON, S); + finally + O.Free; + end; + + O := TEntityWithEnums.Create; + try + FSerializer.DeserializeObject(S, O); + CheckEquals(1, O.Id); + CheckEquals(2, O.Code); + CheckEquals('Daniele Teti', O.Name); + CheckEquals(Ord(TColorEnum.RED), Ord(O.Color)); + finally + O.Free; + end; +end; + procedure TMVCTestSerializerJSON.TestSerializeNill; begin CheckEquals('null', FSerializer.SerializeObject(nil));