Skip to content

Commit

Permalink
additional odds formats
Browse files Browse the repository at this point in the history
  • Loading branch information
zb-sr committed Nov 21, 2024
1 parent 29c9a76 commit 0c5b5ab
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/Sportradar.Mbs.Sdk/Entities/Odds/FractionalOdds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text.Json.Serialization;
using Sportradar.Mbs.Sdk.Internal.Utils;

namespace Sportradar.Mbs.Sdk.Entities.Odds;

/// <summary>
/// Represents fractional odds.
/// </summary>
public class FractionalOdds : OddsBase
{
[JsonInclude]
[JsonPropertyName("type")]
private string Type => "fractional";

/// <summary>
/// Gets or sets the value of the numerator (top, first part) of the fractional odds.
/// </summary>
[JsonConverter(typeof(LongJsonConverter))]
[JsonPropertyName("numerator")]
public long? Numerator { get; set; }

/// <summary>
/// Gets or sets the value of the denominator (bottom, last part) of the fractional odds.
/// </summary>
[JsonConverter(typeof(LongJsonConverter))]
[JsonPropertyName("denominator")]
public long? Denominator { get; set; }
}
21 changes: 21 additions & 0 deletions src/Sportradar.Mbs.Sdk/Entities/Odds/HongKongOdds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
using Sportradar.Mbs.Sdk.Internal.Utils;

namespace Sportradar.Mbs.Sdk.Entities.Odds;

/// <summary>
/// Represents hong kong odds.
/// </summary>
public class HongKongOdds : OddsBase
{
[JsonInclude]
[JsonPropertyName("type")]
private string Type => "hong-kong";

/// <summary>
/// Gets or sets the hong kong value of the odds: eg "0.75".
/// </summary>
[JsonConverter(typeof(DecimalJsonConverter))]
[JsonPropertyName("value")]
public decimal? Value { get; set; }
}
21 changes: 21 additions & 0 deletions src/Sportradar.Mbs.Sdk/Entities/Odds/IndonesianOdds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
using Sportradar.Mbs.Sdk.Internal.Utils;

namespace Sportradar.Mbs.Sdk.Entities.Odds;

/// <summary>
/// Represents indonesian odds.
/// </summary>
public class IndonesianOdds : OddsBase
{
[JsonInclude]
[JsonPropertyName("type")]
private string Type => "indonesian";

/// <summary>
/// Gets or sets the indonesian value of the odds: eg "-3.4".
/// </summary>
[JsonConverter(typeof(DecimalJsonConverter))]
[JsonPropertyName("value")]
public decimal? Value { get; set; }
}
21 changes: 21 additions & 0 deletions src/Sportradar.Mbs.Sdk/Entities/Odds/MalayOdds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
using Sportradar.Mbs.Sdk.Internal.Utils;

namespace Sportradar.Mbs.Sdk.Entities.Odds;

/// <summary>
/// Represents malay odds.
/// </summary>
public class MalayOdds : OddsBase
{
[JsonInclude]
[JsonPropertyName("type")]
private string Type => "malay";

/// <summary>
/// Gets or sets the malay value of the odds: eg "0.75".
/// </summary>
[JsonConverter(typeof(DecimalJsonConverter))]
[JsonPropertyName("value")]
public decimal? Value { get; set; }
}
21 changes: 21 additions & 0 deletions src/Sportradar.Mbs.Sdk/Entities/Odds/MoneylineOdds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
using Sportradar.Mbs.Sdk.Internal.Utils;

namespace Sportradar.Mbs.Sdk.Entities.Odds;

/// <summary>
/// Represents moneyline odds.
/// </summary>
public class MoneylineOdds : OddsBase
{
[JsonInclude]
[JsonPropertyName("type")]
private string Type => "moneyline";

/// <summary>
/// Gets or sets the moneyline value of the odds: eg "-340".
/// </summary>
[JsonConverter(typeof(LongJsonConverter))]
[JsonPropertyName("value")]
public long? Value { get; set; }
}
5 changes: 5 additions & 0 deletions src/Sportradar.Mbs.Sdk/Entities/Odds/OddsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public override OddsBase Read(ref Utf8JsonReader reader, Type typeToConvert, Jso

OddsBase? result = type switch
{
"indonesian" => JsonSerializer.Deserialize<IndonesianOdds>(root.GetRawText()),
"hong-kong" => JsonSerializer.Deserialize<HongKongOdds>(root.GetRawText()),
"fractional" => JsonSerializer.Deserialize<FractionalOdds>(root.GetRawText()),
"decimal" => JsonSerializer.Deserialize<DecimalOdds>(root.GetRawText()),
"moneyline" => JsonSerializer.Deserialize<MoneylineOdds>(root.GetRawText()),
"malay" => JsonSerializer.Deserialize<MalayOdds>(root.GetRawText()),
_ => throw new JsonException("Unknown type of OddsBase: " + type)
};
return result ?? throw new NullReferenceException("Null OddsBase: " + type);
Expand Down
22 changes: 22 additions & 0 deletions src/Sportradar.Mbs.Sdk/Internal/Utils/LongJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Sportradar.Mbs.Sdk.Internal.Utils;

internal class LongJsonConverter : JsonConverter<long>
{
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonVal = reader.GetString();
if (long.TryParse(jsonVal, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result)) return result;

throw new JsonException("Unknown long: " + jsonVal);
}

public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
{
var jsonVal = value.ToString(CultureInfo.InvariantCulture);
writer.WriteStringValue(jsonVal);
}
}
2 changes: 1 addition & 1 deletion src/Sportradar.Mbs.Sdk/Sportradar.Mbs.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.0"/>
<PackageReference Include="System.Text.Json" Version="8.0.5"/>
<PackageReference Include="System.Threading.Channels" Version="8.0.0"/>
</ItemGroup>

Expand Down

0 comments on commit 0c5b5ab

Please sign in to comment.