mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-07 16:06:15 +00:00
45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace CryptoExchange.Net
|
|
{
|
|
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)
|
|
{
|
|
var val = Mapping.Single(v => v.Value == reader.Value.ToString()).Key;
|
|
if (val != null)
|
|
return val;
|
|
return Mapping.Single(v => v.Value.ToLower() == reader.Value.ToString().ToLower());
|
|
}
|
|
|
|
public T ReadString(string data)
|
|
{
|
|
return Mapping.Single(v => v.Value == data).Key;
|
|
}
|
|
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return objectType == typeof(T);
|
|
}
|
|
}
|
|
}
|