Skip to content

Commit 941f9b7

Browse files
committed
Update README and xmldoc
1 parent 950f560 commit 941f9b7

6 files changed

Lines changed: 128 additions & 15 deletions

File tree

README.md

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
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.
77

88
## Installation
9+
```bash
10+
dotnet add package Scarlet.System.Text.Json.DateTimeConverter
11+
```
912

1013
Ensure you have the necessary .NET target framework installed. This attribute is compatible with:
1114
- .NET 6
@@ -17,10 +20,6 @@ Ensure you have the necessary .NET target framework installed. This attribute is
1720
### Example Model
1821

1922
```csharp
20-
using System;
21-
using System.Text.Json.Serialization;
22-
using Scarlet.System.Text.Json.DateTimeConverter;
23-
2423
public class MyModel
2524
{
2625
[JsonDateTimeConverter("yyyy-MM-dd")]
@@ -34,9 +33,6 @@ public class MyModel
3433
### Example Program
3534

3635
```csharp
37-
using System;
38-
using System.Text.Json;
39-
4036
public class Program
4137
{
4238
public static void Main()
@@ -59,7 +55,64 @@ public class Program
5955
}
6056
```
6157

58+
### Source Generator Example
59+
60+
If you are using source generators with `System.Text.Json`, you should use `JsonDateTimeFormatConverter` instead of `JsonDateTimeConverterAttribute`.
61+
62+
#### Example Model
63+
64+
```csharp
65+
public class MyModelSourceGenerator
66+
{
67+
[JsonConverter(typeof(JsonDateTimeFormatConverter<JsonDateTimeFormat.DateTimeFormat>))]
68+
public DateTime Date { get; set; }
69+
70+
[JsonConverter(typeof(JsonDateTimeFormatConverter<JsonDateTimeFormat.DateTimeOffsetFormat>))]
71+
public DateTimeOffset DateTimeOffset { get; set; }
72+
}
73+
74+
internal class JsonDateTimeFormat
75+
{
76+
internal class DateTimeOffsetFormat : IJsonDateTimeFormat
77+
{
78+
public static string Format => "yyyy-MM-ddTHH:mm:ss.fffZ";
79+
}
80+
81+
internal class DateTimeFormat : IJsonDateTimeFormat
82+
{
83+
public static string Format => "yyyy-MM-ddTHH:mm:ss";
84+
}
85+
}
86+
87+
[JsonSerializable(typeof(MyModelSourceGenerator))]
88+
public sealed partial class MyModelSourceGeneratorJsonSerializerContext : JsonSerializerContext;
89+
90+
public class Program
91+
{
92+
public static void Main()
93+
{
94+
var modelType = typeof(MyModelSourceGenerator);
95+
var model = new MyModelSourceGenerator
96+
{
97+
Date = DateTime.Now,
98+
DateTimeOffset = DateTimeOffset.Now
99+
};
100+
101+
var context = MyModelSourceGeneratorJsonSerializerContext.Default;
102+
103+
// Serialize
104+
string jsonString = JsonSerializer.Serialize(model, modelType, context);
105+
Console.WriteLine($"Serialized JSON: {jsonString}");
106+
107+
// Deserialize
108+
var deserializedModel = (MyModelSourceGenerator?)JsonSerializer.Deserialize(jsonString, modelType, context);
109+
Console.WriteLine($"Deserialized Date: {deserializedModel.Date}");
110+
Console.WriteLine($"Deserialized DateTimeOffset: {deserializedModel.DateTimeOffset}");
111+
}
112+
}
113+
```
114+
62115
## Notes
63116

64-
- The `JsonDateTimeConverterAttribute` can be applied to properties of type `DateTime`, `DateTime?`, `DateTimeOffset`, and `DateTimeOffset?`.
117+
- The `JsonDateTimeConverterAttribute` and `JsonDateTimeFormatConverter` can be applied to properties of type `DateTime`, `DateTime?`, `DateTimeOffset`, and `DateTimeOffset?`.
65118
- The format string provided to the attribute should follow the standard date and time format strings in .NET.

src/Scarlet.System.Text.Json.DateTimeConverter.Tests/Scarlet.System.Text.Json.DateTimeConverter.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
7+
<LangVersion>latest</LangVersion>
88
<IsPackable>false</IsPackable>
99
<IsTestProject>true</IsTestProject>
1010
</PropertyGroup>
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
namespace Scarlet.System.Text.Json.DateTimeConverter;
22

3+
/// <summary>
4+
/// Defines a contract for specifying a custom date format for JSON serialization and deserialization.
5+
/// </summary>
6+
/// <remarks>
7+
/// This interface is used when you want to use source generator `System.Text.Json`, but it also works with non-source generator (reflection-based) serialization and deserialization.
8+
/// </remarks>
39
public interface IJsonDateTimeFormat
410
{
11+
#pragma warning disable CA2252
12+
/// <summary>
13+
/// Gets the date format string to be used for JSON serialization and deserialization.
14+
/// </summary>
515
static abstract string Format { get; }
6-
}
16+
#pragma warning restore CA2252
17+
}

src/Scarlet.System.Text.Json.DateTimeConverter/JsonDateTimeConverterAttribute.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,25 @@ namespace Scarlet.System.Text.Json.DateTimeConverter;
55
/// <summary>
66
/// Specifies that a <see cref="DateTime"/>, <see cref="DateTimeOffset"/> (including Nullables) should be converted using a custom date format.
77
/// </summary>
8+
/// <remarks>
9+
/// This attribute does not work with `System.Text.Json` source generator. If you use it with <see cref="JsonSerializerContext"/>, you will get SYSLIB1223.
10+
/// Attributes deriving from <see cref="JsonConverterAttribute"/> are not supported by the source generator. You should use the <see cref="JsonDateTimeFormatConverter{T}"/> instead.
11+
/// </remarks>
12+
/// <example>
13+
/// Example usage:
14+
/// <code>
15+
/// public class Model
16+
/// {
17+
/// [JsonDateTimeConverter("yyyy-MM-ddTHH:mm:ss.fffZ")]
18+
/// public DateTimeOffset DateTimeOffsetProperty { get; set; }
19+
/// }
20+
/// </code>
21+
/// </example>
822
public sealed class JsonDateTimeConverterAttribute : JsonConverterAttribute
923
{
24+
/// <summary>
25+
/// The date format string.
26+
/// </summary>
1027
public string Format { get; }
1128

1229
/// <summary>

src/Scarlet.System.Text.Json.DateTimeConverter/JsonDateTimeFormatConverter.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,37 @@
33

44
namespace Scarlet.System.Text.Json.DateTimeConverter;
55

6+
/// <summary>
7+
/// A JSON converter factory that creates converters for <see cref="DateTime"/> and <see cref="DateTimeOffset"/> types using a specified date format.
8+
/// </summary>
9+
/// <typeparam name="T">The type that implements <see cref="IJsonDateTimeFormat"/> to provide the date format.</typeparam>
10+
/// <remarks>
11+
/// This converter is used when you want to use source generator `System.Text.Json`, but it also works with non-source generator (reflection-based) serialization and deserialization.
12+
/// </remarks>
13+
/// <example>
14+
/// Example usage:
15+
/// <code>
16+
/// public class Model
17+
/// {
18+
/// [JsonConverter(typeof(JsonDateTimeFormatConverter&lt;JsonDateTimeFormat.DateTimeOffsetFormat&gt;))]
19+
/// public DateTimeOffset DateTimeOffsetProperty { get; set; }
20+
/// }
21+
/// internal class JsonDateTimeFormat
22+
/// {
23+
/// internal class DateTimeOffsetFormat : IJsonDateTimeFormat
24+
/// {
25+
/// public static string Format => "yyyy-MM-ddTHH:mm:ss.fffZ";
26+
/// }
27+
/// }
28+
/// </code>
29+
/// </example>
630
public class JsonDateTimeFormatConverter<T> : JsonConverterFactory where T : IJsonDateTimeFormat
731
{
32+
/// <summary>
33+
/// Determines whether the specified type can be converted.
34+
/// </summary>
35+
/// <param name="typeToConvert">The type to convert.</param>
36+
/// <returns><c>true</c> if the type can be converted; otherwise, <c>false</c>.</returns>
837
public override bool CanConvert(Type typeToConvert)
938
{
1039
return typeToConvert == typeof(DateTime) ||
@@ -13,6 +42,12 @@ public override bool CanConvert(Type typeToConvert)
1342
typeToConvert == typeof(DateTimeOffset?);
1443
}
1544

45+
/// <summary>
46+
/// Creates a converter for the specified type.
47+
/// </summary>
48+
/// <param name="typeToConvert">The type to convert.</param>
49+
/// <param name="options">The serializer options.</param>
50+
/// <returns>A <see cref="JsonConverter"/> for the specified type.</returns>
1651
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
1752
{
1853
ArgumentNullException.ThrowIfNull(typeToConvert);
@@ -23,6 +58,3 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
2358
return converter;
2459
}
2560
}
26-
27-
28-

src/Scarlet.System.Text.Json.DateTimeConverter/Scarlet.System.Text.Json.DateTimeConverter.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<LangVersion>preview</LangVersion>
7+
<LangVersion>latest</LangVersion>
88
</PropertyGroup>
99

1010
<PropertyGroup>

0 commit comments

Comments
 (0)