-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use invariant culture for (de)serialization
- Loading branch information
Showing
7 changed files
with
311 additions
and
12 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
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,29 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Kiota.Abstractions; | ||
|
||
namespace Microsoft.Kiota.Serialization.Json.Tests.Converters; | ||
|
||
/// <summary> | ||
/// Converts a Date object or value to/from JSON. | ||
/// </summary> | ||
public class JsonDateConverter : JsonConverter<Date> | ||
{ | ||
/// <inheritdoc /> | ||
public override Date Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
=> reader.TokenType == JsonTokenType.Null | ||
? new Date() | ||
: ReadInternal(ref reader); | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, Date value, JsonSerializerOptions options) | ||
=> WriteInternal(writer, value); | ||
|
||
private static Date ReadInternal(ref Utf8JsonReader reader) | ||
=> new Date(DateTime.ParseExact(reader.GetString()!, "dd---MM---yyyy", CultureInfo.InvariantCulture)); | ||
|
||
private static void WriteInternal(Utf8JsonWriter writer, Date value) | ||
=> writer.WriteStringValue($"{value.Day}---{value.Month}---{value.Year}"); | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/serialization/json/Converters/JsonDateTimeOffsetConverter.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,28 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.Kiota.Serialization.Json.Tests.Converters; | ||
|
||
/// <summary> | ||
/// Converts a DateTimeOffset object or value to/from JSON. | ||
/// </summary> | ||
public class JsonDateTimeOffsetConverter : JsonConverter<DateTimeOffset> | ||
{ | ||
/// <inheritdoc /> | ||
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
=> reader.TokenType == JsonTokenType.Null | ||
? new DateTimeOffset() | ||
: ReadInternal(ref reader); | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) | ||
=> WriteInternal(writer, value); | ||
|
||
private static DateTimeOffset ReadInternal(ref Utf8JsonReader reader) | ||
=> DateTimeOffset.ParseExact(reader.GetString()!, "dd__MM__yyyyTHH_mm_ssZ", CultureInfo.InvariantCulture); | ||
|
||
private static void WriteInternal(Utf8JsonWriter writer, DateTimeOffset value) | ||
=> writer.WriteStringValue(value.ToString("dd__MM__yyyyTHH_mm_ssZ")); | ||
} |
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,29 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Kiota.Abstractions; | ||
|
||
namespace Microsoft.Kiota.Serialization.Json.Tests.Converters; | ||
|
||
/// <summary> | ||
/// Converts a Time object or value to/from JSON. | ||
/// </summary> | ||
public class JsonTimeConverter : JsonConverter<Time> | ||
{ | ||
/// <inheritdoc /> | ||
public override Time Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
=> reader.TokenType == JsonTokenType.Null | ||
? new Time() | ||
: ReadInternal(ref reader); | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, Time value, JsonSerializerOptions options) | ||
=> WriteInternal(writer, value); | ||
|
||
private static Time ReadInternal(ref Utf8JsonReader reader) | ||
=> new Time(DateTime.ParseExact(reader.GetString()!, "HH__mm__ss", CultureInfo.InvariantCulture)); | ||
|
||
private static void WriteInternal(Utf8JsonWriter writer, Time value) | ||
=> writer.WriteStringValue($"{value.Hour}__{value.Minute}__{value.Second}"); | ||
} |
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