-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDeserializationContractResolver.cs
49 lines (42 loc) · 1.69 KB
/
DeserializationContractResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using CryptoShredding.Serialization.JsonConverters;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace CryptoShredding.Serialization.ContractResolvers;
public class DeserializationContractResolver
: DefaultContractResolver
{
private readonly ICryptoTransform _decryptor;
private readonly FieldEncryptionDecryption _fieldEncryptionDecryption;
public DeserializationContractResolver(
ICryptoTransform decryptor,
FieldEncryptionDecryption fieldEncryptionDecryption)
{
_decryptor = decryptor;
_fieldEncryptionDecryption = fieldEncryptionDecryption;
NamingStrategy = new CamelCaseNamingStrategy();
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
foreach (var jsonProperty in properties)
{
var isSimpleValue = IsSimpleValue(type, jsonProperty);
if (!isSimpleValue) continue;
var jsonConverter = new DecryptionJsonConverter(_decryptor, _fieldEncryptionDecryption);
jsonProperty.Converter = jsonConverter;
}
return properties;
}
private bool IsSimpleValue(Type type, JsonProperty jsonProperty)
{
var propertyInfo = type.GetProperty(jsonProperty.UnderlyingName);
if (propertyInfo is null)
return false;
var propertyType = propertyInfo.PropertyType;
var isSimpleValue = propertyType.IsValueType || propertyType == typeof(string);
return isSimpleValue;
}
}