| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Common.Serialization; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides JSON serialization services. |
| | | 8 | | /// </summary> |
| | | 9 | | public class StandardJsonSerializer : ConfigurableSerializer, IJsonSerializer |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | 1 | 12 | | public StandardJsonSerializer(IServiceProvider serviceProvider) : base(serviceProvider) |
| | | 13 | | { |
| | 1 | 14 | | } |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | [RequiresUnreferencedCode("The type is not known at compile time.")] |
| | | 18 | | public string Serialize(object value) |
| | | 19 | | { |
| | 0 | 20 | | var options = GetOptions(); |
| | 0 | 21 | | return JsonSerializer.Serialize(value, options); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | [RequiresUnreferencedCode("The type is not known at compile time.")] |
| | | 26 | | public string Serialize(object? value, Type type) |
| | | 27 | | { |
| | 0 | 28 | | var options = GetOptions(); |
| | 0 | 29 | | return JsonSerializer.Serialize(value, type, options); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public string Serialize<T>(T value) |
| | | 34 | | { |
| | 0 | 35 | | return Serialize(value, typeof(T)); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | [RequiresUnreferencedCode("The type is not known at compile time.")] |
| | | 40 | | public object Deserialize(string json) |
| | | 41 | | { |
| | 0 | 42 | | var options = GetOptions(); |
| | 0 | 43 | | return JsonSerializer.Deserialize<object>(json, options)!; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | [RequiresUnreferencedCode("The type is not known at compile time.")] |
| | | 48 | | public object Deserialize(string json, Type type) |
| | | 49 | | { |
| | 0 | 50 | | var options = GetOptions(); |
| | 0 | 51 | | return JsonSerializer.Deserialize(json, type, options)!; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public T Deserialize<T>(string json) |
| | | 56 | | { |
| | 0 | 57 | | return (T)Deserialize(json, typeof(T)); |
| | | 58 | | } |
| | | 59 | | } |