// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; namespace System.Text.Json.SourceGeneration.Tests { public interface ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode { get; } public bool IsIncludeFieldsEnabled { get; } public JsonTypeInfo Location { get; } public JsonTypeInfo NumberTypes { get; } public JsonTypeInfo RepeatedLocation { get; } public JsonTypeInfo ActiveOrUpcomingEvent { get; } public JsonTypeInfo CampaignSummaryViewModel { get; } public JsonTypeInfo IndexViewModel { get; } public JsonTypeInfo WeatherForecastWithPOCOs { get; } public JsonTypeInfo EmptyPoco { get; } public JsonTypeInfo HighLowTemps { get; } public JsonTypeInfo MyType { get; } public JsonTypeInfo MyType2 { get; } public JsonTypeInfo MyTypeWithCallbacks { get; } public JsonTypeInfo MyTypeWithPropertyOrdering { get; } public JsonTypeInfo MyIntermediateType { get; } public JsonTypeInfo HighLowTempsImmutable { get; } public JsonTypeInfo HighLowTempsRecord { get; } public JsonTypeInfo MyNestedClass { get; } public JsonTypeInfo MyNestedNestedClass { get; } public JsonTypeInfo ObjectArray { get; } public JsonTypeInfo ByteArray { get; } public JsonTypeInfo String { get; } public JsonTypeInfo<(string Label1, int Label2, bool)> ValueTupleStringInt32Boolean { get; } public JsonTypeInfo JsonDocument { get; } public JsonTypeInfo JsonElement { get; } public JsonTypeInfo ClassWithEnumAndNullable { get; } public JsonTypeInfo ClassWithNullableProperties { get; } #if NETCOREAPP public JsonTypeInfo ClassWithDateOnlyAndTimeOnlyValues { get; } #endif public JsonTypeInfo ClassWithCustomConverter { get; } public JsonTypeInfo StructWithCustomConverter { get; } public JsonTypeInfo ClassWithCustomConverterFactory { get; } public JsonTypeInfo StructWithCustomConverterFactory { get; } public JsonTypeInfo ClassWithCustomConverterProperty { get; } public JsonTypeInfo StructWithCustomConverterProperty { get; } public JsonTypeInfo ClassWithCustomConverterFactoryProperty { get; } public JsonTypeInfo StructWithCustomConverterFactoryProperty { get; } public JsonTypeInfo ClassWithBadCustomConverter { get; } public JsonTypeInfo StructWithBadCustomConverter { get; } public JsonTypeInfo NullablePersonStruct { get; } public JsonTypeInfo TypeWithValidationAttributes { get; } public JsonTypeInfo TypeWithDerivedAttribute { get; } public JsonTypeInfo PolymorphicClass { get; } } internal partial class JsonContext : JsonSerializerContext { private static JsonSerializerOptions s_defaultOptions { get; } = new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; private static JsonContext s_defaultContext; public static JsonContext Default => s_defaultContext ??= new JsonContext(new JsonSerializerOptions(s_defaultOptions)); public JsonContext() : base(null) { } public JsonContext(JsonSerializerOptions options) : base(options) { } protected override JsonSerializerOptions? GeneratedSerializerOptions => s_defaultOptions; public override JsonTypeInfo GetTypeInfo(global::System.Type type) { if (type == typeof(JsonMessage)) { return JsonMessage; } return null!; } private JsonTypeInfo _JsonMessage; public JsonTypeInfo JsonMessage { get { if (_JsonMessage == null) { JsonObjectInfoValues objectInfo = new() { ObjectCreator = static () => new JsonMessage(), SerializeHandler = JsonMessageSerialize }; _JsonMessage = JsonMetadataServices.CreateObjectInfo(Options, objectInfo); } return _JsonMessage; } } private static void JsonMessageSerialize(Utf8JsonWriter writer, JsonMessage value) => throw new NotImplementedException(); } [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(Dictionary))] internal partial class DictionaryTypeContext : JsonSerializerContext { } [JsonSerializable(typeof(JsonMessage))] public partial class PublicContext : JsonSerializerContext { } [JsonSerializable(typeof(JsonMessage))] public partial class GenericContext : JsonSerializerContext { } public partial class ContextGenericContainer { [JsonSerializable(typeof(JsonMessage))] public partial class NestedInGenericContainerContext : JsonSerializerContext { } } [JsonSerializable(typeof(MyContainingClass.MyNestedClass.MyNestedNestedClass))] [JsonSerializable(typeof(MyContainingClass.MyNestedClass.MyNestedNestedGenericClass))] [JsonSerializable(typeof(MyContainingClass.MyNestedGenericClass.MyNestedGenericNestedClass))] [JsonSerializable(typeof(MyContainingClass.MyNestedGenericClass.MyNestedGenericNestedGenericClass))] [JsonSerializable(typeof(MyContainingGenericClass.MyNestedClass.MyNestedNestedClass))] [JsonSerializable(typeof(MyContainingGenericClass.MyNestedClass.MyNestedNestedGenericClass))] [JsonSerializable(typeof(MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedClass))] [JsonSerializable(typeof(MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedGenericClass))] internal partial class NestedGenericTypesContext : JsonSerializerContext { } }