Skip to content

Commit

Permalink
split CustomContractResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Nov 10, 2024
1 parent 231191e commit 2e83808
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 111 deletions.
112 changes: 1 addition & 111 deletions src/Verify/Serialization/CustomContractResolver.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
class CustomContractResolver(SerializationSettings settings) :
partial class CustomContractResolver(SerializationSettings settings) :
DefaultContractResolver
{
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
var contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = (_, name, original) => ResolveDictionaryKey(contract, name, original);
if (settings.SortDictionaries)
{
contract.OrderByKey = true;
}

contract.InterceptSerializeItem = (_, key, value) =>
{
if (key is string stringKey &&
settings.TryGetScrubOrIgnoreByName(stringKey, out var scrubOrIgnore))
{
return ToInterceptKeyValueResult(scrubOrIgnore.Value);
}
if (value is not null &&
settings.TryGetScrubOrIgnoreByInstance(value, out scrubOrIgnore))
{
return ToInterceptKeyValueResult(scrubOrIgnore.Value);
}
return KeyValueInterceptResult.Default;
};

return contract;
}

static KeyValueInterceptResult ToInterceptKeyValueResult(ScrubOrIgnore scrubOrIgnore)
{
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
{
return KeyValueInterceptResult.Ignore;
}

return KeyValueInterceptResult.ReplaceValue("{Scrubbed}");
}

static ItemInterceptResult ToInterceptItemResult(ScrubOrIgnore scrubOrIgnore)
{
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
Expand All @@ -50,77 +11,6 @@ static ItemInterceptResult ToInterceptItemResult(ScrubOrIgnore scrubOrIgnore)
return ItemInterceptResult.Replace("{Scrubbed}");
}

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

#if NET6_0_OR_GREATER

if (original is Date date)
{
if (settings.TryConvert(counter, date, out var result))
{
return result;
}
}

if (original is Time time)
{
if (settings.TryConvert(counter, time, out var result))
{
return result;
}
}

#endif

if (original is Guid guid)
{
if (settings.TryConvert(counter, guid, out var result))
{
return result;
}
}

if (original is string stringValue)
{
if (settings.TryParseConvert(counter, stringValue.AsSpan(), out var result))
{
return result;
}
}

if (original is DateTime dateTime)
{
if (settings.TryConvert(counter, dateTime, out var result))
{
return result;
}
}

if (original is DateTimeOffset dateTimeOffset)
{
if (settings.TryConvert(counter, dateTimeOffset, out var result))
{
return result;
}
}

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

return type.SimpleName();
}

return name;
}

static FieldInfo exceptionMessageField = typeof(Exception).GetField("_message", BindingFlags.Instance | BindingFlags.NonPublic)!;

protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
Expand Down
114 changes: 114 additions & 0 deletions src/Verify/Serialization/CustomContractResolver_Dictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
partial class CustomContractResolver
{
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
var contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = (_, name, original) => ResolveDictionaryKey(contract, name, original);
if (settings.SortDictionaries)
{
contract.OrderByKey = true;
}

contract.InterceptSerializeItem = HandleDictionaryItem;

return contract;
}

KeyValueInterceptResult HandleDictionaryItem(JsonWriter writer, object key, object? value)
{
if (key is string stringKey &&
settings.TryGetScrubOrIgnoreByName(stringKey, out var scrubOrIgnore))
{
return ToInterceptKeyValueResult(scrubOrIgnore.Value);
}

if (value is not null &&
settings.TryGetScrubOrIgnoreByInstance(value, out scrubOrIgnore))
{
return ToInterceptKeyValueResult(scrubOrIgnore.Value);
}

return KeyValueInterceptResult.Default;
}

static KeyValueInterceptResult ToInterceptKeyValueResult(ScrubOrIgnore scrubOrIgnore)
{
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
{
return KeyValueInterceptResult.Ignore;
}

return KeyValueInterceptResult.ReplaceValue("{Scrubbed}");
}

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

#if NET6_0_OR_GREATER

if (original is Date date)
{
if (settings.TryConvert(counter, date, out var result))
{
return result;
}
}

if (original is Time time)
{
if (settings.TryConvert(counter, time, out var result))
{
return result;
}
}

#endif

if (original is Guid guid)
{
if (settings.TryConvert(counter, guid, out var result))
{
return result;
}
}

if (original is string stringValue)
{
if (settings.TryParseConvert(counter, stringValue.AsSpan(), out var result))
{
return result;
}
}

if (original is DateTime dateTime)
{
if (settings.TryConvert(counter, dateTime, out var result))
{
return result;
}
}

if (original is DateTimeOffset dateTimeOffset)
{
if (settings.TryConvert(counter, dateTimeOffset, out var result))
{
return result;
}
}

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

return type.SimpleName();
}

return name;
}
}

0 comments on commit 2e83808

Please sign in to comment.