Exception handling when receiving value that does not exist in the mapped values.

Search for mapped values case insensitive
This commit is contained in:
João Antônio Duarte 2020-03-13 11:58:04 -03:00
parent 782bf2c4e0
commit cd162c5977
2 changed files with 11 additions and 2 deletions

View File

@ -314,7 +314,8 @@ uses
MVCFramework.Serializer.JsonDataObjects, MVCFramework.Serializer.JsonDataObjects,
MVCFramework.Serializer.Intf, MVCFramework.Serializer.Intf,
Data.FmtBcd, Data.FmtBcd,
MVCFramework.Nullables; MVCFramework.Nullables,
System.Generics.Defaults;
function NewObjectHolder(const AObject: TObject; const AMetaFiller: TProc<TMVCStringDictionary> = nil; function NewObjectHolder(const AObject: TObject; const AMetaFiller: TProc<TMVCStringDictionary> = nil;
const AOwns: boolean = false): TMVCObjectResponse; const AOwns: boolean = false): TMVCObjectResponse;
@ -732,7 +733,12 @@ end;
constructor MVCEnumSerializationAttribute.Create(const ASerializationType: TMVCEnumSerializationType; constructor MVCEnumSerializationAttribute.Create(const ASerializationType: TMVCEnumSerializationType;
const AMappedValues: string); const AMappedValues: string);
begin begin
FMappedValues := TList<string>.Create; FMappedValues := TList<string>.Create(TDelegatedComparer<string>.Create(
function(const Left, Right: string): Integer
begin
Result := CompareText(Left, Right);
end));
FSerializationType := ASerializationType; FSerializationType := ASerializationType;
if (FSerializationType = estEnumMappedValues) then if (FSerializationType = estEnumMappedValues) then

View File

@ -936,6 +936,9 @@ begin
else else
begin begin
LMappedValueIndex := LEnumMappedValues.IndexOf(AJsonObject[AName].Value); LMappedValueIndex := LEnumMappedValues.IndexOf(AJsonObject[AName].Value);
if LMappedValueIndex < 0 then
raise EMVCSerializationException.CreateFmt('Cannot deserialize property %s from mapped values', [AName]);
TValue.Make(GetEnumValue(AValue.TypeInfo, GetEnumName(AValue.TypeInfo, LMappedValueIndex)), TValue.Make(GetEnumValue(AValue.TypeInfo, GetEnumName(AValue.TypeInfo, LMappedValueIndex)),
AValue.TypeInfo, AValue) AValue.TypeInfo, AValue)
end; end;