-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change types and add converters for values
- Loading branch information
1 parent
ae01338
commit 62aa640
Showing
4 changed files
with
164 additions
and
24 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
33 changes: 33 additions & 0 deletions
33
src/Aydsko.iRacingData/Converters/OneDecimalPointValueConverter.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,33 @@ | ||
using System.Text.Json; | ||
|
||
namespace Aydsko.iRacingData.Converters; | ||
|
||
/// <summary> | ||
/// Convert the cloud cover value from the API to a decimal. | ||
/// </summary> | ||
public class OneDecimalPointValueConverter : JsonConverter<decimal> | ||
{ | ||
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TryGetInt32(out var intValue)) | ||
{ | ||
return intValue / 10m; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
ArgumentNullException.ThrowIfNull(writer); | ||
#else | ||
if (writer is null) | ||
{ | ||
throw new ArgumentNullException(nameof(writer)); | ||
} | ||
#endif | ||
|
||
writer.WriteNumberValue(value * 10); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Aydsko.iRacingData/Converters/TwoDecimalPointsValueConverter.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,33 @@ | ||
using System.Text.Json; | ||
|
||
namespace Aydsko.iRacingData.Converters; | ||
|
||
/// <summary> | ||
/// Convert the temperature value from the API to a decimal. | ||
/// </summary> | ||
public class TwoDecimalPointsValueConverter : JsonConverter<decimal> | ||
{ | ||
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TryGetInt32(out var intValue)) | ||
{ | ||
return intValue / 100m; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
ArgumentNullException.ThrowIfNull(writer); | ||
#else | ||
if (writer is null) | ||
{ | ||
throw new ArgumentNullException(nameof(writer)); | ||
} | ||
#endif | ||
|
||
writer.WriteNumberValue(value * 100); | ||
} | ||
} |
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