OrchardCMS/OrchardCore · error · ArgumentException

Unexpected type to convert.

Error message

Unexpected type to convert.

What it means

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.

Solutions

  1. Remove the DateTimeJsonConverter registration and let System.Text.Json handle the target type with its own converter
  2. If converting Nullable<DateTime>, register a JsonConverter<DateTime?> instead of reusing this converter
  3. Ensure any [JsonConverter(typeof(DateTimeJsonConverter))] attribute is placed only on DateTime members
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs:15 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/a7fa561c28ccfc5f. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs:15

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OrchardCore.Json.Serialization;

public class DateTimeJsonConverter : JsonConverter<DateTime>
{
    public static readonly DateTimeJsonConverter Instance = new();

    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (typeToConvert != typeof(DateTime))
        {
            throw new ArgumentException("Unexpected type to convert.", nameof(typeToConvert));
        }

        if (!reader.TryGetDateTime(out var value))
        {
            var stringValue = reader.GetString();
            if (DateTime.TryParse(stringValue, out value))
            {
                return value;
            }

            throw new JsonException($"Unable to convert \"{stringValue}\" to DateTime.");
        }

        return value;
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
        => writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture));

View on GitHub (pinned to 4306c0717f)