You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+98-14Lines changed: 98 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,11 +24,12 @@ A flexible and powerful library for customizing `DateTime` and `DateTimeOffset`
24
24
25
25
## Overview
26
26
27
-
This package provides three ways to specify custom date formats for `DateTime`, `DateTimeOffset`, and their nullable counterparts when serializing and deserializing JSON using `System.Text.Json`:
27
+
This package provides four ways to specify custom date formats for `DateTime`, `DateTimeOffset`, and their nullable counterparts when serializing and deserializing JSON using `System.Text.Json`:
28
28
29
-
1.**`JsonDateTimeConverterAttribute`** - Simple attribute-based approach (reflection only, or .NET 9+ with resolver)
30
-
2.**`JsonDateTimeFormatConverter<T>`** - Type-safe converter for source generators
31
-
3.**`DateTimeConverterResolver`** - Contract customization for .NET 9+ source generators
29
+
1.**`JsonDateTimeConverterAttribute`** - Simple attribute-based approach (reflection only, or .NET 9+ with resolver but produces warnings)
30
+
2.**`JsonDateTimeFormatAttribute`** - Clean attribute for source generators with .NET 9+ resolver (no warnings)
31
+
3.**`JsonDateTimeFormatConverter<T>`** - Type-safe converter for source generators (all .NET versions)
32
+
4.**`DateTimeConverterResolver`** - Contract customization for .NET 9+ source generators
32
33
33
34
## Installation
34
35
@@ -61,6 +62,26 @@ var json = JsonSerializer.Serialize(new MyModel { Date = DateTime.Now });
61
62
// Output: {"Date":"2026-01-15"}
62
63
```
63
64
65
+
**Best for source generators** (.NET 9+, no warnings):
The resolver still works, but the source generator will emit warnings because `JsonDateTimeConverterAttribute` derives from `JsonConverterAttribute`.
273
+
274
+
**✅ Pros:**
275
+
- Works with existing code using `JsonDateTimeConverterAttribute`
276
+
- Backward compatible
277
+
278
+
**❌ Cons:**
279
+
- Produces SYSLIB1223 warnings during build
280
+
- May confuse users about the warnings
281
+
234
282
---
235
283
236
284
## Available Components
@@ -244,7 +292,20 @@ A `JsonConverterAttribute`-derived attribute for specifying date formats directl
244
292
publicDateTimeDate { get; set; }
245
293
```
246
294
247
-
**When to use:** Reflection-based serialization, or .NET 9+ with `DateTimeConverterResolver`.
295
+
**When to use:** Reflection-based serialization. Can also be used with .NET 9+ `DateTimeConverterResolver` but produces SYSLIB1223 warnings.
296
+
297
+
---
298
+
299
+
### `JsonDateTimeFormatAttribute` (.NET 9+)
300
+
301
+
A simple `Attribute`-derived attribute for specifying date formats with source generators (no warnings).
302
+
303
+
```csharp
304
+
[JsonDateTimeFormat("yyyy-MM-dd")]
305
+
publicDateTimeDate { get; set; }
306
+
```
307
+
308
+
**When to use:** .NET 9+ source generators with `DateTimeConverterResolver` (recommended, no warnings).
248
309
249
310
---
250
311
@@ -268,14 +329,14 @@ public DateTime Date { get; set; }
268
329
269
330
### `DateTimeConverterResolver` (.NET 9+)
270
331
271
-
A `JsonSerializerContext` and `IJsonTypeInfoResolver` that enables `JsonDateTimeConverterAttribute` to work with source generators by using contract customization.
332
+
A `JsonSerializerContext` and `IJsonTypeInfoResolver` that enables attribute-based date formatting with source generators by using contract customization.
| AOT compilation |`JsonDateTimeFormatConverter<T>` or .NET 9+ resolver |
353
+
| AOT compilation |`JsonDateTimeFormatConverter<T>` or .NET 9+ resolver with attributes |
292
354
293
355
---
294
356
@@ -300,7 +362,29 @@ var options = new JsonSerializerOptions { TypeInfoResolver = resolver };
300
362
301
363
> "Attributes deriving from JsonConverterAttribute are not supported by the source generator."
302
364
303
-
**Solution:** Use `JsonDateTimeFormatConverter<T>` instead, or upgrade to .NET 9+.
365
+
**Solution:** Use `JsonDateTimeFormatConverter<T>` instead, or upgrade to .NET 9+ and use `JsonDateTimeFormatAttribute` with `DateTimeConverterResolver` (no warnings).
366
+
367
+
---
368
+
369
+
### SYSLIB1223 Warning with JsonDateTimeConverterAttribute (.NET 9+ Source Generators)
370
+
371
+
When using `JsonDateTimeConverterAttribute` with source generators in .NET 9+, you'll get SYSLIB1223 warnings:
372
+
373
+
> "Attributes deriving from JsonConverterAttribute are not supported by the source generator."
374
+
375
+
This happens because `JsonDateTimeConverterAttribute` derives from `JsonConverterAttribute`. The code still works with `DateTimeConverterResolver`, but the warnings may be confusing.
376
+
377
+
**Solution:** Use `JsonDateTimeFormatAttribute` instead, which derives only from `Attribute` and produces no warnings:
0 commit comments