Skip to content

Commit

Permalink
Core: fix decimal serialization
Browse files Browse the repository at this point in the history
Fix serialization of decimals with no fractional part, that
caused test failures.
  • Loading branch information
webwarrior-ws committed Feb 12, 2024
1 parent 24f3400 commit 5845515
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/FX.Core/RedisStorageLayer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,27 @@ open StackExchange.Redis

[<AutoOpen>]
module Serialization =
let serializationOptions = JsonFSharpOptions.Default().ToJsonSerializerOptions()
type DecimalTypeConverter() =
inherit JsonConverter<decimal>()

override this.Read(reader, _typeToConvert, _options) =
reader.GetDecimal()

override this.Write(writer, value, _options ) =
// Values such as 0m and 0.0m are equal, but have different string representations,
// which may cause problems when comparing serialized values.
// To avoid this, add 0.0m to value before writing, so all decimals will be serilized with fractional
// part, even if it's 0.
// For more info, see https://colinmackay.scot/2021/07/03/why-is-there-a-difference-between-decimal-0-and-0-0/
writer.WriteNumberValue (value + 0.0m)

let serializationOptions =
let options =
JsonFSharpOptions
.Default()
.ToJsonSerializerOptions()
options.Converters.Add(DecimalTypeConverter())
options

type OrderQuery =
{
Expand Down

0 comments on commit 5845515

Please sign in to comment.