Skip to content

Commit

Permalink
Merge PR #21 from webwarrior-ws/system-text-json
Browse files Browse the repository at this point in the history
Replace Newtonsoft.Json with System.Text.Json.
  • Loading branch information
knocte authored Feb 15, 2024
2 parents 33ce6fa + 055624e commit df90233
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="StackExchange.Redis" Version="2.0.513" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="NUnit" Version="3.9.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageVersion Include="Microsoft.AspNetCore.App" Version="2.1.34" />
<PackageVersion Include="Giraffe" Version="3.1.0" />
<PackageVersion Include="Giraffe.Razor" Version="1.3.0" />
<PackageVersion Include="Microsoft.AspNetCore.WebSockets" Version="2.1.1" />
<PackageVersion Include="System.Text.Json" Version="8.0.1" />
<PackageVersion Include="TaskBuilder.fs" Version="2.1.0" />
<PackageVersion Include="FSharp.Core" Version="8.0.101" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/FX.Core/FX.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="StackExchange.Redis" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>
</Project>
49 changes: 41 additions & 8 deletions src/FX.Core/RedisStorageLayer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,42 @@ open System

open FsharpExchangeDotNetStandard

open Newtonsoft.Json
open System.Text.Json
open System.Text.Json.Serialization
open StackExchange.Redis

[<AutoOpen>]
module Serialization =
// TODO: use FSharp.SystemTextJson for Discriminated Union support,
// in that case custom type converters are no longer needed
type SideTypeConverter() =
inherit JsonConverter<Side>()

override this.Read(reader, _typeToConvert, _options) =
reader.GetString() |> Side.Parse

override this.Write(writer, value, _options ) =
writer.WriteStringValue(value.ToString())

type CurrencyTypeConverter() =
inherit JsonConverter<Currency>()

override this.Read(reader, _typeToConvert, _options) =
match reader.GetString() with
| "BTC" -> BTC
| "USD" -> USD
| unknownCurrency -> failwithf "Unknown currency: %s" unknownCurrency

override this.Write(writer, value, _options ) =
writer.WriteStringValue(value.ToString())

let serializationOptions =
let options = JsonSerializerOptions()
options.Converters.Add(SideTypeConverter())
options.Converters.Add(CurrencyTypeConverter())
options


type OrderQuery =
{
OrderId: string;
Expand All @@ -26,9 +59,9 @@ type Query =

type OrderBookSide(market: Market, side: Side) =
let tipQuery = { Market = market; Tip = true; Side = side }
let tipQueryStr = JsonConvert.SerializeObject tipQuery
let tipQueryStr = JsonSerializer.Serialize(tipQuery, serializationOptions)
let tailQuery = { Market = market; Tip = false; Side = side }
let tailQueryStr = JsonConvert.SerializeObject tailQuery
let tailQueryStr = JsonSerializer.Serialize(tailQuery, serializationOptions)
member __.TipQuery = tipQueryStr
member __.TailQuery = tailQueryStr

Expand All @@ -53,7 +86,7 @@ module OrderRedisManager =
db.CreateTransaction()

let InsertOrder (limitOrder: LimitOrder): unit =
let serializedOrder = JsonConvert.SerializeObject limitOrder
let serializedOrder = JsonSerializer.Serialize(limitOrder, serializationOptions)
let success = db.StringSet(RedisKey.op_Implicit (limitOrder.OrderInfo.Id.ToString()),
RedisValue.op_Implicit (serializedOrder))
if not success then
Expand Down Expand Up @@ -89,7 +122,7 @@ module OrderRedisManager =
let orderSerialized = db.StringGet (RedisKey.op_Implicit tipOrderGuid)
if not orderSerialized.HasValue then
failwithf "Something went wrong, order tip was %s but was not found" tipOrderGuid
let tipOrder = JsonConvert.DeserializeObject<LimitOrder> (orderSerialized.ToString())
let tipOrder = JsonSerializer.Deserialize<LimitOrder>(orderSerialized.ToString(), serializationOptions)
tipOrder |> Some

let private GetOrderSerialized (guidStr: string): Option<string> =
Expand All @@ -111,7 +144,7 @@ module OrderRedisManager =
match maybeOrderSerialized with
| None -> None
| Some orderSerialized ->
let order = JsonConvert.DeserializeObject<LimitOrder> (orderSerialized.ToString())
let order = JsonSerializer.Deserialize<LimitOrder>(orderSerialized.ToString(), serializationOptions)
order |> Some

let GetOrderByGuid (guid: Guid): Option<LimitOrder> =
Expand All @@ -122,13 +155,13 @@ module OrderRedisManager =
if not tail.HasValue then
List.empty
else
JsonConvert.DeserializeObject<List<string>> (tail.ToString())
JsonSerializer.Deserialize<List<string>>(tail.ToString(), serializationOptions)

let SetTail (transaction: StackExchange.Redis.ITransaction)
(limitOrderGuids: List<string>)
(orderBookSide: OrderBookSide)
: unit =
let serializedGuids = JsonConvert.SerializeObject limitOrderGuids
let serializedGuids = JsonSerializer.Serialize(limitOrderGuids, serializationOptions)
transaction.StringSetAsync(RedisKey.op_Implicit orderBookSide.TailQuery,
RedisValue.op_Implicit serializedGuids)
|> ignore
Expand Down
2 changes: 1 addition & 1 deletion src/FX.Tests/FX.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="StackExchange.Redis" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="FSharp.Core" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Class1.cs" />
Expand Down
26 changes: 13 additions & 13 deletions src/FX.Tests/RedisIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using FsharpExchangeDotNetStandard;
using FsharpExchangeDotNetStandard.Redis;

using Newtonsoft.Json;
using System.Text.Json;
using NUnit.Framework;
using StackExchange.Redis;

Expand Down Expand Up @@ -33,8 +33,8 @@ private Exchange CreateExchangeAndSendFirstLimitOrder

var tipQuery = new MarketQuery(market, limitOrder.OrderInfo.Side, true);

//e.g. {"Market":{"BuyCurrency":{"Case":"BTC"},"SellCurrency":{"Case":"USD"}},"Side":{"Case":"Buy"},"Tip":true}"
string tipQueryStr = JsonConvert.SerializeObject(tipQuery);
//e.g. {"Market":{"BuyCurrency":"BTC","SellCurrency":"USD"},"Side":"Bid","Tip":true}"
string tipQueryStr = JsonSerializer.Serialize(tipQuery, Serialization.serializationOptions);

using (var redis = ConnectionMultiplexer.Connect("localhost"))
{
Expand Down Expand Up @@ -76,7 +76,7 @@ private Exchange CreateExchangeAndSendFirstLimitOrder
"should have the order content(not null)");

var limitOrderSerialized =
JsonConvert.SerializeObject(limitOrder);
JsonSerializer.Serialize(limitOrder, Serialization.serializationOptions);
Assert.That(order.ToString(),
Is.EqualTo(limitOrderSerialized),
"received order should have same content");
Expand Down Expand Up @@ -129,8 +129,8 @@ public void SendingSecondAndThirdLimitOrderMakesNonTipQueryWorkAfter()

var nonTipQuery = new MarketQuery(market, side, false);

//e.g. {"Market":{"BuyCurrency":{"Case":"BTC"},"SellCurrency":{"Case":"USD"}},"Side":{"Case":"Buy"},"Tip":true}"
string nontipQueryStr = JsonConvert.SerializeObject(nonTipQuery);
//e.g. {"Market":{"BuyCurrency":"BTC","SellCurrency":"USD"},"Side":"Bid","Tip":false}"
string nontipQueryStr = JsonSerializer.Serialize(nonTipQuery, Serialization.serializationOptions);

using (var redis = ConnectionMultiplexer.Connect("localhost"))
{
Expand All @@ -139,7 +139,7 @@ public void SendingSecondAndThirdLimitOrderMakesNonTipQueryWorkAfter()
var values = db.StringGet(nontipQueryStr);
Assert.That(String.IsNullOrEmpty(values), Is.False,
"should have nontip tail(not null) in this market");
var orders = JsonConvert.DeserializeObject<List<string>>(values);
var orders = JsonSerializer.Deserialize<List<string>>(values, Serialization.serializationOptions);
Assert.That(orders.Count, Is.EqualTo(2),
"should have nontip tail of 2 elements in this market now");

Expand All @@ -156,7 +156,7 @@ public void SendingSecondAndThirdLimitOrderMakesNonTipQueryWorkAfter()
Assert.That(order2.IsNull, Is.EqualTo(false),
"should have the second order content(not null)");
var secondLimitOrderSerialized =
JsonConvert.SerializeObject(secondLimitOrder);
JsonSerializer.Serialize(secondLimitOrder, Serialization.serializationOptions);
Assert.That(order2.ToString(),
Is.EqualTo(secondLimitOrderSerialized),
"received second order should have same content");
Expand All @@ -167,7 +167,7 @@ public void SendingSecondAndThirdLimitOrderMakesNonTipQueryWorkAfter()
Assert.That(order3.IsNull, Is.EqualTo(false),
"should have the third order content(not null)");
var thirdLimitOrderSerialized =
JsonConvert.SerializeObject(thirdLimitOrder);
JsonSerializer.Serialize(thirdLimitOrder, Serialization.serializationOptions);
Assert.That(order3.ToString(),
Is.EqualTo(thirdLimitOrderSerialized),
"received second order should have same content");
Expand Down Expand Up @@ -198,8 +198,8 @@ public void TipIsReplaced()

var nonTipQuery = new MarketQuery(market, side, false);

//e.g. {"Market":{"BuyCurrency":{"Case":"BTC"},"SellCurrency":{"Case":"USD"}},"Side":{"Case":"Buy"},"Tip":true}"
string nontipQueryStr = JsonConvert.SerializeObject(nonTipQuery);
//e.g. {"Market":{"BuyCurrency":"BTC","SellCurrency":"USD"},"Side":"Bid","Tip":false}"
string nontipQueryStr = JsonSerializer.Serialize(nonTipQuery, Serialization.serializationOptions);

using (var redis = ConnectionMultiplexer.Connect("localhost"))
{
Expand All @@ -208,7 +208,7 @@ public void TipIsReplaced()
var values = db.StringGet(nontipQueryStr);
Assert.That(String.IsNullOrEmpty(values), Is.False,
"should have nontip tail(not null) in this market");
var orders = JsonConvert.DeserializeObject<List<string>>(values);
var orders = JsonSerializer.Deserialize<List<string>>(values, Serialization.serializationOptions);
Assert.That(orders.Count, Is.EqualTo(1),
"should have nontip tail of 2 elements in this market now");

Expand All @@ -222,7 +222,7 @@ public void TipIsReplaced()
Assert.That(theOrder.IsNull, Is.EqualTo(false),
"should have the second order content(not null)");
var firstLimitOrderSerialized =
JsonConvert.SerializeObject(firstLimitOrder);
JsonSerializer.Serialize(firstLimitOrder, Serialization.serializationOptions);
Assert.That(theOrder.ToString(),
Is.EqualTo(firstLimitOrderSerialized),
"received second order should have same content");
Expand Down

0 comments on commit df90233

Please sign in to comment.