{"record":{"id":"691c112ea4624816","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-convert-string-to-float-at","errorCode":null,"errorMessage":"Cannot convert string to float : '{}' at {}","messagePattern":"Cannot convert string to float : '(.+?)' at (.+?)","errorType":"exception","errorClass":"JSONParseException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetStore/Json.cs","lineNumber":649,"sourceCode":"                    // throw new JSONParseException(\"Missing - or + in 'e' potent specifier at \" + PosMsg());\n                    resstr += cur;\n                    Next();\n                }\n                while (cur >= '0' && cur <= '9')\n                {\n                    resstr += cur;\n                    Next();\n                }\n            }\n\n            try\n            {\n                float f = System.Convert.ToSingle(resstr, CultureInfo.InvariantCulture);\n                return new JSONValue(f);\n            }\n            catch (Exception)\n            {\n                throw new JSONParseException(\"Cannot convert string to float : '\" + resstr + \"' at \" + PosMsg());\n            }\n        }\n\n        private JSONValue ParseConstant()\n        {\n            string c = \"\";\n            c = \"\" + cur + Next() + Next() + Next();\n            Next();\n            if (c == \"true\")\n            {\n                return new JSONValue(true);\n            }\n            else if (c == \"fals\")\n            {\n                if (cur == 'e')\n                {\n                    Next();\n                    return new JSONValue(false);","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetStore/Json.cs#L631-L667","documentation":"Thrown by ParseNumber when the collected numeric character sequence cannot be converted to a float via Convert.ToSingle with InvariantCulture. This covers malformed numbers that passed the initial character-scanning loop but fail actual numeric conversion (e.g. stray characters, multiple decimal points, leading zeros issues).","triggerScenarios":"Parsing a JSON token that starts like a number (digit, minus, or decimal point) but is structurally invalid as a floating-point value — e.g. \"1.2.3\", \"--5\", \".\". The scanner accumulates characters it considers numeric, but Convert.ToSingle rejects the result.","commonSituations":"Corrupted Asset Store JSON with garbled numeric fields, locale-specific number formatting leaking into JSON, data source bugs emitting malformed numeric values, proxy or encoding transformations mangling number strings.","solutions":["Inspect the numeric token reported (resstr in the message) to identify the structural problem.","Ensure the data source emits numbers in standard JSON format (no thousands separators, single decimal point, standard exponent notation).","If parsing user-supplied or third-party data, pre-validate numeric tokens with a regex like ^-?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$ before passing to the parser.","Re-fetch the data to rule out transient corruption."],"exampleFix":"// before — malformed number in payload\nstring json = \"{\\\"price\\\": 1.2.3}\";\n// after — valid number\nstring json = \"{\\\"price\\\": 1.23}\";","handlingStrategy":"validation","validationCode":"// Pre-validate numeric tokens in JSON before parsing\nusing System.Text.RegularExpressions;\nbool HasValidNumbers(string json)\n{\n    // Find all number-like tokens and validate format\n    var matches = Regex.Matches(json, @\"-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?\");\n    // Additionally, scan for malformed numbers like 1.2.3\n    return !Regex.IsMatch(json, @\"\\d\\.\\d+\\.\\d\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var result = JSONParser.SimpleParse(jsonString);\n}\ncatch (JSONParseException ex) when (ex.Message.Contains(\"Cannot convert string to float\"))\n{\n    Debug.LogError($\"Malformed number in JSON: {ex.Message}\");\n}","preventionTips":["Ensure data sources serialize numbers using standard JSON numeric format.","Avoid locale-specific number formatting (commas as decimal separators).","Validate numeric fields with a regex before parsing if data quality is uncertain.","Use InvariantCulture when converting numbers on the producer side."],"tags":["json","parsing","asset-store","number","malformed-data"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}