|
| 1 | +// Copyright © 2024-Present The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"), |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +using ServerlessWorkflow.Sdk.Models; |
| 15 | +using System.Collections.Concurrent; |
| 16 | +using System.Text.Json; |
| 17 | + |
| 18 | +namespace ServerlessWorkflow.Sdk.Serialization.Json; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Represents the <see cref="JsonConverterFactory"/> used to serialize/deserialize to/from <see cref="IOneOf"/> instances |
| 22 | +/// </summary> |
| 23 | +public class OneOfConverter |
| 24 | + : JsonConverterFactory |
| 25 | +{ |
| 26 | + |
| 27 | + static readonly ConcurrentDictionary<Type, JsonConverter> ConverterCache = new(); |
| 28 | + |
| 29 | + /// <inheritdoc/> |
| 30 | + public override bool CanConvert(Type typeToConvert) |
| 31 | + { |
| 32 | + if (!typeToConvert.IsGenericType) return false; |
| 33 | + var genericType = typeToConvert.GetGenericTypeDefinition(); |
| 34 | + return genericType == typeof(OneOf<,>); |
| 35 | + } |
| 36 | + |
| 37 | + /// <inheritdoc/> |
| 38 | + public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| 39 | + { |
| 40 | + return ConverterCache.GetOrAdd(typeToConvert, (type) => |
| 41 | + { |
| 42 | + var typeArgs = type.GetGenericArguments(); |
| 43 | + var converterType = typeof(OneOfConverterInner<,>).MakeGenericType(typeArgs); |
| 44 | + return (JsonConverter?)Activator.CreateInstance(converterType)!; |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + class OneOfConverterInner<T1, T2> : JsonConverter<OneOf<T1, T2>> |
| 49 | + { |
| 50 | + |
| 51 | + /// <inheritdoc/> |
| 52 | + public override OneOf<T1, T2>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 53 | + { |
| 54 | + if (reader.TokenType == JsonTokenType.Null) return null; |
| 55 | + var document = JsonDocument.ParseValue(ref reader); |
| 56 | + var rootElement = document.RootElement; |
| 57 | + try |
| 58 | + { |
| 59 | + var value1 = JsonSerializer.Deserialize<T1>(rootElement.GetRawText(), options); |
| 60 | + if (value1 != null) return new OneOf<T1, T2>(value1); |
| 61 | + } |
| 62 | + catch (JsonException) { } |
| 63 | + try |
| 64 | + { |
| 65 | + var value2 = JsonSerializer.Deserialize<T2>(rootElement.GetRawText(), options); |
| 66 | + if (value2 != null) return new OneOf<T1, T2>(value2); |
| 67 | + } |
| 68 | + catch (JsonException) { throw new JsonException($"Cannot deserialize {rootElement.GetRawText()} as either {typeof(T1).Name} or {typeof(T2).Name}"); } |
| 69 | + throw new JsonException("Unexpected error during deserialization."); |
| 70 | + } |
| 71 | + |
| 72 | + public override void Write(Utf8JsonWriter writer, OneOf<T1, T2> value, JsonSerializerOptions options) |
| 73 | + { |
| 74 | + if (value is null) |
| 75 | + { |
| 76 | + writer.WriteNullValue(); |
| 77 | + return; |
| 78 | + } |
| 79 | + switch (value.TypeIndex) |
| 80 | + { |
| 81 | + case 1: |
| 82 | + JsonSerializer.Serialize(writer, value.T1Value, options); |
| 83 | + break; |
| 84 | + case 2: |
| 85 | + JsonSerializer.Serialize(writer, value.T2Value, options); |
| 86 | + break; |
| 87 | + default: |
| 88 | + throw new JsonException("Invalid index value."); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
| 95 | + |
0 commit comments