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
+30-31Lines changed: 30 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,12 +25,11 @@ A flexible and powerful library for customizing `DateTime`, `DateTimeOffset`, `D
25
25
26
26
## Overview
27
27
28
-
This package provides four ways to specify custom date formats for `DateTime`, `DateTimeOffset`, `DateOnly`, `TimeOnly`, and their nullable counterparts when serializing and deserializing JSON using `System.Text.Json`:
28
+
This package provides three ways to specify custom date formats for `DateTime`, `DateTimeOffset`, `DateOnly`, `TimeOnly`, and their nullable counterparts when serializing and deserializing JSON using `System.Text.Json`:
29
29
30
-
1.**`JsonDateTimeConverterAttribute`** - Simple attribute-based approach (reflection only, or .NET 9+ with resolver but produces warnings)
31
-
2.**`JsonDateTimeFormatAttribute`** - Clean attribute for source generators with .NET 9+ resolver (no warnings)
32
-
3.**`JsonDateTimeFormatConverter<T>`** - Type-safe converter for source generators (all .NET versions)
33
-
4.**`DateTimeConverterResolver`** - Contract customization for .NET 9+ source generators
30
+
1.**`JsonDateTimeConverterAttribute`** - Simple attribute-based approach for reflection-based serialization, or for System.Text.Json source generator with .NET 9+ using `DateTimeConverterResolver` (produces warnings)
31
+
2.**`JsonDateTimeFormatAttribute` + `DateTimeConverterResolver`** - Clean attribute-based approach for System.Text.Json source generator with .NET 9+ (no warnings)
32
+
3.**`JsonDateTimeFormatConverter<T>`** - Type-safe converter that works with both reflection-based serialization and System.Text.Json source generator (all .NET versions)
@@ -63,7 +59,7 @@ var json = JsonSerializer.Serialize(new MyModel { Date = DateTime.Now });
63
59
// Output: {"Date":"2026-01-15"}
64
60
```
65
61
66
-
**Best for source generators** (.NET 9+, no warnings):
62
+
**Best for System.Text.Json source generator** (.NET 9+, no warnings):
67
63
68
64
```csharp
69
65
publicclassMyModel
@@ -142,7 +138,7 @@ var deserializedOrder = JsonSerializer.Deserialize<Order>(json);
142
138
143
139
### Source Generator with Format Converter (.NET 6+)
144
140
145
-
Use `JsonDateTimeFormatConverter<T>` for source generator compatibility across all .NET versions.
141
+
Use `JsonDateTimeFormatConverter<T>` for compatibility with System.Text.Json source generator across all .NET versions. This approach also works with reflection-based serialization.
146
142
147
143
```csharp
148
144
usingScarlet.System.Text.Json.DateTimeConverter;
@@ -218,9 +214,11 @@ var deserializedOrder = (Order?)JsonSerializer.Deserialize(json, typeof(Order),
218
214
```
219
215
220
216
**✅ Pros:**
221
-
- Works with source generators (AOT-friendly)
217
+
- Works with System.Text.Json source generator (AOT-friendly)
218
+
- Also works with reflection-based serialization
222
219
- Compatible with all .NET versions (6+)
223
220
- Type-safe format definitions
221
+
- Does not require `DateTimeConverterResolver`
224
222
225
223
**❌ Cons:**
226
224
- Requires defining a class for each date format
@@ -231,7 +229,7 @@ var deserializedOrder = (Order?)JsonSerializer.Deserialize(json, typeof(Order),
231
229
232
230
### Source Generator with Resolver (.NET 9+)
233
231
234
-
**.NET 9+** populates `JsonPropertyInfo.AttributeProvider` in source generators, enabling attribute-based syntax with `DateTimeConverterResolver`.
232
+
**.NET 9+** populates `JsonPropertyInfo.AttributeProvider` in System.Text.Json source generator, enabling attribute-based syntax with `DateTimeConverterResolver`.
235
233
236
234
#### Option A: JsonDateTimeFormatAttribute (Recommended - No Warnings)
237
235
@@ -281,18 +279,19 @@ var deserializedOrder = (Order?)JsonSerializer.Deserialize(json, typeof(Order),
281
279
```
282
280
283
281
**✅ Pros:**
284
-
- Clean attribute syntax with source generators
282
+
- Clean attribute syntax with System.Text.Json source generator
285
283
- AOT-friendly
286
284
-**No SYSLIB1223 warnings**
287
285
- Best of both worlds: readability + performance
288
286
289
287
**❌ Cons:**
290
288
-**Requires .NET 9+**
289
+
- Requires using `DateTimeConverterResolver` to wrap the context
291
290
- Slightly more setup (need to wrap context with resolver)
292
291
293
292
#### Option B: JsonDateTimeConverterAttribute (Backward Compatible - Has Warnings)
294
293
295
-
You can also use `JsonDateTimeConverterAttribute` (for backward compatibility), but it will produce SYSLIB1223 warnings:
294
+
You can also use `JsonDateTimeConverterAttribute`with `DateTimeConverterResolver`(for backward compatibility), but it will produce SYSLIB1223 warnings:
296
295
297
296
```csharp
298
297
publicclassOrder
@@ -302,7 +301,7 @@ public class Order
302
301
}
303
302
```
304
303
305
-
The resolver still works, but the source generator will emit warnings because `JsonDateTimeConverterAttribute` derives from `JsonConverterAttribute`.
304
+
The resolver still works, but the System.Text.Json source generator will emit warnings because `JsonDateTimeConverterAttribute` derives from `JsonConverterAttribute`.
306
305
307
306
**✅ Pros:**
308
307
- Works with existing code using `JsonDateTimeConverterAttribute`
@@ -325,26 +324,26 @@ A `JsonConverterAttribute`-derived attribute for specifying date formats directl
325
324
publicDateTimeDate { get; set; }
326
325
```
327
326
328
-
**When to use:** Reflection-based serialization. Can also be used with .NET 9+ `DateTimeConverterResolver` but produces SYSLIB1223 warnings.
327
+
**When to use:** Reflection-based serialization. Can also be used with .NET 9+ System.Text.Json source generator using `DateTimeConverterResolver`, but produces SYSLIB1223 warnings.
329
328
330
329
---
331
330
332
331
### `JsonDateTimeFormatAttribute` (.NET 9+)
333
332
334
-
A simple `Attribute`-derived attribute for specifying date formats with source generators (no warnings).
333
+
A simple `Attribute`-derived attribute for specifying date formats with System.Text.Json source generator (no warnings). Must be used with `DateTimeConverterResolver`.
335
334
336
335
```csharp
337
336
[JsonDateTimeFormat("yyyy-MM-dd")]
338
337
publicDateTimeDate { get; set; }
339
338
```
340
339
341
-
**When to use:** .NET 9+ source generators with `DateTimeConverterResolver` (recommended, no warnings).
340
+
**When to use:** .NET 9+ System.Text.Json source generator with `DateTimeConverterResolver` (recommended, no warnings).
342
341
343
342
---
344
343
345
344
### `JsonDateTimeFormatConverter<T>`
346
345
347
-
A `JsonConverterFactory` that uses `IJsonDateTimeFormat` implementations to define formats.
346
+
A `JsonConverterFactory` that uses `IJsonDateTimeFormat` implementations to define formats. Works with both reflection-based serialization and System.Text.Json source generator.
348
347
349
348
```csharp
350
349
publicclassMyFormat : IJsonDateTimeFormat
@@ -356,20 +355,20 @@ public class MyFormat : IJsonDateTimeFormat
356
355
publicDateTimeDate { get; set; }
357
356
```
358
357
359
-
**When to use:**Source generators on any .NET version (6+).
358
+
**When to use:**System.Text.Json source generator on any .NET version (6+), or reflection-based serialization when you want type-safe format definitions.
360
359
361
360
---
362
361
363
362
### `DateTimeConverterResolver` (.NET 9+)
364
363
365
-
A `JsonSerializerContext` and `IJsonTypeInfoResolver` that enables attribute-based date formatting with source generators by using contract customization.
364
+
A `JsonSerializerContext` and `IJsonTypeInfoResolver` that enables attribute-based date formatting with System.Text.Json source generator by using contract customization. Required when using `JsonDateTimeFormatAttribute`.
This is a limitation of source generators not supporting constructor parameters or static analyzer tricks.
432
+
This is a limitation of System.Text.Json source generator not supporting constructor parameters for converters.
434
433
435
434
---
436
435
437
436
### .NET 9+ Resolver Requirement
438
437
439
-
`DateTimeConverterResolver`**only works on .NET 9+** because, while `JsonPropertyInfo.AttributeProvider` exists in .NET 7-8, it is not populated by source generators until .NET 9+. See [runtime#100095](https://github.com/dotnet/runtime/issues/100095) and [runtime#102078](https://github.com/dotnet/runtime/issues/102078) for details.
438
+
`DateTimeConverterResolver`**only works on .NET 9+** because, while `JsonPropertyInfo.AttributeProvider` exists in .NET 7-8, it is not populated by System.Text.Json source generator until .NET 9+. See [runtime#100095](https://github.com/dotnet/runtime/issues/100095) and [runtime#102078](https://github.com/dotnet/runtime/issues/102078) for details.
0 commit comments