mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-24 16:26:25 +00:00
55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace CryptoExchange.Net.Converters
|
|
{
|
|
public abstract class BaseConverter<T>: JsonConverter
|
|
{
|
|
protected abstract Dictionary<T, string> Mapping { get; }
|
|
private readonly bool quotes;
|
|
|
|
protected BaseConverter(bool useQuotes)
|
|
{
|
|
quotes = useQuotes;
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
if (quotes)
|
|
writer.WriteValue(Mapping[(T)value]);
|
|
else
|
|
writer.WriteRawValue(Mapping[(T)value]);
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
if (reader.Value == null)
|
|
return null;
|
|
|
|
var value = reader.Value.ToString();
|
|
if (Mapping.ContainsValue(value))
|
|
return Mapping.Single(m => m.Value == value).Key;
|
|
|
|
var lowerResult = Mapping.SingleOrDefault(m => m.Value.ToLower() == value.ToLower());
|
|
if (!lowerResult.Equals(default(KeyValuePair<T, string>)))
|
|
return lowerResult.Value;
|
|
|
|
Debug.WriteLine($"Cannot map enum. Type: {typeof(T)}, Value: {value}");
|
|
return null;
|
|
}
|
|
|
|
public T ReadString(string data)
|
|
{
|
|
return Mapping.Single(v => v.Value == data).Key;
|
|
}
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return objectType == typeof(T);
|
|
}
|
|
}
|
|
}
|