Fixed yaml serialisation and updated to YamlDotNet (#236)
* Fixed Yaml serialisation/deserialisation Copied json serializer settings from JsonTokenFormatter, apart from CamelCasePropertyNamesContractResolver, as the yaml already had the correct case, and this would cause activity state (which has PascalCasing) to be camelCased too * Updated YamlDotNet.Standard to YamlDotNet Switched package as YamlDotNet.Standard has been unlisted on Nuget
This commit is contained in:
parent
4bc499ce1d
commit
434754e466
|
|
@ -43,8 +43,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.1" />
|
||||
<PackageReference Include="NodaTime.Serialization.JsonNet" Version="2.2.0" />
|
||||
<PackageReference Include="Elsa.Scrutor" Version="3.0.2.26" />
|
||||
|
||||
<PackageReference Include="YamlDotNet.NetStandard" Version="4.0.0" />
|
||||
<PackageReference Include="YamlDotNet" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
using System.Dynamic;
|
||||
using System.Dynamic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NodaTime;
|
||||
using NodaTime.Serialization.JsonNet;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Elsa.Serialization.Formatters
|
||||
|
|
@ -9,15 +11,22 @@ namespace Elsa.Serialization.Formatters
|
|||
public class YamlTokenFormatter : ITokenFormatter
|
||||
{
|
||||
public const string FormatName = "YAML";
|
||||
private readonly ExpandoObjectConverter expandoObjectConverter;
|
||||
private readonly Serializer serializer;
|
||||
private readonly Deserializer deserializer;
|
||||
private readonly ISerializer serializer;
|
||||
private readonly IDeserializer deserializer;
|
||||
private readonly JsonSerializerSettings jsonSerializerSettings;
|
||||
|
||||
public YamlTokenFormatter()
|
||||
{
|
||||
expandoObjectConverter = new ExpandoObjectConverter();
|
||||
serializer = new SerializerBuilder().Build();
|
||||
deserializer = new DeserializerBuilder().Build();
|
||||
|
||||
jsonSerializerSettings = new JsonSerializerSettings {
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
};
|
||||
|
||||
jsonSerializerSettings
|
||||
.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb)
|
||||
.Converters.Add(new ExpandoObjectConverter());
|
||||
}
|
||||
|
||||
public string Format => FormatName;
|
||||
|
|
@ -26,15 +35,16 @@ namespace Elsa.Serialization.Formatters
|
|||
public string ToString(JToken token)
|
||||
{
|
||||
var json = token.ToString(Formatting.None);
|
||||
var expandoObject = JsonConvert.DeserializeObject<ExpandoObject>(json, expandoObjectConverter);
|
||||
var expandoObject = JsonConvert.DeserializeObject<ExpandoObject>(json, jsonSerializerSettings);
|
||||
return serializer.Serialize(expandoObject);
|
||||
}
|
||||
|
||||
public JToken FromString(string data)
|
||||
{
|
||||
var expandoObject = deserializer.Deserialize<ExpandoObject>(data);
|
||||
var json = JsonConvert.SerializeObject(expandoObject);
|
||||
return JToken.Parse(json);
|
||||
var json = JsonConvert.SerializeObject(expandoObject, jsonSerializerSettings);
|
||||
|
||||
return JsonConvert.DeserializeObject<JToken>(json, jsonSerializerSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue