KeyValuePairConverter
Converts a KeyValuePair<T, U> to and from JSON.
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Newtonsoft.Json.Converters
{
public class KeyValuePairConverter : JsonConverter
{
private const string KeyName = "Key";
private const string ValueName = "Value";
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Type type = value.GetType();
PropertyInfo property = type.GetProperty("Key");
PropertyInfo property2 = type.GetProperty("Value");
DefaultContractResolver defaultContractResolver = serializer.ContractResolver as DefaultContractResolver;
writer.WriteStartObject();
writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Key") : "Key");
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(property, value));
writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Value") : "Value");
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(property2, value));
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
bool flag = ReflectionUtils.IsNullableType(objectType);
if (reader.TokenType == JsonToken.Null) {
if (!flag)
throw JsonSerializationException.Create(reader, "Cannot convert null value to KeyValuePair.");
return null;
}
Type type = flag ? Nullable.GetUnderlyingType(objectType) : objectType;
IList<Type> genericArguments = type.GetGenericArguments();
Type objectType2 = genericArguments[0];
Type objectType3 = genericArguments[1];
object obj = null;
object obj2 = null;
reader.Read();
while (reader.TokenType == JsonToken.PropertyName) {
string a = reader.Value.ToString();
if (string.Equals(a, "Key", StringComparison.OrdinalIgnoreCase)) {
reader.Read();
obj = serializer.Deserialize(reader, objectType2);
} else if (string.Equals(a, "Value", StringComparison.OrdinalIgnoreCase)) {
reader.Read();
obj2 = serializer.Deserialize(reader, objectType3);
} else {
reader.Skip();
}
reader.Read();
}
return Activator.CreateInstance(type, obj, obj2);
}
public override bool CanConvert(Type objectType)
{
Type type = ReflectionUtils.IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType;
if (type.IsValueType() && type.IsGenericType())
return (object)type.GetGenericTypeDefinition() == typeof(KeyValuePair<, >);
return false;
}
}
}