{"record":{"id":"6824f7eb207d011a","repo":"JamesNK/Newtonsoft.Json","slug":"expected-date-object-value-6824f7","errorCode":null,"errorMessage":"Expected date object value.","messagePattern":"Expected date object value\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Converters/UnixDateTimeConverter.cs","lineNumber":92,"sourceCode":"        /// <param name=\"value\">The value.</param>\n        /// <param name=\"serializer\">The calling serializer.</param>\n        public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)\n        {\n            long seconds;\n\n            if (value is DateTime dateTime)\n            {\n                seconds = (long)(dateTime.ToUniversalTime() - UnixEpoch).TotalSeconds;\n            }\n#if HAVE_DATE_TIME_OFFSET\n            else if (value is DateTimeOffset dateTimeOffset)\n            {\n                seconds = (long)(dateTimeOffset.ToUniversalTime() - UnixEpoch).TotalSeconds;\n            }\n#endif\n            else\n            {\n                throw new JsonSerializationException(\"Expected date object value.\");\n            }\n\n            if (!AllowPreEpoch && seconds < 0)\n            {\n                throw new JsonSerializationException(\"Cannot convert date value that is before Unix epoch of 00:00:00 UTC on 1 January 1970.\");\n            }\n\n            writer.WriteValue(seconds);\n        }\n\n        /// <summary>\n        /// Reads the JSON representation of the object.\n        /// </summary>\n        /// <param name=\"reader\">The <see cref=\"JsonReader\"/> to read from.</param>\n        /// <param name=\"objectType\">Type of the object.</param>\n        /// <param name=\"existingValue\">The existing property value of the JSON that is being converted.</param>\n        /// <param name=\"serializer\">The calling serializer.</param>\n        /// <returns>The object value.</returns>","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Converters/UnixDateTimeConverter.cs#L74-L110","documentation":"Thrown by UnixDateTimeConverter.WriteJson when the value is not a DateTime or DateTimeOffset. This converter emits dates as Unix epoch seconds; only DateTime and DateTimeOffset are accepted (the DateTimeOffset branch requires HAVE_DATE_TIME_OFFSET), so any other runtime type triggers the error.","triggerScenarios":"Applying UnixDateTimeConverter to a property/type whose runtime value is not DateTime or DateTimeOffset. On platforms without DateTimeOffset support, passing a DateTimeOffset also fails.","commonSituations":"A property refactored from DateTime to int/long (epoch already) or string while UnixDateTimeConverter is still attached. Serializing DateOnly/TimeOnly. Cross-platform builds where DateTimeOffset is compiled out.","solutions":["Ensure the value is DateTime or DateTimeOffset before it reaches UnixDateTimeConverter.","Remove the converter attribute from properties that are no longer dates.","If the value is already epoch seconds, serialize it as a plain long without this converter."],"exampleFix":"// before: value is already epoch seconds, converter double-converts\n[JsonConverter(typeof(UnixDateTimeConverter))]\npublic long EpochSeconds { get; set; }\n\n// after: serialize raw long, no date converter\npublic long EpochSeconds { get; set; }","handlingStrategy":"type-guard","validationCode":"object value = GetValue();\nif (value != null && !(value is DateTime) && !(value is DateTimeOffset))\n    throw new InvalidOperationException($\"UnixDateTimeConverter cannot serialize {value.GetType()}\");","typeGuard":"static bool IsDateType(object v) =>\n    v is DateTime || v is DateTimeOffset;","tryCatchPattern":"try { conv.WriteJson(writer, value, serializer); }\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Expected date object value\"))\n{\n    throw new InvalidOperationException($\"UnixDateTimeConverter got {value?.GetType()}\", ex);\n}","preventionTips":["Only apply UnixDateTimeConverter to DateTime/DateTimeOffset members.","If the value is already epoch seconds (long), serialize it without this converter.","Re-audit converter attributes after refactoring property types."],"tags":["serialization","converter","datetime","type-mismatch","unix-time"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}