Stuff and things
This commit is contained in:
27
PartSource.Data/Converters/ObjectToJsonConverter.cs
Normal file
27
PartSource.Data/Converters/ObjectToJsonConverter.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace PartSource.Data.Converters
|
||||
{
|
||||
public class ObjectToJsonConverter : ValueConverter<object, string>
|
||||
{
|
||||
private static readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings()
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
ContractResolver = new DefaultContractResolver()
|
||||
{
|
||||
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||
}
|
||||
};
|
||||
|
||||
public ObjectToJsonConverter() : base(ObjectToJson, JsonToObject) { }
|
||||
|
||||
public static Expression<Func<string, object>> JsonToObject = value => JsonConvert.DeserializeObject(value, _serializerSettings);
|
||||
public static Expression<Func<object, string>> ObjectToJson = value => JsonConvert.SerializeObject(value, _serializerSettings);
|
||||
}
|
||||
}
|
||||
16
PartSource.Data/Converters/TypeToStringConverter.cs
Normal file
16
PartSource.Data/Converters/TypeToStringConverter.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace PartSource.Data.Converters
|
||||
{
|
||||
public class TypeToStringConverter : ValueConverter<Type, string>
|
||||
{
|
||||
public TypeToStringConverter() : base(TypeToString, StringToType) { }
|
||||
|
||||
public static Expression<Func<string, Type>> StringToType = value => Type.GetType(value);
|
||||
public static Expression<Func<Type, string>> TypeToString = value => value.AssemblyQualifiedName;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user