elsa-core/src/core/Elsa.Abstractions/Extensions/StateExtensions.cs
Sipke Schoorstra c7c41d10a5
Workflow Registry refresh upon workflow definition updates (#101)
* Fix many things. Few more samples left to fix

* Fix all the samples

* Introduce service configurator

* Implement cache signaling and refreshing workflow registry

* Add Sample16 demonstrating dashboard + workflow host combined

* Bugfix where non-published versions of worklows are triggered
2019-10-23 21:23:25 +02:00

36 lines
1.3 KiB
C#

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NodaTime;
using NodaTime.Serialization.JsonNet;
namespace Elsa.Extensions
{
public static class StateExtensions
{
private static readonly JsonSerializer Serializer = new JsonSerializer().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
public static T GetState<T>(this JObject state, string key, Func<T> defaultValue = null)
{
var item = state.GetValue(key, StringComparison.OrdinalIgnoreCase);
if (item == null || item.Type == JTokenType.Null)
return defaultValue != null ? defaultValue() : default;
return item.ToObject<T>(Serializer);
}
public static T GetState<T>(this JObject state, Type type, string key, Func<T> defaultValue = null)
{
var item = state.GetValue(key, StringComparison.OrdinalIgnoreCase);
return item != null ? (T) item.ToObject(type, Serializer) : defaultValue != null ? defaultValue() : default;
}
public static void SetState(this JObject state, string key, object value)
{
state[key] = value != null
? JToken.FromObject(value, Serializer)
: null;
}
}
}