Skip to content

Commit

Permalink
avoid load type in ResolveDictionaryKey
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Nov 10, 2024
1 parent 2e83808 commit e0defd9
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/Verify/Serialization/CustomContractResolver_Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
var contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = (_, name, original) => ResolveDictionaryKey(contract, name, original);
contract.DictionaryKeyResolver = (_, name, original) => ResolveDictionaryKey(name, original);
if (settings.SortDictionaries)
{
contract.OrderByKey = true;
Expand Down Expand Up @@ -41,10 +41,9 @@ static KeyValueInterceptResult ToInterceptKeyValueResult(ScrubOrIgnore scrubOrIg
return KeyValueInterceptResult.ReplaceValue("{Scrubbed}");
}

string ResolveDictionaryKey(JsonDictionaryContract contract, string name, object original)
string ResolveDictionaryKey(string name, object original)
{
var counter = Counter.Current;
var keyType = contract.DictionaryKeyType;

#if NET6_0_OR_GREATER

Expand Down Expand Up @@ -98,14 +97,8 @@ string ResolveDictionaryKey(JsonDictionaryContract contract, string name, object
}
}

if (keyType == typeof(Type))
if (original is Type type)
{
var type = Type.GetType(name);
if (type is null)
{
throw new($"Could not load type `{name}`.");
}

return type.SimpleName();
}

Expand Down

0 comments on commit e0defd9

Please sign in to comment.