Skip to content

Commit

Permalink
Core: add json converters for F# types
Browse files Browse the repository at this point in the history
Implement JSON converters for F# Discriminated Union types
because System.Text.Json can't serialize them out of the box.
  • Loading branch information
webwarrior-ws committed Feb 14, 2024
1 parent 2356027 commit 7a06249
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/FX.Core/RedisStorageLayer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,38 @@ open System
open FsharpExchangeDotNetStandard

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

[<AutoOpen>]
module Serialization =
let serializationOptions = JsonSerializerOptions.Default
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 =
{
Expand Down

0 comments on commit 7a06249

Please sign in to comment.