{"record":{"id":"d5b08b7ce5b65c30","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-int64","errorCode":null,"errorMessage":"Can not convert {0} to Int64.","messagePattern":"Can not convert (.+?) to Int64\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":643,"sourceCode":"            {\n                return Convert.ToBoolean((int)integer);\n            }\n#endif\n\n            return (v.Value != null) ? (bool?)Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture) : null;\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"Int64\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator long(JToken value)\n        {\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, NumberTypes, false))\n            {\n                throw new ArgumentException(\"Can not convert {0} to Int64.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (long)integer;\n            }\n#endif\n\n            return Convert.ToInt64(v.Value, CultureInfo.InvariantCulture);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"DateTime\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator DateTime?(JToken? value)","sourceCodeStart":625,"sourceCodeEnd":661,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L625-L661","documentation":"Thrown by the explicit (non-nullable) cast operator long(JToken) at JToken.cs:643. The token type must be in NumberTypes = {Integer, Float, String, Comment, Raw, Boolean}. A JSON null, a Date/Guid/Uri/TimeSpan/Bytes value, or a container (JObject/JArray) fails this ArgumentException. Note String IS allowed (the digits are parsed by Convert.ToInt64), so only non-numeric strings fail later with a FormatException, not this message.","triggerScenarios":"Casting a JSON null JValue to (long)token; casting a JTokenType.Date/Boolean(Guid-style)/Object/Array to long; a token whose Type is Null because the field was omitted.","commonSituations":"An optional numeric ID that came back as JSON null; a field represented as an object or array instead of an integer; reading a Date-typed value as a number.","solutions":["Switch to the nullable cast (long?)token and coalesce: ((long?)token) ?? 0.","Guard with token.Type == JTokenType.Integer || token.Type == JTokenType.Float before the non-nullable cast.","Use token.Value<long?>() to tolerate null/missing.","Deserialize into a nullable long POCO property."],"exampleFix":"// before\nlong id = (long)token[\"id\"]; // throws when id is null\n\n// after\nlong id = (long?)token[\"id\"] ?? 0;","handlingStrategy":"type-guard","validationCode":"// Verify the token is a number-compatible scalar before the non-nullable long cast\nstatic bool CanToLong(JToken? t) =>\n    t is JValue v && (v.Type == JTokenType.Integer || v.Type == JTokenType.Float\n                      || v.Type == JTokenType.String || v.Type == JTokenType.Boolean);","typeGuard":"static long? SafeLong(JToken? t) => t switch {\n    JValue v when v.Type is JTokenType.Integer or JTokenType.Float\n                       or JTokenType.String or JTokenType.Boolean => (long?)t,\n    JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,\n    _ => null\n};","tryCatchPattern":"try\n{\n    long l = (long)token;\n}\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Can not convert\"))\n{\n    // token is null/container/date; default to 0\n}","preventionTips":["Use the nullable cast (long?)token for fields that may be JSON null.","Check token.Type is Integer/Float before the non-nullable long cast.","Prefer token.Value<long?>() which tolerates missing/null.","Deserialize into a nullable long POCO property."],"tags":["linq-to-json","jtoken","cast","int64","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}