{"record":{"id":"e5a9796e5380887a","repo":"Unity-Technologies/UnityCsReference","slug":"tried-to-read-non-float-json-value-as-float","errorCode":null,"errorMessage":"Tried to read non-float json value as float","messagePattern":"Tried to read non-float json value as float","errorType":"exception","errorClass":"JSONTypeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetStore/Json.cs","lineNumber":93,"sourceCode":"        {\n            if (data is string)\n                return (string)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read non-string json value as string\");\n            return \"\";\n        }\n\n        public string AsString()\n        {\n            return AsString(false);\n        }\n\n        public float AsFloat(bool nothrow)\n        {\n            if (data is float)\n                return (float)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read non-float json value as float\");\n            return 0.0f;\n        }\n\n        public float AsFloat()\n        {\n            return AsFloat(false);\n        }\n\n        public bool AsBool(bool nothrow)\n        {\n            if (data is bool)\n                return (bool)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read non-bool json value as bool\");\n            return false;\n        }\n\n        public bool AsBool()","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetStore/Json.cs#L75-L111","documentation":"JSONValue.AsFloat(bool nothrow) returns the wrapped value only when it is a float; otherwise (with nothrow false, used by the parameterless AsFloat()) it throws JSONTypeException. Because the library stores numbers specifically as float, an integer-looking JSON number that was parsed as float will work, but a string or bool will not, and there is no implicit parsing of numeric strings.","triggerScenarios":"Calling AsFloat() on a value that is a string (e.g. \"1.5\"), bool, object, or array; reading a numeric field that the server sent as a quoted string.","commonSituations":"API returns numbers as strings; mixed-schema responses; field repurposed from number to string across versions.","solutions":["Check IsFloat() (or read via AsFloat(true) which yields 0.0f) before committing to the typed read.","If the source is a numeric string, call AsString(true) then float.TryParse it.","Normalize/validate the response shape before consumption."],"exampleFix":"// before\nfloat v = node[\"price\"].AsFloat(); // throws if \"price\" is \"9.99\"\n\n// after\nfloat v;\nif (node[\"price\"].IsString())\n    float.TryParse(node[\"price\"].AsString(),\n        System.Globalization.NumberStyles.Float,\n        System.Globalization.CultureInfo.InvariantCulture, out v);\nelse\n    v = node[\"price\"].AsFloat(true);","handlingStrategy":"type-guard","validationCode":"float ReadFloat(JSONValue v) {\n    if (v.IsFloat()) return v.AsFloat();\n    if (v.IsString() && float.TryParse(v.AsString(), System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var f)) return f;\n    return 0f;\n}","typeGuard":"static float AsFloatOr(JSONValue v, float fallback) => v.IsFloat() ? v.AsFloat() : fallback;","tryCatchPattern":"float f;\ntry { f = v.AsFloat(); } catch (JSONTypeException) { f = 0f; }","preventionTips":["Prefer IsFloat() / AsFloat(true) for numeric fields with mixed encodings.","Parse numeric strings explicitly with InvariantCulture to avoid locale bugs.","Log field types during development to detect schema changes."],"tags":["json","type-mismatch","asset-store","unity"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}