{"record":{"id":"53859cd9dba8ada8","repo":"JamesNK/Newtonsoft.Json","slug":"integer-string-0-is-not-allowed","errorCode":null,"errorMessage":"Integer string '{0}' is not allowed.","messagePattern":"Integer string '(.+?)' is not allowed\\.","errorType":"validation","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/EnumUtils.cs","lineNumber":319,"sourceCode":"                value = value.Trim();\n                object? temp = null;\n\n                try\n                {\n                    temp = Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);\n                }\n                catch (FormatException)\n                {\n                    // We need to Parse this as a String instead. There are cases\n                    // when you tlbimp enums that can have values of the form \"3D\".\n                    // Don't fix this code.\n                }\n\n                if (temp != null)\n                {\n                    if (disallowNumber)\n                    {\n                        throw new FormatException(\"Integer string '{0}' is not allowed.\".FormatWith(CultureInfo.InvariantCulture, value));\n                    }\n\n                    return Enum.ToObject(enumType, temp);\n                }\n            }\n\n            ulong result = 0;\n\n            int valueIndex = firstNonWhitespaceIndex;\n            while (valueIndex <= value.Length) // '=' is to handle invalid case of an ending comma\n            {\n                // Find the next separator, if there is one, otherwise the end of the string.\n                int endIndex = value.IndexOf(EnumSeparatorChar, valueIndex);\n                if (endIndex == -1)\n                {\n                    endIndex = value.Length;\n                }\n","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/EnumUtils.cs#L301-L337","documentation":"ParseEnum (EnumUtils.cs:296-323), after determining the value parses as a number (char.IsDigit or sign at the start and Convert.ChangeType succeeds), checks the disallowNumber flag at EnumUtils.cs:317-320. If disallowNumber is true, it throws FormatException reporting the integer string. disallowNumber is set by callers that want to reject raw integer enum values (notably StringEnumConverter with AllowIntegerValues=false).","triggerScenarios":"An enum field receives a numeric value (e.g. JSON `\"status\": 3` or the string \"3\") while the configured converter has AllowIntegerValues=false, so integer representations are forbidden.","commonSituations":"StringEnumConverter configured with AllowIntegerValues=false but the source emits integers (common with System.Text.Json interop or older producers); schema enforcing string-only enum values; serializers migrated from one that emitted numbers.","solutions":["Send enum values as their string member names in the JSON (e.g. \"Active\" rather than 3).","If integer values are acceptable, set the StringEnumConverter's AllowIntegerValues=true (and AllowNullValues if needed).","Pre-process the payload to map numbers to names before deserialization.","Use a custom JsonConverter that maps disallowed integers to a default member."],"exampleFix":"// before: StringEnumConverter { AllowIntegerValues = false }, JSON = 3 -> throws\n// after (option A): emit \"Active\" in JSON\n// after (option B): new StringEnumConverter { AllowIntegerValues = true }","handlingStrategy":"validation","validationCode":"// If integers are disallowed, ensure the value is a name, not a number.\nif (disallowNumber && value.Trim().TrimStart('-','+').All(char.IsDigit) && value.Trim().Length > 0)\n    throw new FormatException($\"Integer string '{value}' is not allowed.\");\n// or: set StringEnumConverter.AllowIntegerValues = true if integers are acceptable.","typeGuard":"static bool LooksLikeInteger(string s) { s = s.Trim(); return s.Length > 0 && (char.IsDigit(s[0]) || s[0]=='-' || s[0]=='+') && s.TrimStart('-','+').All(char.IsDigit); }","tryCatchPattern":"try { return EnumUtils.ParseEnum(enumType, null, value, disallowNumber: true); } catch (FormatException ex) when (ex.Message.Contains(\"not allowed\")) { /* map to a name or accept integers */ }","preventionTips":["Send enum values as member-name strings, not integers, when integers are disallowed.","Set StringEnumConverter.AllowIntegerValues=true if numeric enums are acceptable.","Document the string-only contract for enum fields in your API."],"tags":["enum","configuration","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}