From d85407fa72b3b8dc6a48db8346af636ad3bd11aa Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 11 Dec 2018 23:22:56 +0100 Subject: [PATCH] Json.Serializer option to serialize enumerables as ordinal or enumName TJsonSerializer.UseEnumNames --- Quick.Json.Serializer.pas | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/Quick.Json.Serializer.pas b/Quick.Json.Serializer.pas index ff5751a..06eb393 100644 --- a/Quick.Json.Serializer.pas +++ b/Quick.Json.Serializer.pas @@ -97,6 +97,7 @@ TCustomNameProperty = class(TCustomAttribute) TRTTIJson = class private fSerializeLevel : TSerializeLevel; + fUseEnumNames : Boolean; function GetValue(aAddr: Pointer; aType: TRTTIType): TValue; overload; function GetValue(aAddr: Pointer; aTypeInfo: PTypeInfo): TValue; overload; function IsAllowedProperty(aObject : TObject; const aPropertyName : string) : Boolean; @@ -111,6 +112,7 @@ TRTTIJson = class {$ENDIF} public constructor Create(aSerializeLevel : TSerializeLevel); + property UseEnumNames : Boolean read fUseEnumNames write fUseEnumNames; {$IFNDEF FPC} function DeserializeDynArray(aTypeInfo : PTypeInfo; aObject : TObject; const aJsonArray: TJSONArray) : TValue; function DeserializeRecord(aRecord : TValue; aObject : TObject; const aJson : TJSONObject) : TValue; @@ -140,11 +142,15 @@ TRTTIJson = class TJsonSerializer = class(TInterfacedObject,IJsonSerializer) strict private fSerializeLevel : TSerializeLevel; + fUseEnumNames : Boolean; fRTTIJson : TRTTIJson; + private + procedure SetUseEnumNames(const Value: Boolean); public constructor Create(aSerializeLevel : TSerializeLevel); destructor Destroy; override; property SerializeLevel : TSerializeLevel read fSerializeLevel; + property UseEnumNames : Boolean read fUseEnumNames write SetUseEnumNames; function JsonToObject(aType : TClass; const aJson: string) : TObject; overload; function JsonToObject(aObject : TObject; const aJson: string) : TObject; overload; function ObjectToJson(aObject : TObject; aIndent : Boolean = False): string; @@ -359,6 +365,7 @@ function TRTTIJson.DeserializeRecord(aRecord : TValue; aObject : TObject; const constructor TRTTIJson.Create(aSerializeLevel: TSerializeLevel); begin fSerializeLevel := aSerializeLevel; + fUseEnumNames := True; end; function TRTTIJson.DeserializeClass(aType: TClass; const aJson: TJSONObject): TObject; @@ -653,7 +660,8 @@ function TRTTIJson.DeserializeType(aObject : TObject; aType : TTypeKind; aTypeIn end else begin - TValue.Make(GetEnumValue(aTypeInfo,value),aTypeInfo, Result); + if fUseEnumNames then TValue.Make(GetEnumValue(aTypeInfo,value),aTypeInfo, Result) + else TValue.Make(StrToInt(value),aTypeInfo, Result); end; end; tkSet : @@ -797,9 +805,17 @@ function TRTTIJson.GetPropertyValue(Instance : TObject; const PropertyName : str {$ENDIF} tkLString : Result := GetStrProp(Instance,pinfo); {$IFDEF FPC} - tkEnumeration : Result := GetEnumName(pinfo.PropType,GetOrdProp(Instance,PropertyName)); + tkEnumeration : + begin + if fUseEnumNames then Result := GetEnumName(pinfo.PropType,GetOrdProp(Instance,PropertyName)) + else Result := GetOrdProp(Instance,PropertyName); + end; {$ELSE} - tkEnumeration : Result := GetEnumName(@pinfo.PropType,GetOrdProp(Instance,PropertyName)); + tkEnumeration : + begin + if fUseEnumNames then Result := GetEnumName(@pinfo.PropType,GetOrdProp(Instance,PropertyName)) + else Result := GetOrdProp(Instance,PropertyName); + end; {$ENDIF} tkSet : Result := GetSetProp(Instance,pinfo,True); {$IFNDEF FPC} @@ -1074,7 +1090,8 @@ function TRTTIJson.Serialize(const aName : string; aValue : TValue) : TJSONPair; else begin //Result.JsonValue := TJSONString.Create(GetEnumName(aValue.TypeInfo,aValue.AsOrdinal)); - Result.JsonValue := TJSONString.Create(aValue.ToString); + if fUseEnumNames then Result.JsonValue := TJSONString.Create(aValue.ToString) + else Result.JsonValue := TJSONNumber.Create(GetEnumValue(aValue.TypeInfo,aValue.ToString)); end; end; tkSet : @@ -1248,7 +1265,8 @@ function TRTTIJson.Serialize(aObject : TObject; aType : TTypeKind; const aProper end else begin - Result.JsonValue := TJSONString.Create(GetEnumName(propinfo.PropType,GetOrdProp(aObject,aPropertyName))); + if fUseEnumNames then Result.JsonValue := TJSONString.Create(GetEnumName(propinfo.PropType,GetOrdProp(aObject,aPropertyName))) + else Result.JsonValue := TJSONNumber.Create(GetOrdProp(aObject,aPropertyName)); //Result.JsonValue := TJSONString.Create(aValue.ToString); end; end; @@ -1304,7 +1322,9 @@ constructor TJsonSerializer.Create(aSerializeLevel: TSerializeLevel); if aSerializeLevel = TSerializeLevel.slPublicProperty then raise EJsonSerializeError.Create('FreePascal RTTI only supports published properties'); {$ENDIF} fSerializeLevel := aSerializeLevel; + fUseEnumNames := True; fRTTIJson := TRTTIJson.Create(aSerializeLevel); + fRTTIJson.UseEnumNames := fUseEnumNames; end; function TJsonSerializer.JsonToObject(aType: TClass; const aJson: string): TObject; @@ -1350,6 +1370,12 @@ function TJsonSerializer.ObjectToJson(aObject : TObject; aIndent : Boolean = Fal end; end; +procedure TJsonSerializer.SetUseEnumNames(const Value: Boolean); +begin + fUseEnumNames := Value; + if Assigned(fRTTIJson) then fRTTIJson.UseEnumNames := Value; +end; + {$IFNDEF FPC} { TCommentProperty }