diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net60RegressionTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net60RegressionTests.cs index 1d768ac3286601..21a87bbb113ccc 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net60RegressionTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net60RegressionTests.cs @@ -1,15 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Text.Json.Serialization.Metadata; -using System.Text.Json.Serialization.Tests; using System.Text.Json.Tests.SourceGenRegressionTests.Net60; -using System.Threading.Tasks; using Xunit; using HighLowTemps = System.Text.Json.Tests.SourceGenRegressionTests.Net60.HighLowTemps; using WeatherForecastWithPOCOs = System.Text.Json.Tests.SourceGenRegressionTests.Net60.WeatherForecastWithPOCOs; diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.ClassWithCustomConverter.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.ClassWithCustomConverter.g.cs new file mode 100644 index 00000000000000..23ec7bdf24594a --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.ClassWithCustomConverter.g.cs @@ -0,0 +1,57 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? _ClassWithCustomConverter; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo ClassWithCustomConverter + { + get => _ClassWithCustomConverter ??= Create_ClassWithCustomConverter(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Create_ClassWithCustomConverter(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, customConverter); + } + else + { + global::System.Text.Json.Serialization.JsonConverter converter = new global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter.CustomConverter(); + global::System.Type typeToConvert = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter); + if (!converter.CanConvert(typeToConvert)) + { + throw new global::System.InvalidOperationException(string.Format("The converter '{0}' is not compatible with the type '{1}'.", converter.GetType(), typeToConvert)); + } + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo (options, converter); + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.DateTimeOffset.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.DateTimeOffset.g.cs new file mode 100644 index 00000000000000..64b6d3e910d4af --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.DateTimeOffset.g.cs @@ -0,0 +1,51 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? _DateTimeOffset; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo DateTimeOffset + { + get => _DateTimeOffset ??= Create_DateTimeOffset(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Create_DateTimeOffset(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.DateTimeOffset))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, customConverter); + } + else + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.DateTimeOffsetConverter); + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.DictionaryStringHighLowTemps.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.DictionaryStringHighLowTemps.g.cs new file mode 100644 index 00000000000000..afd5f0e518fbca --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.DictionaryStringHighLowTemps.g.cs @@ -0,0 +1,80 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo>? _DictionaryStringHighLowTemps; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo> DictionaryStringHighLowTemps + { + get => _DictionaryStringHighLowTemps ??= Create_DictionaryStringHighLowTemps(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo> Create_DictionaryStringHighLowTemps(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo>? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Collections.Generic.Dictionary))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo>(options, customConverter); + } + else + { + global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues> info = new global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues>() + { + ObjectCreator = () => new global::System.Collections.Generic.Dictionary(), + NumberHandling = default, + SerializeHandler = DictionaryStringHighLowTempsSerializeHandler + }; + + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateDictionaryInfo, global::System.String, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps>(options, info); + + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + + // Intentionally not a static method because we create a delegate to it. Invoking delegates to instance + // methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk. + private void DictionaryStringHighLowTempsSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::System.Collections.Generic.Dictionary? value) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStartObject(); + + foreach (global::System.Collections.Generic.KeyValuePair pair in value) + { + writer.WritePropertyName(pair.Key); + HighLowTempsSerializeHandler(writer, pair.Value!); + } + + writer.WriteEndObject(); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.GetJsonTypeInfo.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.GetJsonTypeInfo.g.cs new file mode 100644 index 00000000000000..1d59adb8af1c1f --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.GetJsonTypeInfo.g.cs @@ -0,0 +1,122 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext: global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver + { + /// + public override global::System.Text.Json.Serialization.Metadata.JsonTypeInfo GetTypeInfo(global::System.Type type) + { + if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)) + { + return this.WeatherForecastWithPOCOs; + } + + if (type == typeof(global::System.DateTimeOffset)) + { + return this.DateTimeOffset; + } + + if (type == typeof(global::System.Int32)) + { + return this.Int32; + } + + if (type == typeof(global::System.String)) + { + return this.String; + } + + if (type == typeof(global::System.Collections.Generic.List)) + { + return this.ListDateTimeOffset; + } + + if (type == typeof(global::System.Collections.Generic.Dictionary)) + { + return this.DictionaryStringHighLowTemps; + } + + if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps)) + { + return this.HighLowTemps; + } + + if (type == typeof(global::System.String[])) + { + return this.StringArray; + } + + if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter)) + { + return this.ClassWithCustomConverter; + } + + return null!; + } + + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? global::System.Text.Json.Serialization.Metadata.IJsonTypeInfoResolver.GetTypeInfo(global::System.Type type, global::System.Text.Json.JsonSerializerOptions options) + { + if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)) + { + return Create_WeatherForecastWithPOCOs(options, makeReadOnly: false); + } + + if (type == typeof(global::System.DateTimeOffset)) + { + return Create_DateTimeOffset(options, makeReadOnly: false); + } + + if (type == typeof(global::System.Int32)) + { + return Create_Int32(options, makeReadOnly: false); + } + + if (type == typeof(global::System.String)) + { + return Create_String(options, makeReadOnly: false); + } + + if (type == typeof(global::System.Collections.Generic.List)) + { + return Create_ListDateTimeOffset(options, makeReadOnly: false); + } + + if (type == typeof(global::System.Collections.Generic.Dictionary)) + { + return Create_DictionaryStringHighLowTemps(options, makeReadOnly: false); + } + + if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps)) + { + return Create_HighLowTemps(options, makeReadOnly: false); + } + + if (type == typeof(global::System.String[])) + { + return Create_StringArray(options, makeReadOnly: false); + } + + if (type == typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.ClassWithCustomConverter)) + { + return Create_ClassWithCustomConverter(options, makeReadOnly: false); + } + + return null; + } + + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.HighLowTemps.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.HighLowTemps.g.cs new file mode 100644 index 00000000000000..c24ad300f47e90 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.HighLowTemps.g.cs @@ -0,0 +1,124 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? _HighLowTemps; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo HighLowTemps + { + get => _HighLowTemps ??= Create_HighLowTemps(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Create_HighLowTemps(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, customConverter); + } + else + { + global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues objectInfo = new global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues() + { + ObjectCreator = static () => new global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps(), + ObjectWithParameterizedConstructorCreator = null, + PropertyMetadataInitializer = _ => HighLowTempsPropInit(options), + ConstructorParameterMetadataInitializer = null, + NumberHandling = default, + SerializeHandler = HighLowTempsSerializeHandler + }; + + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo(options, objectInfo); + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] HighLowTempsPropInit(global::System.Text.Json.JsonSerializerOptions options) + { + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[2]; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps)obj).High, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps)obj).High = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "High", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo0 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo(options, info0); + properties[0] = propertyInfo0; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues info1 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps)obj).Low, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps)obj).Low = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "Low", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo1 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo(options, info1); + properties[1] = propertyInfo1; + + return properties; + } + + // Intentionally not a static method because we create a delegate to it. Invoking delegates to instance + // methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk. + private void HighLowTempsSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps? value) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStartObject(); + writer.WriteNumber(PropName_High, value.High); + writer.WriteNumber(PropName_Low, value.Low); + + writer.WriteEndObject(); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.Int32.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.Int32.g.cs new file mode 100644 index 00000000000000..75e02c997103ef --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.Int32.g.cs @@ -0,0 +1,51 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? _Int32; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Int32 + { + get => _Int32 ??= Create_Int32(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Create_Int32(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Int32))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, customConverter); + } + else + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.Int32Converter); + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.ListDateTimeOffset.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.ListDateTimeOffset.g.cs new file mode 100644 index 00000000000000..354cd2ff19d0e7 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.ListDateTimeOffset.g.cs @@ -0,0 +1,79 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo>? _ListDateTimeOffset; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo> ListDateTimeOffset + { + get => _ListDateTimeOffset ??= Create_ListDateTimeOffset(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo> Create_ListDateTimeOffset(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo>? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Collections.Generic.List))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo>(options, customConverter); + } + else + { + global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues> info = new global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues>() + { + ObjectCreator = () => new global::System.Collections.Generic.List(), + NumberHandling = default, + SerializeHandler = ListDateTimeOffsetSerializeHandler + }; + + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateListInfo, global::System.DateTimeOffset>(options, info); + + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + + // Intentionally not a static method because we create a delegate to it. Invoking delegates to instance + // methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk. + private void ListDateTimeOffsetSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::System.Collections.Generic.List? value) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStartArray(); + + for (int i = 0; i < value.Count; i++) + { + writer.WriteStringValue(value[i]); + } + + writer.WriteEndArray(); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.PropertyNames.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.PropertyNames.g.cs new file mode 100644 index 00000000000000..d2edcb1433c79c --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.PropertyNames.g.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + + private static readonly global::System.Text.Json.JsonEncodedText PropName_Date = global::System.Text.Json.JsonEncodedText.Encode("Date"); + private static readonly global::System.Text.Json.JsonEncodedText PropName_TemperatureCelsius = global::System.Text.Json.JsonEncodedText.Encode("TemperatureCelsius"); + private static readonly global::System.Text.Json.JsonEncodedText PropName_Summary = global::System.Text.Json.JsonEncodedText.Encode("Summary"); + private static readonly global::System.Text.Json.JsonEncodedText PropName_DatesAvailable = global::System.Text.Json.JsonEncodedText.Encode("DatesAvailable"); + private static readonly global::System.Text.Json.JsonEncodedText PropName_TemperatureRanges = global::System.Text.Json.JsonEncodedText.Encode("TemperatureRanges"); + private static readonly global::System.Text.Json.JsonEncodedText PropName_SummaryWords = global::System.Text.Json.JsonEncodedText.Encode("SummaryWords"); + private static readonly global::System.Text.Json.JsonEncodedText PropName_High = global::System.Text.Json.JsonEncodedText.Encode("High"); + private static readonly global::System.Text.Json.JsonEncodedText PropName_Low = global::System.Text.Json.JsonEncodedText.Encode("Low"); + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.String.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.String.g.cs new file mode 100644 index 00000000000000..f75f28dcc1f6f8 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.String.g.cs @@ -0,0 +1,51 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? _String; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo String + { + get => _String ??= Create_String(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Create_String(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.String))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, customConverter); + } + else + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.StringConverter); + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.StringArray.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.StringArray.g.cs new file mode 100644 index 00000000000000..5c24760489554e --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.StringArray.g.cs @@ -0,0 +1,79 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? _StringArray; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo StringArray + { + get => _StringArray ??= Create_StringArray(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Create_StringArray(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.String[]))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, customConverter); + } + else + { + global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues info = new global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues() + { + ObjectCreator = null, + NumberHandling = default, + SerializeHandler = StringArraySerializeHandler + }; + + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateArrayInfo(options, info); + + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + + // Intentionally not a static method because we create a delegate to it. Invoking delegates to instance + // methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk. + private void StringArraySerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::System.String[]? value) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStartArray(); + + for (int i = 0; i < value.Length; i++) + { + writer.WriteStringValue(value[i]); + } + + writer.WriteEndArray(); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.WeatherForecastWithPOCOs.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.WeatherForecastWithPOCOs.g.cs new file mode 100644 index 00000000000000..9a8cd85b6cc709 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.WeatherForecastWithPOCOs.g.cs @@ -0,0 +1,231 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + public partial class Net70GeneratedContext + { + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? _WeatherForecastWithPOCOs; + /// + /// Defines the source generated JSON serialization contract metadata for a given type. + /// + public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo WeatherForecastWithPOCOs + { + get => _WeatherForecastWithPOCOs ??= Create_WeatherForecastWithPOCOs(Options, makeReadOnly: true); + } + + private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo Create_WeatherForecastWithPOCOs(global::System.Text.Json.JsonSerializerOptions options, bool makeReadOnly) + { + global::System.Text.Json.Serialization.Metadata.JsonTypeInfo? jsonTypeInfo = null; + global::System.Text.Json.Serialization.JsonConverter? customConverter; + if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs))) != null) + { + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo(options, customConverter); + } + else + { + global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues objectInfo = new global::System.Text.Json.Serialization.Metadata.JsonObjectInfoValues() + { + ObjectCreator = static () => new global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs(), + ObjectWithParameterizedConstructorCreator = null, + PropertyMetadataInitializer = _ => WeatherForecastWithPOCOsPropInit(options), + ConstructorParameterMetadataInitializer = null, + NumberHandling = default, + SerializeHandler = WeatherForecastWithPOCOsSerializeHandler + }; + + jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateObjectInfo(options, objectInfo); + } + + if (makeReadOnly) + { + jsonTypeInfo.MakeReadOnly(); + } + + return jsonTypeInfo; + } + + private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] WeatherForecastWithPOCOsPropInit(global::System.Text.Json.JsonSerializerOptions options) + { + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[7]; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).Date, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).Date = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "Date", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo0 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo(options, info0); + properties[0] = propertyInfo0; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues info1 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).TemperatureCelsius, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).TemperatureCelsius = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "TemperatureCelsius", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo1 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo(options, info1); + properties[1] = propertyInfo1; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues info2 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).Summary!, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).Summary = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "Summary", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo2 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo(options, info2); + properties[2] = propertyInfo2; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues> info3 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues>() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).DatesAvailable!, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).DatesAvailable = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "DatesAvailable", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo3 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo>(options, info3); + properties[3] = propertyInfo3; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues> info4 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues>() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).TemperatureRanges!, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).TemperatureRanges = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "TemperatureRanges", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo4 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo>(options, info4); + properties[4] = propertyInfo4; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues info5 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues() + { + IsProperty = true, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).SummaryWords!, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).SummaryWords = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "SummaryWords", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo5 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo(options, info5); + properties[5] = propertyInfo5; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues info6 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues() + { + IsProperty = false, + IsPublic = true, + IsVirtual = false, + DeclaringType = typeof(global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs), + Converter = null, + Getter = static (obj) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).SummaryField!, + Setter = static (obj, value) => ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)obj).SummaryField = value!, + IgnoreCondition = null, + HasJsonInclude = false, + IsExtensionData = false, + NumberHandling = default, + PropertyName = "SummaryField", + JsonPropertyName = null + }; + + global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo propertyInfo6 = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo(options, info6); + properties[6] = propertyInfo6; + + return properties; + } + + // Intentionally not a static method because we create a delegate to it. Invoking delegates to instance + // methods is almost as fast as virtual calls. Static methods need to go through a shuffle thunk. + private void WeatherForecastWithPOCOsSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs? value) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStartObject(); + writer.WriteString(PropName_Date, ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)value).Date); + writer.WriteNumber(PropName_TemperatureCelsius, ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)value).TemperatureCelsius); + writer.WriteString(PropName_Summary, ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)value).Summary); + writer.WritePropertyName(PropName_DatesAvailable); + ListDateTimeOffsetSerializeHandler(writer, ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)value).DatesAvailable!); + writer.WritePropertyName(PropName_TemperatureRanges); + DictionaryStringHighLowTempsSerializeHandler(writer, ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)value).TemperatureRanges!); + writer.WritePropertyName(PropName_SummaryWords); + StringArraySerializeHandler(writer, ((global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs)value).SummaryWords!); + + writer.WriteEndObject(); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.cs new file mode 100644 index 00000000000000..ebfbbd7c79f07a --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.cs @@ -0,0 +1,48 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + //[JsonSerializable(typeof(WeatherForecastWithPOCOs))] + //[JsonSerializable(typeof(ClassWithCustomConverter))] + public partial class Net70GeneratedContext : JsonSerializerContext { } + + public class WeatherForecastWithPOCOs + { + public DateTimeOffset Date { get; set; } + public int TemperatureCelsius { get; set; } + public string? Summary { get; set; } + public string? SummaryField; + public List? DatesAvailable { get; set; } + public Dictionary? TemperatureRanges { get; set; } + public string[]? SummaryWords { get; set; } + } + + public class HighLowTemps + { + public int High { get; set; } + public int Low { get; set; } + } + + [JsonConverter(typeof(CustomConverter))] + public class ClassWithCustomConverter + { + public int Value { get; set; } + + public class CustomConverter : JsonConverter + { + public override ClassWithCustomConverter? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => new ClassWithCustomConverter { Value = reader.GetInt32() - 1 }; + + public override void Write(Utf8JsonWriter writer, ClassWithCustomConverter value, JsonSerializerOptions options) + => writer.WriteNumberValue(value.Value + 1); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.g.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.g.cs new file mode 100644 index 00000000000000..d6427a7c7848b1 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70/Net70GeneratedContext.g.cs @@ -0,0 +1,80 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// Source files represent a source generated JsonSerializerContext as produced by the .NET 7 SDK. +// Used to validate correctness of contexts generated by previous SDKs against the current System.Text.Json runtime components. +// Unless absolutely necessary DO NOT MODIFY any of these files -- it would invalidate the purpose of the regression tests. + +// + +#nullable enable annotations +#nullable disable warnings + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +namespace System.Text.Json.Tests.SourceGenRegressionTests.Net70 +{ + + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Text.Json.SourceGeneration", "7.0.7.1805")] + public partial class Net70GeneratedContext + { + + private static global::System.Text.Json.JsonSerializerOptions s_defaultOptions { get; } = new global::System.Text.Json.JsonSerializerOptions() + { + DefaultIgnoreCondition = global::System.Text.Json.Serialization.JsonIgnoreCondition.Never, + IgnoreReadOnlyFields = false, + IgnoreReadOnlyProperties = false, + IncludeFields = false, + WriteIndented = false, + }; + + private static global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.Net70GeneratedContext? s_defaultContext; + + /// + /// The default associated with a default instance. + /// + public static global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.Net70GeneratedContext Default => s_defaultContext ??= new global::System.Text.Json.Tests.SourceGenRegressionTests.Net70.Net70GeneratedContext(new global::System.Text.Json.JsonSerializerOptions(s_defaultOptions)); + + /// + /// The source-generated options associated with this context. + /// + protected override global::System.Text.Json.JsonSerializerOptions? GeneratedSerializerOptions { get; } = s_defaultOptions; + + /// + public Net70GeneratedContext() : base(null) + { + } + + /// + public Net70GeneratedContext(global::System.Text.Json.JsonSerializerOptions options) : base(options) + { + } + + private static global::System.Text.Json.Serialization.JsonConverter? GetRuntimeProvidedCustomConverter(global::System.Text.Json.JsonSerializerOptions options, global::System.Type type) + { + global::System.Collections.Generic.IList converters = options.Converters; + + for (int i = 0; i < converters.Count; i++) + { + global::System.Text.Json.Serialization.JsonConverter? converter = converters[i]; + + if (converter.CanConvert(type)) + { + if (converter is global::System.Text.Json.Serialization.JsonConverterFactory factory) + { + converter = factory.CreateConverter(type, options); + if (converter == null || converter is global::System.Text.Json.Serialization.JsonConverterFactory) + { + throw new global::System.InvalidOperationException(string.Format("The converter '{0}' cannot return null or a JsonConverterFactory instance.", factory.GetType())); + } + } + + return converter; + } + } + + return null; + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70RegressionTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70RegressionTests.cs new file mode 100644 index 00000000000000..38c06bc04a2195 --- /dev/null +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/SourceGenRegressionTests/Net70RegressionTests.cs @@ -0,0 +1,157 @@ +// 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.Metadata; +using System.Text.Json.Tests.SourceGenRegressionTests.Net70; +using Xunit; +using HighLowTemps = System.Text.Json.Tests.SourceGenRegressionTests.Net70.HighLowTemps; +using WeatherForecastWithPOCOs = System.Text.Json.Tests.SourceGenRegressionTests.Net70.WeatherForecastWithPOCOs; + +namespace System.Text.Json.Tests.SourceGenRegressionTests +{ + public static class Net70RegressionTests + { + [Theory] + [MemberData(nameof(GetSupportedTypeRoundtripData))] + public static void SupportedTypeRoundtrip(JsonTypeInfo jsonTypeInfo, T value, string expectedJson) + { + string json = JsonSerializer.Serialize(value, jsonTypeInfo); + JsonTestHelper.AssertJsonEqual(expectedJson, json); + + T deserializedValue = JsonSerializer.Deserialize(json, jsonTypeInfo); + json = JsonSerializer.Serialize(deserializedValue, jsonTypeInfo); + JsonTestHelper.AssertJsonEqual(expectedJson, json); + } + + public static IEnumerable GetSupportedTypeRoundtripData() + { + var ctx = Net70GeneratedContext.Default; + yield return Wrap(ctx.Int32, 42, "42"); + yield return Wrap(ctx.DateTimeOffset, DateTimeOffset.MinValue, "\"0001-01-01T00:00:00+00:00\""); + yield return Wrap(ctx.String, "I am a string", "\"I am a string\""); + yield return Wrap(ctx.HighLowTemps, new HighLowTemps { Low = 0, High = 5 }, """{"Low":0,"High":5}"""); + yield return Wrap(ctx.ListDateTimeOffset, new List { DateTimeOffset.MinValue }, "[\"0001-01-01T00:00:00+00:00\"]"); + yield return Wrap(ctx.ClassWithCustomConverter, new ClassWithCustomConverter { Value = 41 }, "42"); + yield return Wrap(ctx.WeatherForecastWithPOCOs, new WeatherForecastWithPOCOs + { + Date = DateTimeOffset.MinValue, + TemperatureCelsius = 10, + Summary = "I am a string", + DatesAvailable = new List { DateTimeOffset.MinValue }, + TemperatureRanges = new Dictionary + { + ["key"] = new HighLowTemps { Low = 0, High = 5 } + }, + SummaryWords = new[] { "word1", "word2" }, + }, + """ + { + "Date" : "0001-01-01T00:00:00+00:00", + "TemperatureCelsius" : 10, + "Summary" : "I am a string", + "DatesAvailable" : [ "0001-01-01T00:00:00+00:00" ], + "TemperatureRanges" : + { + "key" : { "Low" : 0, "High" : 5 } + }, + "SummaryWords" : [ "word1", "word2" ] + } + """); + + static object[] Wrap(JsonTypeInfo jsonTypeInfo, T value, string expectedJson) => new object[] { jsonTypeInfo, value, expectedJson }; + } + + [Theory] + [MemberData(nameof(GetSupportedTypeRoundtripData_OptionsBased))] + public static void SupportedTypeRoundtrip_OptionsBased(T value, string expectedJson) + { + string json = JsonSerializer.Serialize(value, Net70GeneratedContext.Default.Options); + JsonTestHelper.AssertJsonEqual(expectedJson, json); + + T deserializedValue = JsonSerializer.Deserialize(json, Net70GeneratedContext.Default.Options); + json = JsonSerializer.Serialize(deserializedValue, Net70GeneratedContext.Default.Options); + JsonTestHelper.AssertJsonEqual(expectedJson, json); + } + + [Fact] + public static void UnsupportedType_ThrowsInvalidOperationException() + { + DateTime value = DateTime.MinValue; + string json = JsonSerializer.Serialize(value); + + Assert.Throws(() => JsonSerializer.Serialize(value, value.GetType(), Net70GeneratedContext.Default)); + Assert.Throws(() => JsonSerializer.Deserialize(json, value.GetType(), Net70GeneratedContext.Default)); + } + + public static IEnumerable GetSupportedTypeRoundtripData_OptionsBased() + { + yield return Wrap(new ClassWithCustomConverter { Value = 41 }, "42"); + yield return Wrap(new WeatherForecastWithPOCOs + { + Date = DateTimeOffset.MinValue, + TemperatureCelsius = 10, + Summary = "I am a string", + DatesAvailable = new List { DateTimeOffset.MinValue }, + TemperatureRanges = new Dictionary + { + ["key"] = new HighLowTemps { Low = 0, High = 5 } + }, + SummaryWords = new[] { "word1", "word2" }, + }, + """ + { + "Date" : "0001-01-01T00:00:00+00:00", + "TemperatureCelsius" : 10, + "Summary" : "I am a string", + "DatesAvailable" : [ "0001-01-01T00:00:00+00:00" ], + "TemperatureRanges" : + { + "key" : { "Low" : 0, "High" : 5 } + }, + "SummaryWords" : [ "word1", "word2" ] + } + """); + + static object[] Wrap(T value, string expectedJson) => new object[] { value, expectedJson }; + } + + [Fact] + public static void HighLowTemps_ContextReportsCorrectMetadata() + { + JsonTypeInfo jsonTypeInfo = Net70GeneratedContext.Default.HighLowTemps; + + HighLowTemps instance = jsonTypeInfo.CreateObject(); + Assert.Equal(0, instance.Low); + Assert.Equal(0, instance.High); + + Assert.Equal(2, jsonTypeInfo.Properties.Count); + + JsonPropertyInfo jsonPropertyInfo = jsonTypeInfo.Properties[0]; + Assert.Equal("High", jsonPropertyInfo.Name); + jsonPropertyInfo.Set(instance, 1); + Assert.Equal(1, instance.High); + Assert.Equal(1, jsonPropertyInfo.Get(instance)); + + jsonPropertyInfo = jsonTypeInfo.Properties[1]; + Assert.Equal("Low", jsonPropertyInfo.Name); + jsonPropertyInfo.Set(instance, 2); + Assert.Equal(2, instance.Low); + Assert.Equal(2, jsonPropertyInfo.Get(instance)); + } + + [Fact] + public static void CombinedContexts_WorksAsExpected() + { + var options = new JsonSerializerOptions + { + TypeInfoResolver = JsonTypeInfoResolver.Combine(Net70GeneratedContext.Default, new DefaultJsonTypeInfoResolver()) + }; + + // Unlike v6, v7 Contexts do implement IJsonTypeInfoResolver so combined resolvers will produce the expected output. + string expected = JsonSerializer.Serialize(new HighLowTemps(), Net70GeneratedContext.Default.HighLowTemps); + string actual = JsonSerializer.Serialize(new HighLowTemps(), options); + Assert.Equal(expected, actual); + } + } +} diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj index 9c95305a2ea693..d24b8eacb9faaf 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/System.Text.Json.Tests.csproj @@ -217,6 +217,20 @@ + + + + + + + + + + + + + +