Skip to content

Commit c2edbf5

Browse files
committed
Add README.md
1 parent 74bfcfb commit c2edbf5

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## Overview
2+
3+
The `JsonDateTimeConverterAttribute` allows you to specify a custom date format for `DateTime`, `DateTimeOffset`, and their nullable counterparts when serializing and deserializing JSON using `System.Text.Json`. This ensures consistency in how date and time values are handled across your application.
4+
5+
## Installation
6+
7+
Ensure you have the necessary .NET target framework installed. This attribute is compatible with:
8+
- .NET 6
9+
- .NET 7
10+
- .NET 8
11+
12+
## Usage
13+
14+
### Example Model
15+
16+
```csharp
17+
using System;
18+
using System.Text.Json.Serialization;
19+
using Scarlet.System.Text.Json.DateTimeConverter;
20+
21+
public class MyModel
22+
{
23+
[JsonDateTimeConverter("yyyy-MM-dd")]
24+
public DateTime Date { get; set; }
25+
26+
[JsonDateTimeConverter("yyyy-MM-ddTHH:mm:ss.fffZ")]
27+
public DateTimeOffset DateTimeOffset { get; set; }
28+
}
29+
```
30+
31+
### Example Program
32+
33+
```csharp
34+
using System;
35+
using System.Text.Json;
36+
37+
public class Program
38+
{
39+
public static void Main()
40+
{
41+
var model = new MyModel
42+
{
43+
Date = DateTime.Now,
44+
DateTimeOffset = DateTimeOffset.Now
45+
};
46+
47+
// Serialize
48+
string jsonString = JsonSerializer.Serialize(model);
49+
Console.WriteLine($"Serialized JSON: {jsonString}");
50+
51+
// Deserialize
52+
var deserializedModel = JsonSerializer.Deserialize<MyModel>(jsonString);
53+
Console.WriteLine($"Deserialized Date: {deserializedModel.Date}");
54+
Console.WriteLine($"Deserialized DateTimeOffset: {deserializedModel.DateTimeOffset}");
55+
}
56+
}
57+
```
58+
59+
## Notes
60+
61+
- The `JsonDateTimeConverterAttribute` can be applied to properties of type `DateTime`, `DateTime?`, `DateTimeOffset`, and `DateTimeOffset?`.
62+
- The format string provided to the attribute should follow the standard date and time format strings in .NET.

0 commit comments

Comments
 (0)