From 8adddd8e9381c803d69dc87933cfc12d90996b18 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 24 Jan 2019 23:52:01 +0100 Subject: [PATCH] Quick.Json.Serializer improved fpc serialization --- Quick.Json.Serializer.pas | 58 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/Quick.Json.Serializer.pas b/Quick.Json.Serializer.pas index 06eb393..e80d746 100644 --- a/Quick.Json.Serializer.pas +++ b/Quick.Json.Serializer.pas @@ -1154,10 +1154,62 @@ function TRTTIJson.Serialize(const aName : string; aValue : TValue) : TJSONPair; //Result.JsonString := TJSONString(aName); try case avalue.Kind of - tkInteger, tkInt64 : + tkClass : + begin + Result.JsonValue := TJSONValue(Serialize(aValue.AsObject)); + end; + tkString, tkLString, tkWString, tkUString : + begin + Result.JsonValue := TJSONString.Create(aValue.AsString); + end; + tkChar, tkWChar : + begin + Result.JsonValue := TJSONString.Create(aValue.AsString); + end; + tkInteger : + begin + Result.JsonValue := TJSONNumber.Create(aValue.AsInteger); + end; + tkInt64 : begin Result.JsonValue := TJSONNumber.Create(aValue.AsInt64); end; + tkFloat : + begin + if aValue.TypeInfo = TypeInfo(TDateTime) then + begin + Result.JsonValue := TJSONString.Create(DateTimeToJsonDate(aValue.AsExtended)); + end + else if aValue.TypeInfo = TypeInfo(TDate) then + begin + Result.JsonValue := TJSONString.Create(DateToStr(aValue.AsExtended)); + end + else if aValue.TypeInfo = TypeInfo(TTime) then + begin + Result.JsonValue := TJSONString.Create(TimeToStr(aValue.AsExtended)); + end + else + begin + Result.JsonValue := TJSONNumber.Create(aValue.AsExtended); + end; + end; + tkEnumeration : + begin + if (aValue.TypeInfo = System.TypeInfo(Boolean)) then + begin + Result.JsonValue := TJSONBool.Create(aValue.AsBoolean); + end + else + begin + //Result.JsonValue := TJSONString.Create(GetEnumName(aValue.TypeInfo,aValue.AsOrdinal)); + if fUseEnumNames then Result.JsonValue := TJSONString.Create(aValue.ToString) + else Result.JsonValue := TJSONNumber.Create(GetEnumValue(aValue.TypeInfo,aValue.ToString)); + end; + end; + tkSet : + begin + Result.JsonValue := TJSONString.Create(aValue.ToString); + end; else begin //raise EJsonDeserializeError.CreateFmt('Not supported type "%s":%d',[aName,Integer(aValue.Kind)]); @@ -1211,10 +1263,10 @@ function TRTTIJson.Serialize(aObject : TObject; aType : TTypeKind; const aProper end; end; end; - Result.JsonValue := jArray; finally - DynArrayClear(pArr,propinfo.PropType); + //DynArrayClear(pArr,propinfo.PropType); + pArr := nil; end; end; tkClass :