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
+31-29Lines changed: 31 additions & 29 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 +62,7 @@ var json = JsonSerializer.Serialize(new MyModel { Date = DateTime.Now });
63
62
// Output: {"Date":"2026-01-15"}
64
63
```
65
64
66
-
**Best for source generators** (.NET 9+, no warnings):
65
+
**Best for System.Text.Json source generator** (.NET 9+, no warnings):
67
66
68
67
```csharp
69
68
publicclassMyModel
@@ -142,7 +141,7 @@ var deserializedOrder = JsonSerializer.Deserialize<Order>(json);
142
141
143
142
### Source Generator with Format Converter (.NET 6+)
144
143
145
-
Use `JsonDateTimeFormatConverter<T>` for source generator compatibility across all .NET versions.
144
+
Use `JsonDateTimeFormatConverter<T>` for compatibility with System.Text.Json source generator across all .NET versions. This approach also works with reflection-based serialization.
146
145
147
146
```csharp
148
147
usingScarlet.System.Text.Json.DateTimeConverter;
@@ -218,9 +217,11 @@ var deserializedOrder = (Order?)JsonSerializer.Deserialize(json, typeof(Order),
218
217
```
219
218
220
219
**✅ Pros:**
221
-
- Works with source generators (AOT-friendly)
220
+
- Works with System.Text.Json source generator (AOT-friendly)
221
+
- Also works with reflection-based serialization
222
222
- Compatible with all .NET versions (6+)
223
223
- Type-safe format definitions
224
+
- Does not require `DateTimeConverterResolver`
224
225
225
226
**❌ Cons:**
226
227
- Requires defining a class for each date format
@@ -231,7 +232,7 @@ var deserializedOrder = (Order?)JsonSerializer.Deserialize(json, typeof(Order),
231
232
232
233
### Source Generator with Resolver (.NET 9+)
233
234
234
-
**.NET 9+** populates `JsonPropertyInfo.AttributeProvider` in source generators, enabling attribute-based syntax with `DateTimeConverterResolver`.
235
+
**.NET 9+** populates `JsonPropertyInfo.AttributeProvider` in System.Text.Json source generator, enabling attribute-based syntax with `DateTimeConverterResolver`.
235
236
236
237
#### Option A: JsonDateTimeFormatAttribute (Recommended - No Warnings)
237
238
@@ -281,18 +282,19 @@ var deserializedOrder = (Order?)JsonSerializer.Deserialize(json, typeof(Order),
281
282
```
282
283
283
284
**✅ Pros:**
284
-
- Clean attribute syntax with source generators
285
+
- Clean attribute syntax with System.Text.Json source generator
285
286
- AOT-friendly
286
287
-**No SYSLIB1223 warnings**
287
288
- Best of both worlds: readability + performance
288
289
289
290
**❌ Cons:**
290
291
-**Requires .NET 9+**
292
+
- Requires using `DateTimeConverterResolver` to wrap the context
291
293
- Slightly more setup (need to wrap context with resolver)
292
294
293
295
#### Option B: JsonDateTimeConverterAttribute (Backward Compatible - Has Warnings)
294
296
295
-
You can also use `JsonDateTimeConverterAttribute` (for backward compatibility), but it will produce SYSLIB1223 warnings:
297
+
You can also use `JsonDateTimeConverterAttribute`with `DateTimeConverterResolver`(for backward compatibility), but it will produce SYSLIB1223 warnings:
296
298
297
299
```csharp
298
300
publicclassOrder
@@ -302,7 +304,7 @@ public class Order
302
304
}
303
305
```
304
306
305
-
The resolver still works, but the source generator will emit warnings because `JsonDateTimeConverterAttribute` derives from `JsonConverterAttribute`.
307
+
The resolver still works, but the System.Text.Json source generator will emit warnings because `JsonDateTimeConverterAttribute` derives from `JsonConverterAttribute`.
306
308
307
309
**✅ Pros:**
308
310
- Works with existing code using `JsonDateTimeConverterAttribute`
@@ -325,26 +327,26 @@ A `JsonConverterAttribute`-derived attribute for specifying date formats directl
325
327
publicDateTimeDate { get; set; }
326
328
```
327
329
328
-
**When to use:** Reflection-based serialization. Can also be used with .NET 9+ `DateTimeConverterResolver` but produces SYSLIB1223 warnings.
330
+
**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
331
330
332
---
331
333
332
334
### `JsonDateTimeFormatAttribute` (.NET 9+)
333
335
334
-
A simple `Attribute`-derived attribute for specifying date formats with source generators (no warnings).
336
+
A simple `Attribute`-derived attribute for specifying date formats with System.Text.Json source generator (no warnings). Must be used with `DateTimeConverterResolver`.
335
337
336
338
```csharp
337
339
[JsonDateTimeFormat("yyyy-MM-dd")]
338
340
publicDateTimeDate { get; set; }
339
341
```
340
342
341
-
**When to use:** .NET 9+ source generators with `DateTimeConverterResolver` (recommended, no warnings).
343
+
**When to use:** .NET 9+ System.Text.Json source generator with `DateTimeConverterResolver` (recommended, no warnings).
342
344
343
345
---
344
346
345
347
### `JsonDateTimeFormatConverter<T>`
346
348
347
-
A `JsonConverterFactory` that uses `IJsonDateTimeFormat` implementations to define formats.
349
+
A `JsonConverterFactory` that uses `IJsonDateTimeFormat` implementations to define formats. Works with both reflection-based serialization and System.Text.Json source generator.
348
350
349
351
```csharp
350
352
publicclassMyFormat : IJsonDateTimeFormat
@@ -356,20 +358,20 @@ public class MyFormat : IJsonDateTimeFormat
356
358
publicDateTimeDate { get; set; }
357
359
```
358
360
359
-
**When to use:**Source generators on any .NET version (6+).
361
+
**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
362
361
363
---
362
364
363
365
### `DateTimeConverterResolver` (.NET 9+)
364
366
365
-
A `JsonSerializerContext` and `IJsonTypeInfoResolver` that enables attribute-based date formatting with source generators by using contract customization.
367
+
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.
435
+
This is a limitation of System.Text.Json source generator not supporting constructor parameters or static analyzer tricks.
434
436
435
437
---
436
438
437
439
### .NET 9+ Resolver Requirement
438
440
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.
441
+
`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