-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core,GrpcModels: Match & Option json converters
Implemented json converters for Match and Option<'T> types. Match type converter is placed in GrpcModels.ModelSerialization since it's not used in Redis, but only in GRPC.
- Loading branch information
1 parent
90fe289
commit 7b4e449
Showing
4 changed files
with
86 additions
and
2 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
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
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
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,54 @@ | ||
namespace GrpcModels | ||
|
||
open System.Text.Json | ||
open System.Text.Json.Serialization | ||
|
||
open FsharpExchangeDotNetStandard | ||
|
||
module ModelSerialization = | ||
type MatchTypeConverter() = | ||
inherit JsonConverter<Match>() | ||
|
||
override this.Read(reader, _typeToConvert, _options) = | ||
if reader.TokenType <> JsonTokenType.StartObject then | ||
raise <| JsonException() | ||
|
||
// "Type" key | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.PropertyName || reader.GetString() <> "Type" then | ||
raise <| JsonException() | ||
// "Type" value | ||
reader.Read() |> ignore | ||
match reader.GetString() with | ||
| "Full" -> | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.EndObject then | ||
raise <| JsonException() | ||
Match.Full | ||
| "Partial" -> | ||
// "Amount" key | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.PropertyName || reader.GetString() <> "Amount" then | ||
raise <| JsonException() | ||
// "Amount" value | ||
let amount = reader.GetDecimal() | ||
reader.Read() |> ignore | ||
if reader.TokenType <> JsonTokenType.EndObject then | ||
raise <| JsonException() | ||
Match.Partial amount | ||
| typeName -> raise <| JsonException("Unknown Match type: " + typeName) | ||
|
||
override this.Write(writer, value, _options ) = | ||
writer.WriteStartObject() | ||
match value with | ||
| Full -> | ||
writer.WriteString("Type", "Full") | ||
| Partial amount -> | ||
writer.WriteString("Type", "Partial") | ||
writer.WriteNumber("Amount", amount) | ||
writer.WriteEndObject() | ||
|
||
let serializationOptions = | ||
let options = JsonSerializerOptions(Redis.Serialization.serializationOptions) | ||
options.Converters.Add(MatchTypeConverter()) | ||
options |