ContextClasses.cs 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// 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
    {
12
        public JsonSourceGenerationMode JsonSourceGenerationMode { get; }
13
        public bool IsIncludeFieldsEnabled { get; }
14

15
        public JsonTypeInfo<Location> Location { get; }
16
        public JsonTypeInfo<NumberTypes> NumberTypes { get; }
17 18 19 20 21 22 23 24 25
        public JsonTypeInfo<RepeatedTypes.Location> RepeatedLocation { get; }
        public JsonTypeInfo<ActiveOrUpcomingEvent> ActiveOrUpcomingEvent { get; }
        public JsonTypeInfo<CampaignSummaryViewModel> CampaignSummaryViewModel { get; }
        public JsonTypeInfo<IndexViewModel> IndexViewModel { get; }
        public JsonTypeInfo<WeatherForecastWithPOCOs> WeatherForecastWithPOCOs { get; }
        public JsonTypeInfo<EmptyPoco> EmptyPoco { get; }
        public JsonTypeInfo<HighLowTemps> HighLowTemps { get; }
        public JsonTypeInfo<MyType> MyType { get; }
        public JsonTypeInfo<MyType2> MyType2 { get; }
26
        public JsonTypeInfo<MyTypeWithCallbacks> MyTypeWithCallbacks { get; }
27
        public JsonTypeInfo<MyTypeWithPropertyOrdering> MyTypeWithPropertyOrdering { get; }
28 29
        public JsonTypeInfo<MyIntermediateType> MyIntermediateType { get; }
        public JsonTypeInfo<HighLowTempsImmutable> HighLowTempsImmutable { get; }
30
        public JsonTypeInfo<HighLowTempsRecord> HighLowTempsRecord { get; }
31 32 33
        public JsonTypeInfo<RealWorldContextTests.MyNestedClass> MyNestedClass { get; }
        public JsonTypeInfo<RealWorldContextTests.MyNestedClass.MyNestedNestedClass> MyNestedNestedClass { get; }
        public JsonTypeInfo<object[]> ObjectArray { get; }
34
        public JsonTypeInfo<byte[]> ByteArray { get; }
35
        public JsonTypeInfo<string> String { get; }
36
        public JsonTypeInfo<(string Label1, int Label2, bool)> ValueTupleStringInt32Boolean { get; }
37 38
        public JsonTypeInfo<JsonDocument> JsonDocument { get; }
        public JsonTypeInfo<JsonElement> JsonElement { get; }
39
        public JsonTypeInfo<RealWorldContextTests.ClassWithEnumAndNullable> ClassWithEnumAndNullable { get; }
40
        public JsonTypeInfo<RealWorldContextTests.ClassWithNullableProperties> ClassWithNullableProperties { get; }
41 42 43
#if NETCOREAPP
        public JsonTypeInfo<RealWorldContextTests.ClassWithDateOnlyAndTimeOnlyValues> ClassWithDateOnlyAndTimeOnlyValues { get; }
#endif
44 45
        public JsonTypeInfo<ClassWithCustomConverter> ClassWithCustomConverter { get; }
        public JsonTypeInfo<StructWithCustomConverter> StructWithCustomConverter { get; }
46 47
        public JsonTypeInfo<ClassWithCustomConverterFactory> ClassWithCustomConverterFactory { get; }
        public JsonTypeInfo<StructWithCustomConverterFactory> StructWithCustomConverterFactory { get; }
48 49
        public JsonTypeInfo<ClassWithCustomConverterProperty> ClassWithCustomConverterProperty { get; }
        public JsonTypeInfo<StructWithCustomConverterProperty> StructWithCustomConverterProperty { get; }
50 51
        public JsonTypeInfo<ClassWithCustomConverterFactoryProperty> ClassWithCustomConverterFactoryProperty { get; }
        public JsonTypeInfo<StructWithCustomConverterFactoryProperty> StructWithCustomConverterFactoryProperty { get; }
52 53
        public JsonTypeInfo<ClassWithBadCustomConverter> ClassWithBadCustomConverter { get; }
        public JsonTypeInfo<StructWithBadCustomConverter> StructWithBadCustomConverter { get; }
54
        public JsonTypeInfo<PersonStruct?> NullablePersonStruct { get; }
55 56
        public JsonTypeInfo<TypeWithValidationAttributes> TypeWithValidationAttributes { get; }
        public JsonTypeInfo<TypeWithDerivedAttribute> TypeWithDerivedAttribute { get; }
57
        public JsonTypeInfo<PolymorphicClass> PolymorphicClass { get; }
58 59 60 61 62 63 64 65 66 67 68 69 70
    }

    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));

71
        public JsonContext() : base(null)
72 73 74
        {
        }

75
        public JsonContext(JsonSerializerOptions options) : base(options)
76 77 78
        {
        }

79 80
        protected override JsonSerializerOptions? GeneratedSerializerOptions => s_defaultOptions;

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        public override JsonTypeInfo GetTypeInfo(global::System.Type type)
        {
            if (type == typeof(JsonMessage))
            {
                return JsonMessage;
            }

            return null!;
        }

        private JsonTypeInfo<JsonMessage> _JsonMessage;
        public JsonTypeInfo<JsonMessage> JsonMessage
        {
            get
            {
                if (_JsonMessage == null)
                {
98 99 100 101 102
                    JsonObjectInfoValues<JsonMessage> objectInfo = new()
                    {
                        ObjectCreator = static () => new JsonMessage(),
                        SerializeHandler = JsonMessageSerialize
                    };
103

104
                    _JsonMessage = JsonMetadataServices.CreateObjectInfo<JsonMessage>(Options, objectInfo);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                }

                return _JsonMessage;
            }
        }

        private static void JsonMessageSerialize(Utf8JsonWriter writer, JsonMessage value) => throw new NotImplementedException();
    }

    [JsonSerializable(typeof(Dictionary<string, string>))]
    [JsonSerializable(typeof(Dictionary<int, string>))]
    [JsonSerializable(typeof(Dictionary<string, JsonMessage>))]
    internal partial class DictionaryTypeContext : JsonSerializerContext { }

    [JsonSerializable(typeof(JsonMessage))]
    public partial class PublicContext : JsonSerializerContext { }
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    [JsonSerializable(typeof(JsonMessage))]
    public partial class GenericContext<T> : JsonSerializerContext { }

    public partial class ContextGenericContainer<T>
    {
        [JsonSerializable(typeof(JsonMessage))]
        public partial class NestedInGenericContainerContext : JsonSerializerContext { }
    }

    [JsonSerializable(typeof(MyContainingClass.MyNestedClass.MyNestedNestedClass))]
    [JsonSerializable(typeof(MyContainingClass.MyNestedClass.MyNestedNestedGenericClass<int>))]
    [JsonSerializable(typeof(MyContainingClass.MyNestedGenericClass<int>.MyNestedGenericNestedClass))]
    [JsonSerializable(typeof(MyContainingClass.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>))]
    [JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedClass.MyNestedNestedClass))]
    [JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedClass.MyNestedNestedGenericClass<int>))]
    [JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedClass))]
    [JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>))]
    internal partial class NestedGenericTypesContext : JsonSerializerContext { }
140
}