Knocks/BackEnd/Knoks.Core/UidConverter.cs

43 lines
1.2 KiB
C#

using Knoks.Core.Entities;
using Knoks.Core.Logic.Interfaces;
using Knoks.Core.Logic.Managers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace Knoks.Core
{
public class UidConverter: JsonConverter
{
private string key;
private Type type;
public UidConverter() {
key = "hjtSome random value gjoej";
}
public UidConverter(string Key, Type Type)
{
key = Key;
this.type = Type;
}
public override bool CanRead => true;
public override bool CanWrite => true;
public override bool CanConvert(Type type) => type == typeof(string);
public override void WriteJson(
JsonWriter writer, object value, JsonSerializer serializer)
{
int number = Int32.Parse((string)value);
writer.WriteValue(new UidManager().ToIntUid(number, type, key));
}
public override object ReadJson(
JsonReader reader, Type type, object existingValue, JsonSerializer serializer)
{
var value = reader.ReadAsString();
return new UidManager().ToIntValue(value, type, key).ToString();
}
}
}