{"record":{"id":"d91c1690d211edcb","repo":"JamesNK/Newtonsoft.Json","slug":"unexpected-type-code-0-for-type-1","errorCode":null,"errorMessage":"Unexpected type code '{0}' for type '{1}'.","messagePattern":"Unexpected type code '(.+?)' for type '(.+?)'\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs","lineNumber":526,"sourceCode":"                    return schemaType | JsonSchemaType.Integer;\n                case PrimitiveTypeCode.Single:\n                case PrimitiveTypeCode.Double:\n                case PrimitiveTypeCode.Decimal:\n                    return schemaType | JsonSchemaType.Float;\n                // convert to string?\n                case PrimitiveTypeCode.DateTime:\n#if HAVE_DATE_TIME_OFFSET\n                case PrimitiveTypeCode.DateTimeOffset:\n#endif\n                    return schemaType | JsonSchemaType.String;\n                case PrimitiveTypeCode.String:\n                case PrimitiveTypeCode.Uri:\n                case PrimitiveTypeCode.Guid:\n                case PrimitiveTypeCode.TimeSpan:\n                case PrimitiveTypeCode.Bytes:\n                    return schemaType | JsonSchemaType.String;\n                default:\n                    throw new JsonException(\"Unexpected type code '{0}' for type '{1}'.\".FormatWith(CultureInfo.InvariantCulture, typeCode, type));\n            }\n        }\n    }\n}","sourceCodeStart":508,"sourceCodeEnd":530,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs#L508-L530","documentation":"Thrown by JsonSchemaGenerator.Generate when mapping a .NET type to a JSON Schema type. The generator switches on the type's PrimitiveTypeCode (obtained via ConvertUtils.GetTypeCode) and the value landed in the unhandled 'default' branch (line 526), meaning the type could not be classified into any known JSON Schema category (boolean, integer, float, string, null). It is effectively an internal signal that the schema generator does not know how to represent the given type.","triggerScenarios":"Calling JsonSchemaGenerator.Generate(typeof(T)) where T resolves to a PrimitiveTypeCode that none of the handled cases cover (the switch handles Empty, Object, DBNull, Boolean, Char, the integer codes, the float codes, DateTime, DateTimeOffset, String, Uri, Guid, TimeSpan, Bytes). Any type whose code falls outside that set hits the default branch.","commonSituations":"Exotic CLR types (COM/dynamic interop types, types exposed by a newer runtime whose primitive code Json.NET in this version does not enumerate), or types whose reflection-reported type code is unexpected. Rare in normal DTO serialization; seen when generating schemas for unusual framework types.","solutions":["Do not generate a JSON schema for the offending type; serialize it directly with JsonConvert.SerializeObject instead of JsonSchemaGenerator.Generate.","Upgrade Newtonsoft.Json to a version that recognizes the primitive type code returned for your type.","Map the problem type to a known schema manually (e.g. JsonSchemaType.String) rather than letting the generator infer it.","If the type is one you control, ensure its primitive representation maps to a handled code, or register a custom contract/converter so the generator sees a supported type."],"exampleFix":"// before\nvar schema = generator.Generate(typeof(MyExoticType)); // throws Unexpected type code\n\n// after\nvar schema = generator.Generate(typeof(MyExoticType), false);\n// or avoid schema generation; just serialize the value as a string\nvar json = JsonConvert.SerializeObject(myExoticType.ToString());","handlingStrategy":"validation","validationCode":"// Before generating a schema, confirm the type maps to a known schema category.\nvar typeCode = Newtonsoft.Json.Utilities.ConvertUtils.GetTypeCode(typeof(T));\nvar known = typeCode == PrimitiveTypeCode.Empty || typeCode == PrimitiveTypeCode.Object\n    || typeCode == PrimitiveTypeCode.Boolean || typeCode == PrimitiveTypeCode.Char\n    || typeCode == PrimitiveTypeCode.String || typeCode == PrimitiveTypeCode.Uri\n    || typeCode == PrimitiveTypeCode.Guid || typeCode == PrimitiveTypeCode.TimeSpan\n    || typeCode == PrimitiveTypeCode.Bytes || typeCode == PrimitiveTypeCode.DateTime\n    || typeCode == PrimitiveTypeCode.DBNull\n    || new[] { PrimitiveTypeCode.SByte, PrimitiveTypeCode.Byte, PrimitiveTypeCode.Int16,\n        PrimitiveTypeCode.UInt16, PrimitiveTypeCode.Int32, PrimitiveTypeCode.UInt32,\n        PrimitiveTypeCode.Int64, PrimitiveTypeCode.UInt64, PrimitiveTypeCode.BigInteger,\n        PrimitiveTypeCode.Single, PrimitiveTypeCode.Double, PrimitiveTypeCode.Decimal }.Contains(typeCode);\nif (!known) throw new InvalidOperationException($\"Cannot generate schema for type code {typeCode}.\");","typeGuard":"static bool HasKnownSchemaTypeCode(Type t)\n{\n    var code = Newtonsoft.Json.Utilities.ConvertUtils.GetTypeCode(t);\n    return code != PrimitiveTypeCode.Empty; // refine with the set above as needed\n}","tryCatchPattern":"try { var schema = generator.Generate(type); }\ncatch (JsonException ex) when (ex.Message.StartsWith(\"Unexpected type code\"))\n{\n    // fall back to a string schema or skip schema generation\n}","preventionTips":["Restrict JsonSchemaGenerator.Generate to simple, primitive-heavy DTOs.","Upgrade Newtonsoft.Json so new primitive codes are handled.","Prefer direct serialization over schema generation for exotic types."],"tags":["json-schema","schema-generation","type-mapping","reflection"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}