Serialize converters were added

This commit is contained in:
Evgeny Redikultsev
2024-09-08 17:47:46 +05:00
parent 408e9f6999
commit 6e0b7b8070
60 changed files with 1713 additions and 443 deletions

View File

@@ -1,95 +0,0 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StructureHelperCommon.Models;
using System;
using System.Reflection;
namespace DataAccess.JsonConverters
{
public abstract class BaseConverter<T> : JsonConverter<T>
{
private IShiftTraceLogger traceLogger;
protected BaseConverter(IShiftTraceLogger logger)
{
traceLogger = logger;
}
public override void WriteJson(JsonWriter writer, T value, JsonSerializer serializer)
{
traceLogger.AddMessage($"Serializing {typeof(T).Name} (ID: {GetId(value)})");
// Use JsonSerializer's default behavior to handle attributes like [JsonIgnore] and [JsonProperty]
var jo = new JObject();
foreach (var prop in typeof(T).GetProperties())
{
if (!ShouldIgnoreProperty(prop))
{
string propertyName = GetPropertyName(prop);
var propValue = prop.GetValue(value);
jo.Add(propertyName, JToken.FromObject(propValue, serializer));
}
}
jo.WriteTo(writer);
}
// Helper method to check if a property should be ignored
private bool ShouldIgnoreProperty(PropertyInfo prop)
{
// Check for [JsonIgnore] attribute
var jsonIgnoreAttribute = prop.GetCustomAttribute<JsonIgnoreAttribute>();
return jsonIgnoreAttribute != null;
}
// Helper method to get the property name, considering [JsonProperty] and [JsonPropertyName] attributes
private string GetPropertyName(PropertyInfo prop)
{
// Check for [JsonProperty] attribute (for Newtonsoft.Json)
var jsonPropertyAttribute = prop.GetCustomAttribute<JsonPropertyAttribute>();
if (jsonPropertyAttribute != null)
{
return jsonPropertyAttribute.PropertyName;
}
// Check for [JsonPropertyName] attribute (for System.Text.Json compatibility)
var jsonPropertyNameAttribute = prop.GetCustomAttribute<System.Text.Json.Serialization.JsonPropertyNameAttribute>();
if (jsonPropertyNameAttribute != null)
{
return jsonPropertyNameAttribute.Name;
}
// Default to the property name if no attributes are found
return prop.Name;
}
public override T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer)
{
traceLogger.AddMessage($"Deserializing {typeof(T).Name}");
// Use JsonSerializer's default behavior to handle attributes during deserialization
JObject jo = JObject.Load(reader);
T obj = Activator.CreateInstance<T>();
foreach (var prop in typeof(T).GetProperties())
{
if (!ShouldIgnoreProperty(prop) && jo.TryGetValue(GetPropertyName(prop), out JToken value))
{
var propValue = value.ToObject(prop.PropertyType, serializer);
prop.SetValue(obj, propValue);
}
}
traceLogger.AddMessage($"Deserialized {typeof(T).Name} (ID: {GetId(obj)})");
return obj;
}
// Method to get the ID for logging purposes, assumes all classes have an 'Id' property of type Guid.
private Guid GetId(object obj)
{
var idProp = obj.GetType().GetProperty("Id");
return idProp != null ? (Guid)idProp.GetValue(obj) : Guid.Empty;
}
}
}

View File

@@ -0,0 +1,48 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StructureHelperCommon.Models;
using System;
using System.Reflection;
namespace DataAccess.JsonConverters
{
public abstract class BaseJsonConverter<T> : JsonConverter<T>
{
private IWriteJsonLogic<T> writeJsonLogic;
private IReadJsonLogic<T> readJsonLogic;
public IShiftTraceLogger TraceLogger { get; set; }
protected BaseJsonConverter(IShiftTraceLogger logger, IWriteJsonLogic<T> writeJsonLogic, IReadJsonLogic<T> readJsonLogic)
{
this.writeJsonLogic = writeJsonLogic;
this.readJsonLogic = readJsonLogic;
TraceLogger = logger;
}
protected BaseJsonConverter(IShiftTraceLogger logger)
: this (logger,
new WriteJsonLogic<T>() { TraceLogger = logger},
new ReadJsonLogic<T>() { TraceLogger = logger})
{
}
public override void WriteJson(JsonWriter writer, T? value, JsonSerializer serializer)
{
writeJsonLogic.TraceLogger = TraceLogger;
writeJsonLogic.WriteJson(writer, value, serializer);
}
public override T ReadJson(JsonReader reader, Type objectType, T? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
readJsonLogic.TraceLogger = TraceLogger;
return readJsonLogic.ReadJson(reader, objectType, existingValue, hasExistingValue, serializer);
}
}
}

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace DataAccess.JsonConverters
{
public class CrossSectionJsonConverter : BaseConverter<CrossSectionDTO>
public class CrossSectionJsonConverter : BaseJsonConverter<CrossSectionDTO>
{
public CrossSectionJsonConverter(IShiftTraceLogger logger) : base(logger)
{

View File

@@ -0,0 +1,17 @@
using DataAccess.DTOs;
using StructureHelperCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.JsonConverters
{
public class FileVersionDTOJsonConverter : BaseJsonConverter<FileVersionDTO>
{
public FileVersionDTOJsonConverter(IShiftTraceLogger logger) : base(logger)
{
}
}
}

View File

@@ -0,0 +1,22 @@
using StructureHelperCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.JsonConverters
{
/// <inheritdoc/>
public class GetIdFromObjectLogic : IGetIdFromObjectLogic
{
/// <inheritdoc/>
public IShiftTraceLogger? TraceLogger { get; set; }
/// <inheritdoc/>
public Guid GetId(object obj)
{
var idProp = obj.GetType().GetProperty("Id");
return idProp != null ? (Guid)idProp.GetValue(obj) : Guid.Empty;
}
}
}

View File

@@ -0,0 +1,38 @@
using Newtonsoft.Json;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.JsonConverters
{
public class GetPropertyNameLogic : IGetPropertyNameLogic
{
public IShiftTraceLogger? TraceLogger { get; set; }
// Helper method to get the property name, considering [JsonProperty] and [JsonPropertyName] attributes
public string GetPropertyName(PropertyInfo prop)
{
// Check for [JsonProperty] attribute (for Newtonsoft.Json)
var jsonPropertyAttribute = prop.GetCustomAttribute<JsonPropertyAttribute>();
if (jsonPropertyAttribute != null)
{
return jsonPropertyAttribute.PropertyName;
}
// Check for [JsonPropertyName] attribute (for System.Text.Json compatibility)
var jsonPropertyNameAttribute = prop.GetCustomAttribute<System.Text.Json.Serialization.JsonPropertyNameAttribute>();
if (jsonPropertyNameAttribute != null)
{
return jsonPropertyNameAttribute.Name;
}
// Default to the property name if no attributes are found
return prop.Name;
}
}
}

View File

@@ -0,0 +1,12 @@
using StructureHelperCommon.Infrastructures.Interfaces;
namespace DataAccess.JsonConverters
{
/// <summary>
/// Logic to get the ID for logging purposes, assumes all classes have an 'Id' property of type Guid.
/// </summary>
public interface IGetIdFromObjectLogic : ILogic
{
Guid GetId(object obj);
}
}

View File

@@ -0,0 +1,13 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using System.Reflection;
namespace DataAccess.JsonConverters
{
/// <summary>
/// Helper logic to get the property name, considering [JsonProperty] and [JsonPropertyName] attributes
/// </summary>
public interface IGetPropertyNameLogic : ILogic
{
string GetPropertyName(PropertyInfo prop);
}
}

View File

@@ -0,0 +1,23 @@
using Newtonsoft.Json;
using StructureHelperCommon.Infrastructures.Interfaces;
namespace DataAccess.JsonConverters
{
/// <summary>
/// Helper logic for JSON converter
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IReadJsonLogic<T> : ILogic
{
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="hasExistingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer);
}
}

View File

@@ -0,0 +1,13 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using System.Reflection;
namespace DataAccess.JsonConverters
{
/// <summary>
/// Helper logic to check if a property should be ignored
/// </summary>
public interface IShouldIgnorePropertyLogic : ILogic
{
bool ShouldIgnoreProperty(PropertyInfo prop);
}
}

View File

@@ -0,0 +1,10 @@
using Newtonsoft.Json;
using StructureHelperCommon.Infrastructures.Interfaces;
namespace DataAccess.JsonConverters
{
public interface IWriteJsonLogic<T> : ILogic
{
void WriteJson(JsonWriter writer, T value, JsonSerializer serializer);
}
}

View File

@@ -0,0 +1,56 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using StructureHelperCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.JsonConverters
{
public class ReadJsonLogic<T> : IReadJsonLogic<T>
{
private IShouldIgnorePropertyLogic shouldIgnorePropertyLogic;
private IGetPropertyNameLogic getPropertyNameLogic;
private IGetIdFromObjectLogic getIdFromObjectLogic;
public IShiftTraceLogger? TraceLogger { get; set; }
public ReadJsonLogic(IShouldIgnorePropertyLogic shouldIgnorePropertyLogic,
IGetPropertyNameLogic getPropertyNameLogic,
IGetIdFromObjectLogic getIdFromObjectLogic)
{
this.shouldIgnorePropertyLogic = shouldIgnorePropertyLogic;
this.getPropertyNameLogic = getPropertyNameLogic;
this.getIdFromObjectLogic = getIdFromObjectLogic;
}
public ReadJsonLogic()
: this(new ShouldIgnorePropertyLogic(),
new GetPropertyNameLogic(),
new GetIdFromObjectLogic())
{
}
public T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer)
{
TraceLogger?.AddMessage($"Deserializing {typeof(T).Name}");
shouldIgnorePropertyLogic.TraceLogger = getPropertyNameLogic.TraceLogger = getIdFromObjectLogic.TraceLogger = TraceLogger;
// Use JsonSerializer's default behavior to handle attributes during deserialization
JObject jo = JObject.Load(reader);
T obj = Activator.CreateInstance<T>();
foreach (var prop in typeof(T).GetProperties())
{
if (! shouldIgnorePropertyLogic.ShouldIgnoreProperty(prop) && jo.TryGetValue(getPropertyNameLogic.GetPropertyName(prop), out JToken value))
{
var propValue = value.ToObject(prop.PropertyType, serializer);
prop.SetValue(obj, propValue);
}
}
TraceLogger?.AddMessage($"Deserialized {typeof(T).Name} (ID: {getIdFromObjectLogic.GetId(obj)})");
return obj;
}
}
}

View File

@@ -0,0 +1,22 @@
using Newtonsoft.Json;
using StructureHelperCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.JsonConverters
{
public class ShouldIgnorePropertyLogic : IShouldIgnorePropertyLogic
{
public IShiftTraceLogger? TraceLogger { get; set; }
public bool ShouldIgnoreProperty(PropertyInfo prop)
{
// Check for [JsonIgnore] attribute
var jsonIgnoreAttribute = prop.GetCustomAttribute<JsonIgnoreAttribute>();
return jsonIgnoreAttribute != null;
}
}
}

View File

@@ -0,0 +1,54 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.JsonConverters
{
public class WriteJsonLogic<T> : IWriteJsonLogic<T>
{
private IShouldIgnorePropertyLogic shouldIgnorePropertyLogic;
private IGetPropertyNameLogic getPropertyNameLogic;
private IGetIdFromObjectLogic getIdFromObjectLogic;
public IShiftTraceLogger? TraceLogger { get; set; }
public WriteJsonLogic(IShouldIgnorePropertyLogic shouldIgnorePropertyLogic,
IGetPropertyNameLogic getPropertyNameLogic,
IGetIdFromObjectLogic getIdFromObjectLogic)
{
this.shouldIgnorePropertyLogic = shouldIgnorePropertyLogic;
this.getPropertyNameLogic = getPropertyNameLogic;
this.getIdFromObjectLogic = getIdFromObjectLogic;
}
public WriteJsonLogic()
: this(new ShouldIgnorePropertyLogic(),
new GetPropertyNameLogic(),
new GetIdFromObjectLogic())
{
}
public void WriteJson(JsonWriter writer, T value, JsonSerializer serializer)
{
TraceLogger?.AddMessage($"Serializing {typeof(T).Name} (ID: {getIdFromObjectLogic.GetId(value)})");
shouldIgnorePropertyLogic.TraceLogger = getPropertyNameLogic.TraceLogger = getIdFromObjectLogic.TraceLogger = TraceLogger;
// Use JsonSerializer's default behavior to handle attributes like [JsonIgnore] and [JsonProperty]
var jo = new JObject();
foreach (var prop in typeof(T).GetProperties())
{
if (!shouldIgnorePropertyLogic.ShouldIgnoreProperty(prop))
{
string propertyName = getPropertyNameLogic.GetPropertyName(prop);
var propValue = prop.GetValue(value);
jo.Add(propertyName, JToken.FromObject(propValue, serializer));
}
}
jo.WriteTo(writer);
}
}
}