{"record":{"id":"a7fa561c28ccfc5f","repo":"OrchardCMS/OrchardCore","slug":"unexpected-type-to-convert","errorCode":null,"errorMessage":"Unexpected type to convert.","messagePattern":"Unexpected type to convert\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs","lineNumber":15,"sourceCode":"using System.Globalization;\nusing System.Text.Json;\nusing System.Text.Json.Serialization;\n\nnamespace OrchardCore.Json.Serialization;\n\npublic class DateTimeJsonConverter : JsonConverter<DateTime>\n{\n    public static readonly DateTimeJsonConverter Instance = new();\n\n    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)\n    {\n        if (typeToConvert != typeof(DateTime))\n        {\n            throw new ArgumentException(\"Unexpected type to convert.\", nameof(typeToConvert));\n        }\n\n        if (!reader.TryGetDateTime(out var value))\n        {\n            var stringValue = reader.GetString();\n            if (DateTime.TryParse(stringValue, out value))\n            {\n                return value;\n            }\n\n            throw new JsonException($\"Unable to convert \\\"{stringValue}\\\" to DateTime.\");\n        }\n\n        return value;\n    }\n\n    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)\n        => writer.WriteStringValue(value.ToString(\"yyyy-MM-ddTHH:mm:ssZ\", CultureInfo.InvariantCulture));","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs#L1-L33","documentation":"Thrown by the DateTimeJsonConverter when the serializer calls Read with a typeToConvert that is not exactly System.DateTime. This is an internal contract guard: the converter is registered for DateTime only, so in practice it indicates the converter was incorrectly applied to another type (e.g. DateTime? or DateTimeOffset) via options.Converters or a [JsonConverter] attribute.","triggerScenarios":"Thrown at src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs:15 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Remove the DateTimeJsonConverter registration and let System.Text.Json handle the target type with its own converter","If converting Nullable<DateTime>, register a JsonConverter<DateTime?> instead of reusing this converter","Ensure any [JsonConverter(typeof(DateTimeJsonConverter))] attribute is placed only on DateTime members"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}