{"id":18151986,"url":"https://github.com/scarletkuro/scarlet.system.text.json.datetimeconverter","last_synced_at":"2026-04-18T14:04:40.326Z","repository":{"id":259872826,"uuid":"879687775","full_name":"ScarletKuro/Scarlet.System.Text.Json.DateTimeConverter","owner":"ScarletKuro","description":"A custom attribute for System.Text.Json that allows you to specify custom date formats for DateTime, DateTimeOffset, and their nullable counterparts during JSON serialization and deserialization.","archived":false,"fork":false,"pushed_at":"2024-10-29T10:30:13.000Z","size":44,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T23:45:14.618Z","etag":null,"topics":["datetime","datetimeoffset","json","json-serialization","jsonconverter","jsonconverterattribute","stj","system-text-json"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ScarletKuro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-28T11:26:15.000Z","updated_at":"2024-10-31T19:05:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"b236a936-a42a-4761-a296-88b5cc4f7d5f","html_url":"https://github.com/ScarletKuro/Scarlet.System.Text.Json.DateTimeConverter","commit_stats":null,"previous_names":["scarletkuro/scarlet.system.text.json.datetimeconverter"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScarletKuro%2FScarlet.System.Text.Json.DateTimeConverter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScarletKuro%2FScarlet.System.Text.Json.DateTimeConverter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScarletKuro%2FScarlet.System.Text.Json.DateTimeConverter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ScarletKuro%2FScarlet.System.Text.Json.DateTimeConverter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ScarletKuro","download_url":"https://codeload.github.com/ScarletKuro/Scarlet.System.Text.Json.DateTimeConverter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247569130,"owners_count":20959758,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datetime","datetimeoffset","json","json-serialization","jsonconverter","jsonconverterattribute","stj","system-text-json"],"created_at":"2024-11-02T02:05:13.392Z","updated_at":"2026-04-18T14:04:40.286Z","avatar_url":"https://github.com/ScarletKuro.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Overview\n[![Nuget](https://img.shields.io/nuget/v/Scarlet.System.Text.Json.DateTimeConverter?color=ff4081\u0026logo=nuget)](https://www.nuget.org/packages/Scarlet.System.Text.Json.DateTimeConverter)\n[![Nuget](https://img.shields.io/nuget/dt/Scarlet.System.Text.Json.DateTimeConverter?color=ff4081\u0026label=nuget%20downloads\u0026logo=nuget)](https://www.nuget.org/packages/Scarlet.System.Text.Json.DateTimeConverter)\n[![GitHub](https://img.shields.io/github/license/ScarletKuro/Scarlet.System.Text.Json.DateTimeConverter?color=594ae2\u0026logo=github)](https://github.com/ScarletKuro/Scarlet.System.Text.Json.DateTimeConverter/blob/master/LICENSE)\n\nThis package allows you to specify a custom date format for `DateTime`, `DateTimeOffset`, and their nullable counterparts when serializing and deserializing JSON using `System.Text.Json`.\n\n## Installation\n\nTo install the **Scarlet.System.Text.Json.DateTimeConverter** package, run the following command in your terminal:\n\n```bash\ndotnet add package Scarlet.System.Text.Json.DateTimeConverter\n```\n\n### Prerequisites\n\nMake sure you have the appropriate .NET target framework installed. This package is compatible with the following versions:\n\n- .NET 6\n- .NET 7\n- .NET 8\n\n## Usage\n\nExamples of how to serialize and deserialize models with custom date formats using `JsonDateTimeConverter` attribute and `JsonDateTimeFormatConverter` converter. \n\n**Note:** The `JsonDateTimeConverter` attribute does not support `System.Text.Json` source generators. Using this attribute with `JsonSerializerContext` results in **SYSLIB1223**: \"Attributes deriving from `JsonConverterAttribute` are not supported by the source generator.\"\n\nIn such cases, use the `JsonDateTimeFormatConverter`, which also works with reflection-based serialization and deserialization. The `JsonDateTimeConverter` attribute is simply less verbose and more readable than the `JsonDateTimeFormatConverter`.\n\n### Using reflection based only with `JsonDateTimeConverter`\n\nThis will work only with reflection-based serialization and deserialization.\n\n```csharp\npublic class MyModel\n{\n    [JsonDateTimeConverter(\"yyyy-MM-dd\")]\n    public DateTime Date { get; set; }\n\n    [JsonDateTimeConverter(\"yyyy-MM-ddTHH:mm:ss.fffZ\")]\n    public DateTimeOffset DateTimeOffset { get; set; }\n}\n\npublic class Program\n{\n    public static void Main()\n    {\n        var model = new MyModel\n        {\n            Date = DateTime.Now,\n            DateTimeOffset = DateTimeOffset.Now\n        };\n\n        // Serialize\n        string jsonString = JsonSerializer.Serialize(model);\n        Console.WriteLine($\"Serialized JSON: {jsonString}\");\n\n        // Deserialize\n        var deserializedModel = JsonSerializer.Deserialize\u003cMyModel\u003e(jsonString);\n        Console.WriteLine($\"Deserialized Date: {deserializedModel.Date}\");\n        Console.WriteLine($\"Deserialized DateTimeOffset: {deserializedModel.DateTimeOffset}\");\n    }\n}\n```\n\n### Using Source Generators with `JsonDateTimeFormatConverter`\n\nTo work with `System.Text.Json` source generators, use `JsonDateTimeFormatConverter` instead of `JsonDateTimeConverterAttribute`. This can also work with reflection-based serialization and deserialization.\n\n\n```csharp\npublic class MyModelSourceGenerator\n{\n    [JsonConverter(typeof(JsonDateTimeFormatConverter\u003cJsonDateTimeFormat.DateTimeFormat\u003e))]\n    public DateTime Date { get; set; }\n\n    [JsonConverter(typeof(JsonDateTimeFormatConverter\u003cJsonDateTimeFormat.DateTimeOffsetFormat\u003e))]\n    public DateTimeOffset DateTimeOffset { get; set; }\n}\n\ninternal class JsonDateTimeFormat\n{\n    internal class DateTimeOffsetFormat : IJsonDateTimeFormat\n    {\n        public static string Format =\u003e \"yyyy-MM-ddTHH:mm:ss.fffZ\";\n    }\n    \n    internal class DateTimeFormat : IJsonDateTimeFormat\n    {\n        public static string Format =\u003e \"yyyy-MM-ddTHH:mm:ss\";\n    }\n}\n\n[JsonSerializable(typeof(MyModelSourceGenerator))]\npublic sealed partial class MyModelSourceGeneratorJsonSerializerContext : JsonSerializerContext;\n\npublic class Program\n{\n    public static void Main()\n    {\n        var modelType = typeof(MyModelSourceGenerator);\n        var model = new MyModelSourceGenerator\n        {\n            Date = DateTime.Now,\n            DateTimeOffset = DateTimeOffset.Now\n        };\n\n        var context = MyModelSourceGeneratorJsonSerializerContext.Default;\n\n        // Serialize\n        string jsonString = JsonSerializer.Serialize(model, modelType, context);\n        Console.WriteLine($\"Serialized JSON: {jsonString}\");\n\n        // Deserialize\n        var deserializedModel = (MyModelSourceGenerator?)JsonSerializer.Deserialize(jsonString, modelType, context);\n        Console.WriteLine($\"Deserialized Date: {deserializedModel.Date}\");\n        Console.WriteLine($\"Deserialized DateTimeOffset: {deserializedModel.DateTimeOffset}\");\n    }\n}\n```\n\nUnfortunately, there is no better way with the source generator than defining a class for each date-time format. This is because the `JsonConverterAttribute` is not supported by the source generator, and neither `JsonConverterFactory` nor `JsonConverter` allows passing the format string to the converter, as they lack constructors with parameters.\nThe new contract customization does not provide attribute support for the source generator as well.\n\n## Notes\n\n- The `JsonDateTimeConverterAttribute` and `JsonDateTimeFormatConverter` can be applied to properties of type `DateTime`, `DateTime?`, `DateTimeOffset`, and `DateTimeOffset?`.\n- The format string provided to the attribute should follow the standard date and time format strings in .NET.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscarletkuro%2Fscarlet.system.text.json.datetimeconverter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscarletkuro%2Fscarlet.system.text.json.datetimeconverter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscarletkuro%2Fscarlet.system.text.json.datetimeconverter/lists"}