-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SystemTextJson: Support automatic TypeSafeEnum/Union converter select…
…ion (#69)
- Loading branch information
Showing
10 changed files
with
109 additions
and
74 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
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
22 changes: 22 additions & 0 deletions
22
src/FsCodec.SystemTextJson/UnionOrTypeSafeEnumConverterFactory.fs
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,22 @@ | ||
namespace FsCodec.SystemTextJson | ||
|
||
open System | ||
open System.Linq.Expressions | ||
open System.Text.Json.Serialization | ||
|
||
type internal ConverterActivator = delegate of unit -> JsonConverter | ||
|
||
type UnionOrTypeSafeEnumConverterFactory() = | ||
inherit JsonConverterFactory() | ||
|
||
override _.CanConvert(t : Type) = | ||
Union.isUnion t | ||
|
||
override _.CreateConverter(typ, _options) = | ||
let openConverterType = if Union.hasOnlyNullaryCases typ then typedefof<TypeSafeEnumConverter<_>> else typedefof<UnionConverter<_>> | ||
let constructor = openConverterType.MakeGenericType(typ).GetConstructors() |> Array.head | ||
let newExpression = Expression.New(constructor) | ||
let lambda = Expression.Lambda(typeof<ConverterActivator>, newExpression) | ||
|
||
let activator = lambda.Compile() :?> ConverterActivator | ||
activator.Invoke() |
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,24 @@ | ||
module FsCodec.SystemTextJson.Tests.AutoUnionTests | ||
|
||
open FsCodec.SystemTextJson | ||
open Swensen.Unquote | ||
|
||
type ATypeSafeEnum = A | B | C | ||
type NotAUnion = { body : string } | ||
type AUnion = D of value : string | E of ATypeSafeEnum | F | ||
type Any = Tse of enum : ATypeSafeEnum | Not of NotAUnion | Union of AUnion | ||
|
||
let serdes = Options.Create(autoUnion = true) |> Serdes | ||
|
||
let [<Xunit.Fact>] ``Basic characteristics`` () = | ||
test <@ "\"B\"" = serdes.Serialize B @> | ||
test <@ "{\"body\":\"A\"}" = serdes.Serialize { body = "A" } @> | ||
test <@ "{\"case\":\"D\",\"value\":\"A\"}" = serdes.Serialize (D "A") @> | ||
test <@ "{\"case\":\"Tse\",\"enum\":\"B\"}" = serdes.Serialize (Tse B) @> | ||
test <@ Tse B = serdes.Deserialize "{\"case\":\"Tse\",\"enum\":\"B\"}" @> | ||
test <@ Not { body = "A" } = serdes.Deserialize "{\"case\":\"Not\",\"body\":\"A\"}" @> | ||
|
||
let [<FsCheck.Xunit.Property>] ``auto-encodes Unions and non-unions`` (x : Any) = | ||
let encoded = serdes.Serialize x | ||
let decoded : Any = serdes.Deserialize encoded | ||
test <@ decoded = x @> |
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