Add custom type serializer to manage json deserializer from database (#420)

This commit is contained in:
syntaxonline-rewhitley 2020-10-23 09:23:32 -04:00 committed by GitHub
parent cfcbc3d762
commit d0d6e26a43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View file

@ -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;
}
}
}

View file

@ -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();
}