-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
feadfdf
commit 05a50d8
Showing
6 changed files
with
193 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Globalization; | ||
using System.Numerics; | ||
|
||
namespace System.Text.Json.Serialization; | ||
|
||
/// <summary> | ||
/// Json converter for System.Numerics.BigInteger | ||
/// </summary> | ||
public class BigIntegerJsonConverter : JsonConverter<BigInteger> | ||
{ | ||
/// <inheritdoc /> | ||
public override BigInteger Read(ref Utf8JsonReader reader, Type typeToConvert, | ||
JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType is not (JsonTokenType.Number or JsonTokenType.String)) | ||
throw new JsonException( | ||
$"Found token {reader.TokenType} but expected token {JsonTokenType.Number}"); | ||
|
||
using var doc = JsonDocument.ParseValue(ref reader); | ||
var value = reader.TokenType == JsonTokenType.String | ||
? doc.RootElement.GetString() ?? "0" | ||
: doc.RootElement.GetRawText(); | ||
|
||
var bigInteger = BigInteger.Parse(value, NumberFormatInfo.InvariantInfo); | ||
return bigInteger; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, BigInteger value, | ||
JsonSerializerOptions options) | ||
{ | ||
var result = value.ToString(NumberFormatInfo.InvariantInfo); | ||
writer.WriteRawValue(result); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
tests/CSharpPlus.Tests/JsonConverters/BigIntegerJsonConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Numerics; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CSharpPlus.Tests.JsonConverters; | ||
|
||
using System.Text.Json; | ||
using static System.Text.Json.JsonSerializer; | ||
|
||
public class BigIntegerJsonConverterTests : BaseTest | ||
{ | ||
readonly JsonSerializerOptions options = new() | ||
{ | ||
Converters = | ||
{ | ||
new BigIntegerJsonConverter() | ||
} | ||
}; | ||
|
||
record TestDate(BigInteger Data); | ||
|
||
[Test] | ||
public void ShouldParseLong() | ||
{ | ||
var expected = faker.Random.Long(); | ||
var value = Deserialize<TestDate>($@"{{""Data"": ""{expected}""}}", options)!.Data; | ||
|
||
value.Should().Be(expected); | ||
} | ||
|
||
[Test] | ||
public void ShouldParseBigNum() | ||
{ | ||
var bigNum = faker.Random.ReplaceNumbers(new('#', 100)); | ||
var value = Deserialize<TestDate>($@"{{""Data"": ""{bigNum}""}}", options)!.Data; | ||
|
||
var expected = BigInteger.Parse(bigNum); | ||
value.Should().Be(expected); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
tests/CSharpPlus.Tests/JsonConverters/DateTimeUtcOnlyJsonConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CSharpPlus.Tests.JsonConverters; | ||
|
||
using System.Text.Json; | ||
using static System.Text.Json.JsonSerializer; | ||
|
||
public class DateTimeTests | ||
{ | ||
readonly JsonSerializerOptions options = new() | ||
{ | ||
Converters = | ||
{ | ||
new DateTimeUtcOnlyJsonConverter() | ||
} | ||
}; | ||
|
||
record TestDate(DateTime Data); | ||
|
||
DateTime Parse(string isoDate) => | ||
Deserialize<TestDate>($@"{{""Data"": ""{isoDate}""}}", options)!.Data; | ||
|
||
[Test] | ||
public void ShouldNotChangeOnReadISO8601UtcDate() | ||
{ | ||
var expected = new DateTime(2022, 07, 2, 10, 30, 20, DateTimeKind.Utc); | ||
var isoDate = "2022-07-02T10:30:20Z"; | ||
Parse(isoDate).Should().Be(expected) | ||
.And.Subject!.Value.Kind.Should().Be(DateTimeKind.Utc); | ||
} | ||
|
||
[Test] | ||
public void ShouldSetUTCAndNotChangeOnReadISO8601UnspecifiedKindDate() | ||
{ | ||
var expected = new DateTime(2022, 7, 2, 10, 30, 20, DateTimeKind.Utc); | ||
var isoDate = "2022-07-02T10:30:20"; | ||
|
||
DateTime.Parse(isoDate).Kind.Should().Be(DateTimeKind.Unspecified); | ||
var parsed = Parse(isoDate); | ||
parsed.Should().Be(expected).And.Subject!.Value.Kind.Should().Be(DateTimeKind.Utc); | ||
} | ||
|
||
[Test] | ||
public void ShouldSetUnspecifiedKindUtcConvertingFromTimeZone() | ||
{ | ||
var expected = new DateTime(2022, 7, 2, 10, 30, 20, DateTimeKind.Utc); | ||
var isoDate = "2022-07-02T07:30:20"; | ||
DateTime.Parse(isoDate).Kind.Should().Be(DateTimeKind.Unspecified); | ||
|
||
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("America/Sao_Paulo"); | ||
|
||
var parsed = Deserialize<TestDate>( | ||
$@"{{""Data"": ""{isoDate}""}}", | ||
new JsonSerializerOptions | ||
{ | ||
Converters = | ||
{ | ||
new DateTimeUtcOnlyJsonConverter(timeZone) | ||
} | ||
})!.Data; | ||
|
||
parsed.Should().Be(expected).And.Subject!.Value.Kind.Should().Be(DateTimeKind.Utc); | ||
} | ||
|
||
[Test] | ||
public void ShouldSetUnspecifiedKindUtcConvertingFromOffset() | ||
{ | ||
var expected = new DateTime(2022, 7, 2, 10, 30, 20, DateTimeKind.Utc); | ||
var isoDate = "2022-07-02T13:30:20"; | ||
DateTime.Parse(isoDate).Kind.Should().Be(DateTimeKind.Unspecified); | ||
|
||
var offset = TimeSpan.FromHours(3); | ||
|
||
var parsed = Deserialize<TestDate>( | ||
$@"{{""Data"": ""{isoDate}""}}", | ||
new JsonSerializerOptions | ||
{ | ||
Converters = | ||
{ | ||
new DateTimeUtcOnlyJsonConverter(offset) | ||
} | ||
})!.Data; | ||
|
||
parsed.Should().Be(expected).And.Subject!.Value.Kind.Should().Be(DateTimeKind.Utc); | ||
} | ||
|
||
[Test] | ||
public void ShouldChangeLocalDateToUtc() | ||
{ | ||
var expected = new DateTime(2022, 7, 2, 10, 30, 20, DateTimeKind.Utc); | ||
var isoDate = "2022-07-02T07:30:20-03:00"; | ||
|
||
DateTime.Parse(isoDate).Kind.Should().Be(DateTimeKind.Local); | ||
var parsed = Parse(isoDate); | ||
parsed.Should().Be(expected).And.Subject!.Value.Kind.Should().Be(DateTimeKind.Utc); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.