elsa-core/src/modules/Elsa.Common/Serialization/JsonSerializer.cs
Marko Lahma 12f947cf9b
Upgrade Quartz and some warning fixes (#5949)
Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
2024-09-16 01:55:24 -07:00

60 lines
1.6 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Elsa.Common.Contracts;
namespace Elsa.Common.Serialization;
/// <summary>
/// Provides JSON serialization services.
/// </summary>
public class StandardJsonSerializer : ConfigurableSerializer, IJsonSerializer
{
/// <inheritdoc />
public StandardJsonSerializer(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
/// <inheritdoc />
[RequiresUnreferencedCode("The type is not known at compile time.")]
public string Serialize(object value)
{
var options = GetOptions();
return JsonSerializer.Serialize(value, options);
}
/// <inheritdoc />
[RequiresUnreferencedCode("The type is not known at compile time.")]
public string Serialize(object? value, Type type)
{
var options = GetOptions();
return JsonSerializer.Serialize(value, type, options);
}
/// <inheritdoc />
public string Serialize<T>(T value)
{
return Serialize(value, typeof(T));
}
/// <inheritdoc />
[RequiresUnreferencedCode("The type is not known at compile time.")]
public object Deserialize(string json)
{
var options = GetOptions();
return JsonSerializer.Deserialize<object>(json, options)!;
}
/// <inheritdoc />
[RequiresUnreferencedCode("The type is not known at compile time.")]
public object Deserialize(string json, Type type)
{
var options = GetOptions();
return JsonSerializer.Deserialize(json, type, options)!;
}
/// <inheritdoc />
public T Deserialize<T>(string json)
{
return (T)Deserialize(json, typeof(T));
}
}