{"record":{"id":"842479bcbc646d1f","repo":"Unity-Technologies/UnityCsReference","slug":"tried-to-read-non-bool-json-value-as-bool","errorCode":null,"errorMessage":"Tried to read non-bool json value as bool","messagePattern":"Tried to read non-bool json value as bool","errorType":"exception","errorClass":"JSONTypeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetStore/Json.cs","lineNumber":107,"sourceCode":"        {\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()\n        {\n            return AsBool(false);\n        }\n\n        public List<JSONValue> AsList(bool nothrow)\n        {\n            if (data is List<JSONValue>)\n                return (List<JSONValue>)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read \" + data.GetType().Name + \" json value as list\");\n            return null;\n        }\n\n        public List<JSONValue> AsList()","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetStore/Json.cs#L89-L125","documentation":"JSONValue.AsBool(bool nothrow) returns the value only when it is a bool; the parameterless AsBool() uses nothrow=false and throws JSONTypeException on any other type. There is no coercion of 0/1, \"true\"/\"false\" strings, so a JSON field that uses 1/0 or strings for booleans will not satisfy the bool check.","triggerScenarios":"Calling AsBool() on a value that is a number (0/1), a string (\"true\"), object, or array; reading a flag field whose representation differs from a JSON boolean.","commonSituations":"API uses integers for boolean flags; strings \"true\"/\"false\"; field type changed across versions.","solutions":["Check IsBool() before calling AsBool(), or use AsBool(true) to get false on mismatch.","If the source is 0/1 or a string, read it as number/string and convert explicitly.","Validate the expected boolean encoding at the schema boundary."],"exampleFix":"// before\nbool enabled = node[\"enabled\"].AsBool(); // throws if \"enabled\" is 1\n\n// after\nbool enabled = node[\"enabled\"].IsBool()\n    ? node[\"enabled\"].AsBool()\n    : node[\"enabled\"].AsFloat(true) != 0f;","handlingStrategy":"type-guard","validationCode":"bool ReadBool(JSONValue v) {\n    if (v.IsBool()) return v.AsBool();\n    if (v.IsFloat()) return v.AsFloat() != 0f;\n    if (v.IsString()) return v.AsString() == \"true\";\n    return false;\n}","typeGuard":"static bool AsBoolOr(JSONValue v, bool fallback) => v.IsBool() ? v.AsBool() : fallback;","tryCatchPattern":"bool b;\ntry { b = v.AsBool(); } catch (JSONTypeException) { b = false; }","preventionTips":["Normalize non-bool encodings (0/1, \"true\"/\"false\") at the schema boundary.","Use IsBool() before AsBool() for fields that may drift.","Document the canonical boolean encoding expected from each endpoint."],"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"}