{"record":{"id":"d5562863079c15d7","repo":"OrchardCMS/OrchardCore","slug":"unknown-token-type-0-dynamicjsonconverter","errorCode":null,"errorMessage":"Unknown token type {0}","messagePattern":"Unknown token type (.+?)","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DynamicJsonConverter.cs","lineNumber":93,"sourceCode":"                        case JsonTokenType.PropertyName:\n                            var key = reader.GetString();\n                            reader.Read();\n                            if (key is not null)\n                            {\n                                dictionary[key] = Read(ref reader, typeof(object), options);\n                            }\n\n                            break;\n\n                        default:\n                            throw new JsonException(\"Cannot parse object.\");\n                    }\n                }\n\n                throw new JsonException();\n\n            default:\n                throw new JsonException(string.Format(\"Unknown token type {0}\", reader.TokenType));\n        }\n    }\n\n    public override void Write(\n        Utf8JsonWriter writer,\n        object objectToWrite,\n        JsonSerializerOptions options) =>\n        JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options);\n}\n","sourceCodeStart":75,"sourceCodeEnd":103,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DynamicJsonConverter.cs#L75-L103","documentation":"This is the terminal fallback of DynamicJsonConverter.Read: after the switch exhausts all recognized JsonTokenType cases it throws a JsonException formatted with the unknown reader.TokenType. Unlike error 45 (thrown inside object parsing), this fires at the top-level dispatch, meaning the very first/most recent token read was of a type the converter never mapped at all. It signals the converter was handed JSON it was never designed to consume.","triggerScenarios":"JsonSerializer.Deserialize/Serialize with a type routed through DynamicJsonConverter when reader.TokenType is outside the handled set — e.g. None (reader not advanced), a stray PropertyName, EndObject/EndArray reached as a value position, or a comment token (JsonCommentHandling.Allow).","commonSituations":"Passing an unadvanced Utf8JsonReader (TokenType == None) into deserialization; enabling comment handling and feeding JSON with comments; calling Deserialize on a stream already consumed; custom pipeline code that re-reads a value and hands the converter an EndObject token.","solutions":["Check reader.TokenType before calling the deserializer; call reader.Read() at least once so TokenType is not None.","Strip comments or avoid JsonCommentHandling.Allow when the payload goes through this converter.","Ensure the stream/reader is rewound (reader = new Utf8JsonReader(buffer)) and not already exhausted before deserialization.","Deserialize the complete top-level value (object/array), not a fragment; wrap primitives in a document if needed.","If a new token type is valid for your data, add an explicit case for it in DynamicJsonConverter.Read."],"exampleFix":"// before: unadvanced reader\nvar reader = new Utf8JsonReader(buffer);\nvar value = JsonSerializer.Deserialize<JsonDynamicObject>(ref reader);\n\n// after: advance the reader to a real token first\nvar reader = new Utf8JsonReader(buffer);\nif (!reader.Read()) throw new InvalidDataException(\"Empty payload\");\nif (reader.TokenType == JsonTokenType.StartObject)\n{\n    var value = JsonSerializer.Deserialize<JsonDynamicObject>(ref reader);\n}","handlingStrategy":"validation","validationCode":"var reader = new Utf8JsonReader(buffer);\nif (!reader.Read()) throw new InvalidDataException(\"Empty JSON payload\");\nif (reader.TokenType is JsonTokenType.None or JsonTokenType.Comment or JsonTokenType.PropertyName or JsonTokenType.EndObject or JsonTokenType.EndArray)\n    throw new InvalidDataException($\"Reader must start on a value token, got {reader.TokenType}\");","typeGuard":"static bool IsValueStartToken(JsonTokenType t) =>\n    t is JsonTokenType.StartObject or JsonTokenType.StartArray\n        or JsonTokenType.String or JsonTokenType.Number\n        or JsonTokenType.True or JsonTokenType.False or JsonTokenType.Null;","tryCatchPattern":"try\n{\n    var value = JsonSerializer.Deserialize<JsonDynamicObject>(ref reader, options);\n}\ncatch (JsonException ex) when (ex.Message.StartsWith(\"Unknown token type\"))\n{\n    logger.LogError(ex, \"Reader was on token {Token}; rewind the reader to a value start\", reader.TokenType);\n    reader = new Utf8JsonReader(buffer); reader.Read(); // rewind and retry once\n}","preventionTips":["Call reader.Read() before deserializing so TokenType is never None.","Do not enable JsonCommentHandling.Allow for payloads handled by this converter.","Deserialize whole top-level values, never JSON fragments or trailing end tokens.","Keep a single serializer options instance so converter routing is predictable.","Test with empty, truncated, and comment-laden payloads."],"tags":["json","deserialization","system-text-json","orchardcore"],"backgroundTag":"json-unmarshal-failed","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"}