-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashSetConverter.cs
61 lines (48 loc) · 1.88 KB
/
HashSetConverter.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
50
51
52
53
54
55
56
57
58
59
60
61
using Bayat.Core.Reflection;
using Bayat.Json.Linq;
using Bayat.Json.Serialization;
using System;
using System.Collections.Generic;
namespace Bayat.Json.Converters
{
public class HashSetConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializerWriter internalWriter)
{
//throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializerReader internalReader)
{
var shouldReplace = internalReader.Serializer.ObjectCreationHandling == ObjectCreationHandling.Replace;
//Return the existing value (or null if it doesn't exist)
if (reader.TokenType == JsonToken.Null)
return shouldReplace ? null : existingValue;
//Dynamically create the HashSet
var result = !shouldReplace && existingValue != null
? existingValue
: Activator.CreateInstance(objectType);
var genericType = objectType.GetGenericArguments()[0];
var addMethod = objectType.GetMethod("Add");
var jo = JArray.Load(reader);
for (var i = 0; i < jo.Count; i++)
{
var itemValue = internalReader.Serializer.Deserialize(jo[i].CreateReader(), genericType);
addMethod.Invoke(result, new[] { itemValue });
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public override bool CanConvert(Type objectType)
{
return objectType.IsGenericType() && objectType.GetGenericTypeDefinition() == typeof(HashSet<>);
}
public override bool CanWrite
{
get { return false; }
}
}
}