Add custom type serializer to manage json deserializer from database (#420)
This commit is contained in:
parent
cfcbc3d762
commit
d0d6e26a43
|
|
@ -0,0 +1,28 @@
|
|||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Elsa.Serialization.Converters
|
||||
{
|
||||
public class ExceptionConverter : JsonConverter<Exception>
|
||||
{
|
||||
|
||||
public override void WriteJson(JsonWriter writer, Exception value, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
|
||||
}
|
||||
public override bool CanWrite => false;
|
||||
public override bool CanRead => true;
|
||||
|
||||
public override Exception ReadJson(JsonReader reader, Type objectType, Exception existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
//reader will throw exception if not read to end
|
||||
var jObject = JObject.Load(reader);
|
||||
var ex = System.Text.Json.JsonSerializer.Deserialize<Exception>(jObject.ToString());
|
||||
return ex;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ using System.Collections.Generic;
|
|||
using Elsa.Models;
|
||||
using Elsa.Persistence.EntityFrameworkCore.CustomSchema;
|
||||
using Elsa.Persistence.EntityFrameworkCore.Entities;
|
||||
using Elsa.Serialization.Converters;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
|
@ -29,6 +30,8 @@ namespace Elsa.Persistence.EntityFrameworkCore.DbContexts
|
|||
protected ElsaContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
serializerSettings = new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||
serializerSettings.Converters.Add(new ExceptionConverter());
|
||||
|
||||
DbContextCustomSchema = options.GetDbContextCustomSchema();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue