Update serializer settings to allow for objects with reference loops
This commit is contained in:
parent
173107de12
commit
f4fff18698
|
|
@ -8,10 +8,8 @@ namespace Elsa
|
|||
{
|
||||
public static class JObjectExtensions
|
||||
{
|
||||
private static readonly JsonSerializer Serializer = new JsonSerializer().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||
public static T? GetState<T>(this JObject? state, string key) => state.GetState<T>(key, () => default!);
|
||||
|
||||
public static T? GetState<T>(this JObject? state, string key) => state.GetState<T>(key, () => default!);
|
||||
|
||||
public static T GetState<T>(this JObject? state, string key, Func<T> defaultValue)
|
||||
{
|
||||
var item = state?.GetValue(key, StringComparison.OrdinalIgnoreCase);
|
||||
|
|
@ -19,7 +17,7 @@ namespace Elsa
|
|||
if (item == null || item.Type == JTokenType.Null)
|
||||
return defaultValue();
|
||||
|
||||
return item.ToObject<T>(Serializer)!;
|
||||
return item.ToObject<T>(CreateSerializer())!;
|
||||
}
|
||||
|
||||
public static T? GetState<T>(this JObject? state, Type type, string key) => state.GetState<T>(type, key, () => default!);
|
||||
|
|
@ -27,11 +25,25 @@ namespace Elsa
|
|||
public static T GetState<T>(this JObject? state, Type type, string key, Func<T> defaultValue)
|
||||
{
|
||||
var item = state?.GetValue(key, StringComparison.OrdinalIgnoreCase);
|
||||
return item != null ? (T) item.ToObject(type, Serializer)! : defaultValue();
|
||||
return item != null ? (T) item.ToObject(type, CreateSerializer())! : defaultValue();
|
||||
}
|
||||
|
||||
public static void SetState(this JObject state, string key, object? value) => state[key] = value != null ? JToken.FromObject(value, Serializer) : null;
|
||||
|
||||
public static void SetState(this JObject state, string key, object? value) => state[key] = value != null ? JToken.FromObject(value, CreateSerializer()) : null;
|
||||
|
||||
public static bool HasKey(this JObject state, string key) => state.ContainsKey(key);
|
||||
|
||||
private static JsonSerializer CreateSerializer()
|
||||
{
|
||||
var serializer = new JsonSerializer();
|
||||
|
||||
serializer.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||
serializer.NullValueHandling = NullValueHandling.Ignore;
|
||||
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
|
||||
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
|
||||
serializer.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple;
|
||||
serializer.TypeNameHandling = TypeNameHandling.Auto;
|
||||
|
||||
return serializer;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue