(className: string, variants: JsonUnionVariant[], description: string | undefined, jsonContextType: string, isInternal: boolean)
| 968 | } |
| 969 | |
| 970 | function generateJsonUnionClass(className: string, variants: JsonUnionVariant[], description: string | undefined, jsonContextType: string, isInternal: boolean): string { |
| 971 | const lines: string[] = []; |
| 972 | lines.push(...xmlDocCommentWithFallback(description, `JSON union data type for <c>${escapeXml(className)}</c>.`, "")); |
| 973 | lines.push(`[JsonConverter(typeof(Converter))]`); |
| 974 | lines.push(`${isInternal ? "internal" : "public"} sealed partial class ${className}`); |
| 975 | lines.push(`{`); |
| 976 | |
| 977 | for (const variant of variants) { |
| 978 | lines.push(` /// <summary>Gets the value when this instance contains <see cref="${variant.typeName}"/>.</summary>`); |
| 979 | lines.push(` public ${variant.typeName}? ${variant.propertyName} { get; }`, ""); |
| 980 | } |
| 981 | |
| 982 | for (const variant of variants) { |
| 983 | lines.push(` /// <summary>Initializes a new instance of the <see cref="${className}"/> class from <see cref="${variant.typeName}"/>.</summary>`); |
| 984 | lines.push(` public ${className}(${variant.typeName} value)`); |
| 985 | lines.push(` {`); |
| 986 | lines.push(` ArgumentNullException.ThrowIfNull(value);`); |
| 987 | lines.push(` ${variant.propertyName} = value;`); |
| 988 | lines.push(` }`, ""); |
| 989 | lines.push(` /// <summary>Converts <see cref="${variant.typeName}"/> to <see cref="${className}"/>.</summary>`); |
| 990 | lines.push(` public static implicit operator ${className}(${variant.typeName} value) => new(value);`, ""); |
| 991 | } |
| 992 | |
| 993 | lines.push(` /// <summary>Provides a <see cref="JsonConverter{${className}}"/> for serializing <see cref="${className}"/> instances.</summary>`); |
| 994 | lines.push(` [EditorBrowsable(EditorBrowsableState.Never)]`); |
| 995 | lines.push(` public sealed class Converter : JsonConverter<${className}>`); |
| 996 | lines.push(` {`); |
| 997 | lines.push(` /// <inheritdoc />`); |
| 998 | lines.push(` public override ${className} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)`); |
| 999 | lines.push(` {`); |
| 1000 | lines.push(` if (reader.TokenType == JsonTokenType.Null)`); |
| 1001 | lines.push(` {`); |
| 1002 | lines.push(` throw new JsonException("Expected JSON object for ${escapeCSharpStringLiteral(className)}.");`); |
| 1003 | lines.push(` }`); |
| 1004 | lines.push(``); |
| 1005 | lines.push(` using var document = JsonDocument.ParseValue(ref reader);`); |
| 1006 | lines.push(` var element = document.RootElement;`); |
| 1007 | |
| 1008 | const fallbackVariants: JsonUnionVariant[] = []; |
| 1009 | for (const variant of variants) { |
| 1010 | const matchExpression = getJsonUnionMatchExpression(variant, variants); |
| 1011 | if (!matchExpression) { |
| 1012 | fallbackVariants.push(variant); |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
| 1016 | const valueName = variant.propertyName.charAt(0).toLowerCase() + variant.propertyName.slice(1); |
| 1017 | const deserializeExpression = `JsonSerializer.Deserialize(element, ${jsonContextType}.Default.${variant.typeName})`; |
| 1018 | lines.push(` if (${matchExpression})`); |
| 1019 | lines.push(` {`); |
| 1020 | lines.push(` var ${valueName} = ${deserializeExpression};`); |
| 1021 | lines.push(` return ${valueName} is null ? throw new JsonException("Expected ${escapeCSharpStringLiteral(variant.typeName)} value.") : new ${className}(${valueName});`); |
| 1022 | lines.push(` }`); |
| 1023 | } |
| 1024 | |
| 1025 | for (const variant of fallbackVariants) { |
| 1026 | const valueName = variant.propertyName.charAt(0).toLowerCase() + variant.propertyName.slice(1); |
| 1027 | const deserializeExpression = `JsonSerializer.Deserialize(element, ${jsonContextType}.Default.${variant.typeName})`; |
no test coverage detected
searching dependent graphs…