Skip to content

Commit 7c41e59

Browse files
committed
Issue #102 - Fixed problem with ambiguous class names in two well-known namespaces.
1 parent 97eaaf2 commit 7c41e59

8 files changed

Lines changed: 38 additions & 36 deletions

File tree

DateTimeOnly.Json/DateOnlyConverter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
namespace System.Text.Json;
1212

1313
/// <summary>
14-
/// Custom converter for handling the <see cref="System.DateOnly"/> data type with the <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library.
14+
/// Custom converter for handling the <see cref="DateOnly"/> data type with the <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library.
1515
/// </summary>
1616
/// <remarks>
1717
/// This class backported from:
1818
/// <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/DateOnlyConverter.cs">
1919
/// System.Text.Json.Serialization.Converters.DateOnlyConverter</see>
2020
/// </remarks>
21-
public sealed class DateOnlyConverter : JsonConverter<System.DateOnly>
21+
public sealed class DateOnlyConverter : JsonConverter<DateOnly>
2222
{
2323
private const int FormatLength = 10; // YYYY-MM-DD
2424

2525
private const int MaxEscapedFormatLength = FormatLength * JsonConstants.MaxExpansionFactorWhileEscaping;
2626

2727
/// <inheritdoc />
28-
public override System.DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
28+
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2929
{
3030
if (reader.TokenType != JsonTokenType.String)
3131
{
@@ -36,7 +36,7 @@ public override System.DateOnly Read(ref Utf8JsonReader reader, Type typeToConve
3636
}
3737

3838
/// <inheritdoc />
39-
public override System.DateOnly ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
39+
public override DateOnly ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
4040
{
4141
Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
4242
return ReadCore(ref reader);
@@ -70,7 +70,7 @@ private static DateOnly ReadCore(ref Utf8JsonReader reader)
7070
}
7171

7272
/// <inheritdoc />
73-
public override void Write(Utf8JsonWriter writer, System.DateOnly value, JsonSerializerOptions options)
73+
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
7474
{
7575
#if NET8_0_OR_GREATER
7676
Span<byte> buffer = stackalloc byte[FormatLength];
@@ -84,7 +84,7 @@ public override void Write(Utf8JsonWriter writer, System.DateOnly value, JsonSer
8484
}
8585

8686
/// <inheritdoc />
87-
public override void WriteAsPropertyName(Utf8JsonWriter writer, System.DateOnly value, JsonSerializerOptions options)
87+
public override void WriteAsPropertyName(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
8888
{
8989
#if NET8_0_OR_GREATER
9090
Span<byte> buffer = stackalloc byte[FormatLength];

DateTimeOnly.Json/Helpers/JsonHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static bool TryParseAsIso(ReadOnlySpan<byte> source, out DateOnly value)
4242
parseData.IsCalendarDateOnly &&
4343
TryCreateDateTime(parseData, DateTimeKind.Unspecified, out DateTime dateTime))
4444
{
45-
value = System.DateOnly.FromDateTime(dateTime);
45+
value = DateOnly.FromDateTime(dateTime);
4646
return true;
4747
}
4848

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ namespace System.Text.Json;
77
/// <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library source code generators.
88
/// </summary>
99
[JsonConverter(typeof(JsonDateOnlyConverter))]
10-
public readonly record struct DateOnly
10+
public readonly record struct JsonDateOnly
1111
{
12-
private readonly System.DateOnly _value;
12+
private readonly DateOnly _value;
1313

14-
private DateOnly(System.DateOnly value) => _value = value;
14+
private JsonDateOnly(DateOnly value) => _value = value;
1515

1616
/// <inheritdoc />
1717
public override string ToString() => _value.ToString();
@@ -21,12 +21,12 @@ public readonly record struct DateOnly
2121
/// </summary>
2222
/// <param name="value">A <see cref="DateOnly"/> struct instance.</param>
2323
/// <returns>A <see cref="System.DateOnly"/> struct instance.</returns>
24-
public static implicit operator System.DateOnly(DateOnly value) => value._value;
24+
public static implicit operator DateOnly(JsonDateOnly value) => value._value;
2525

2626
/// <summary>
2727
/// Implicitly converts a <paramref name="value"/> into a <see cref="DateOnly"/> struct instance.
2828
/// </summary>
2929
/// <param name="value">A <see cref="System.DateOnly"/> struct instance.</param>
3030
/// <returns>A <see cref="DateOnly"/> struct instance.</returns>
31-
public static implicit operator DateOnly(System.DateOnly value) => new(value);
31+
public static implicit operator JsonDateOnly(DateOnly value) => new(value);
3232
}

DateTimeOnly.Json/JsonDateOnlyConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace System.Text.Json;
44

55
/// <summary>
6-
/// Helper attribute for the proper <see cref="DateOnly"/> struct instance handling by the
6+
/// Helper attribute for the proper <see cref="JsonDateOnly"/> struct instance handling by the
77
/// <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library.
88
/// </summary>
9-
public sealed class JsonDateOnlyConverter : JsonConverter<DateOnly>
9+
public sealed class JsonDateOnlyConverter : JsonConverter<JsonDateOnly>
1010
{
1111
private static readonly DateOnlyConverter DateOnlyConverter = new ();
1212

1313
/// <inheritdoc />
14-
public override DateOnly Read(
14+
public override JsonDateOnly Read(
1515
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
1616
DateOnlyConverter.Read(ref reader, typeToConvert, options);
1717

1818
/// <inheritdoc />
1919
public override void Write(
20-
Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) =>
20+
Utf8JsonWriter writer, JsonDateOnly value, JsonSerializerOptions options) =>
2121
DateOnlyConverter.Write(writer, value, options);
2222
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ namespace System.Text.Json;
77
/// <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library source code generators.
88
/// </summary>
99
[JsonConverter(typeof(JsonTimeOnlyConverter))]
10-
public readonly record struct TimeOnly
10+
public readonly record struct JsonTimeOnly
1111
{
12-
private readonly System.TimeOnly _value;
12+
private readonly TimeOnly _value;
1313

14-
private TimeOnly(System.TimeOnly value) => _value = value;
14+
private JsonTimeOnly(TimeOnly value) => _value = value;
1515

1616
/// <inheritdoc />
1717
public override string ToString() => _value.ToString();
@@ -21,12 +21,12 @@ public readonly record struct TimeOnly
2121
/// </summary>
2222
/// <param name="value">A <see cref="TimeOnly"/> struct instance.</param>
2323
/// <returns>A <see cref="System.TimeOnly"/> struct instance.</returns>
24-
public static implicit operator System.TimeOnly(TimeOnly value) => value._value;
24+
public static implicit operator TimeOnly(JsonTimeOnly value) => value._value;
2525

2626
/// <summary>
2727
/// Implicitly converts a <paramref name="value"/> into a <see cref="TimeOnly"/> struct instance.
2828
/// </summary>
2929
/// <param name="value">A <see cref="System.TimeOnly"/> struct instance.</param>
3030
/// <returns>A <see cref="TimeOnly"/> struct instance.</returns>
31-
public static implicit operator TimeOnly(System.TimeOnly value) => new(value);
31+
public static implicit operator JsonTimeOnly(TimeOnly value) => new(value);
3232
}

DateTimeOnly.Json/JsonTimeOnlyConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace System.Text.Json;
44

55
/// <summary>
6-
/// Helper attribute for the proper <see cref="TimeOnly"/> struct instance handling by the
6+
/// Helper attribute for the proper <see cref="JsonTimeOnly"/> struct instance handling by the
77
/// <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library.
88
/// </summary>
9-
public sealed class JsonTimeOnlyConverter : JsonConverter<TimeOnly>
9+
public sealed class JsonTimeOnlyConverter : JsonConverter<JsonTimeOnly>
1010
{
1111
private static readonly TimeOnlyConverter TimeOnlyConverter = new ();
1212

1313
/// <inheritdoc />
14-
public override TimeOnly Read(
14+
public override JsonTimeOnly Read(
1515
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
1616
TimeOnlyConverter.Read(ref reader, typeToConvert, options);
1717

1818
/// <inheritdoc />
1919
public override void Write(
20-
Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) =>
20+
Utf8JsonWriter writer, JsonTimeOnly value, JsonSerializerOptions options) =>
2121
TimeOnlyConverter.Write(writer, value, options);
2222
}

DateTimeOnly.Json/TimeOnlyConverter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
namespace System.Text.Json;
1111

1212
/// <summary>
13-
/// Custom converter for handling the <see cref="System.TimeOnly"/> data type with the <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library.
13+
/// Custom converter for handling the <see cref="TimeOnly"/> data type with the <see href="https://docs.microsoft.com/dotnet/api/system.text.json">System.Text.Json</see> library.
1414
/// </summary>
1515
/// <remarks>
1616
/// This class backported from:
1717
/// <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeOnlyConverter.cs">
1818
/// System.Text.Json.Serialization.Converters.TimeOnlyConverter</see>
1919
/// </remarks>
20-
public sealed class TimeOnlyConverter : JsonConverter<System.TimeOnly>
20+
public sealed class TimeOnlyConverter : JsonConverter<TimeOnly>
2121
{
2222
private const int MinimumTimeOnlyFormatLength = 8; // hh:mm:ss
2323
// ReSharper disable once CommentTypo
2424
private const int MaximumTimeOnlyFormatLength = 16; // hh:mm:ss.fffffff
2525
private const int MaximumEscapedTimeOnlyFormatLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumTimeOnlyFormatLength;
2626

2727
/// <inheritdoc />
28-
public override System.TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
28+
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2929
{
3030
if (reader.TokenType != JsonTokenType.String)
3131
{
@@ -77,12 +77,12 @@ private static TimeOnly ReadCore(ref Utf8JsonReader reader)
7777
ThrowHelper.ThrowFormatException(DataType.TimeOnly);
7878
}
7979

80-
Debug.Assert(System.TimeOnly.MinValue.ToTimeSpan() <= timespan && timespan <= System.TimeOnly.MaxValue.ToTimeSpan());
81-
return System.TimeOnly.FromTimeSpan(timespan);
80+
Debug.Assert(TimeOnly.MinValue.ToTimeSpan() <= timespan && timespan <= TimeOnly.MaxValue.ToTimeSpan());
81+
return TimeOnly.FromTimeSpan(timespan);
8282
}
8383

8484
/// <inheritdoc />
85-
public override void Write(Utf8JsonWriter writer, System.TimeOnly value, JsonSerializerOptions options)
85+
public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
8686
{
8787
Span<byte> output = stackalloc byte[MaximumTimeOnlyFormatLength];
8888

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
namespace System.Tests;
1+
using System.Text.Json;
2+
3+
namespace System.Tests;
24

35
public class ClassWithDateOnlyAndTimeOnlyValues
46
{
5-
public Text.Json.DateOnly DateOnly { get; set; }
7+
public JsonDateOnly DateOnly { get; set; }
68

7-
public Text.Json.DateOnly? NullableDateOnly { get; set; }
9+
public JsonDateOnly? NullableDateOnly { get; set; }
810

9-
public Text.Json.TimeOnly TimeOnly { get; set; }
11+
public JsonTimeOnly TimeOnly { get; set; }
1012

11-
public Text.Json.TimeOnly? NullableTimeOnly { get; set; }
13+
public JsonTimeOnly? NullableTimeOnly { get; set; }
1214
}

0 commit comments

Comments
 (0)