| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Text.Json.Schema; |
| | | 5 | | using System.Text.Json.Nodes; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization.Converters |
| | | 8 | | { |
| | | 9 | | internal sealed class UnsupportedTypeConverter<T> : JsonConverter<T> |
| | | 10 | | { |
| | | 11 | | private readonly string? _errorMessage; |
| | | 12 | | |
| | 0 | 13 | | public UnsupportedTypeConverter(string? errorMessage = null) => _errorMessage = errorMessage; |
| | | 14 | | |
| | 0 | 15 | | public string ErrorMessage => _errorMessage ?? SR.Format(SR.SerializeTypeInstanceNotSupported, typeof(T).FullNam |
| | | 16 | | |
| | | 17 | | public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => |
| | 0 | 18 | | throw new NotSupportedException(ErrorMessage); |
| | | 19 | | |
| | | 20 | | public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) => |
| | 0 | 21 | | throw new NotSupportedException(ErrorMessage); |
| | | 22 | | |
| | | 23 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => |
| | 0 | 24 | | new JsonSchema { Comment = "Unsupported .NET type", Not = JsonSchema.CreateTrueSchema() }; |
| | | 25 | | } |
| | | 26 | | } |