Skip to content

Commit

Permalink
Json.Serializer option to serialize enumerables as ordinal or enumName
Browse files Browse the repository at this point in the history
TJsonSerializer.UseEnumNames
  • Loading branch information
exilon committed Dec 11, 2018
1 parent 16f828a commit d85407f
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions Quick.Json.Serializer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 :
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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 :
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 }

Expand Down

0 comments on commit d85407f

Please sign in to comment.