Unity-Technologies/UnityCsReference · error · JSONTypeException
Tried to read non-float json value as float
Error message
Tried to read non-float json value as float
What it means
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.
Source
Thrown at Editor/Mono/AssetStore/Json.cs:93
{
if (data is string)
return (string)data;
if (!nothrow)
throw new JSONTypeException("Tried to read non-string json value as string");
return "";
}
public string AsString()
{
return AsString(false);
}
public float AsFloat(bool nothrow)
{
if (data is float)
return (float)data;
if (!nothrow)
throw new JSONTypeException("Tried to read non-float json value as float");
return 0.0f;
}
public float AsFloat()
{
return AsFloat(false);
}
public bool AsBool(bool nothrow)
{
if (data is bool)
return (bool)data;
if (!nothrow)
throw new JSONTypeException("Tried to read non-bool json value as bool");
return false;
}
public bool AsBool()View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before
float v = node["price"].AsFloat(); // throws if "price" is "9.99"
// after
float v;
if (node["price"].IsString())
float.TryParse(node["price"].AsString(),
System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture, out v);
else
v = node["price"].AsFloat(true); Defensive patterns
Strategy: type-guard
Validate before calling
float ReadFloat(JSONValue v) {
if (v.IsFloat()) return v.AsFloat();
if (v.IsString() && float.TryParse(v.AsString(), System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var f)) return f;
return 0f;
} Type guard
static float AsFloatOr(JSONValue v, float fallback) => v.IsFloat() ? v.AsFloat() : fallback;
Try / catch
float f;
try { f = v.AsFloat(); } catch (JSONTypeException) { f = 0f; } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: API returns numbers as strings; mixed-schema responses; field repurposed from number to string across versions.
Related errors
- Tried to read non-string json value as string
- Tried to read non-bool json value as bool
- Tried to read {} json value as list
- Tried to read non-dictionary json value as dictionary
- Cannot serialize json value of unknown type
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/e5a9796e5380887a.
Report an issue: GitHub.