|
| 1 | +module FsCodec.SystemTextJson.Tests.StringIdTests |
| 2 | + |
| 3 | +open System.Collections.Generic |
| 4 | +open FsCodec.SystemTextJson |
| 5 | +open Xunit |
| 6 | +open Swensen.Unquote |
| 7 | + |
| 8 | +(* Recommended helper aliases to put in your namespace global to avoid having to open long namespaces *) |
| 9 | + |
| 10 | +type StjNameAttribute = System.Text.Json.Serialization.JsonPropertyNameAttribute |
| 11 | +type StjIgnoreAttribute = System.Text.Json.Serialization.JsonIgnoreAttribute |
| 12 | +type StjConverterAttribute = System.Text.Json.Serialization.JsonConverterAttribute |
| 13 | + |
| 14 | +module Guid = |
| 15 | + |
| 16 | + let inline gen () = System.Guid.NewGuid() |
| 17 | + let inline toStringN (x: System.Guid) = x.ToString "N" |
| 18 | + let inline parse (x: string) = System.Guid.Parse x |
| 19 | + |
| 20 | +module Bare = |
| 21 | + |
| 22 | + [<Sealed; AutoSerializable false; StjConverter(typeof<SkuIdConverter>)>] |
| 23 | + type SkuId(value: System.Guid) = |
| 24 | + // No JSON Ignore attribute required as read-only property |
| 25 | + member val Value = value |
| 26 | + and private SkuIdConverter() = |
| 27 | + inherit JsonIsomorphism<SkuId, string>() |
| 28 | + override _.Pickle(value: SkuId) = value.Value |> Guid.toStringN |
| 29 | + override _.UnPickle input = input |> Guid.parse |> SkuId |
| 30 | + |
| 31 | + [<Fact>] |
| 32 | + let comparison () = |
| 33 | + let g = Guid.gen () |
| 34 | + let id1, id2 = SkuId g, SkuId g |
| 35 | + false =! id1.Equals id2 |
| 36 | + id1 <>! id2 |
| 37 | + |
| 38 | + [<Fact>] |
| 39 | + let serdes () = |
| 40 | + let x = Guid.gen () |> SkuId |
| 41 | + $"\"{Guid.toStringN x.Value}\"" =! Serdes.Default.Serialize x |
| 42 | + let ser = Serdes.Default.Serialize x |
| 43 | + $"\"{x.Value}\"" <>! ser // Default render of Guid is not toStringN |
| 44 | + x.Value =! Serdes.Default.Deserialize<SkuId>(ser).Value |
| 45 | + |
| 46 | + let d = Dictionary() |
| 47 | + d.Add(x, "value") |
| 48 | + raises<System.NotSupportedException> <@ Serdes.Default.Serialize d @> |
| 49 | + |
| 50 | +module StringIdIsomorphism = |
| 51 | + |
| 52 | + [<Sealed; AutoSerializable false; StjConverter(typeof<SkuIdConverter>)>] |
| 53 | + type SkuId(value: System.Guid) = inherit FsCodec.StringId<SkuId>(Guid.toStringN value) |
| 54 | + and private SkuIdConverter() = |
| 55 | + inherit JsonIsomorphism<SkuId, string>() |
| 56 | + override _.Pickle(value: SkuId) = value |> string |
| 57 | + override _.UnPickle input = input |> Guid.parse |> SkuId |
| 58 | + |
| 59 | + [<Fact>] |
| 60 | + let comparison () = |
| 61 | + let g = Guid.gen() |
| 62 | + let id1, id2 = SkuId g, SkuId g |
| 63 | + true =! id1.Equals id2 |
| 64 | + id1 =! id2 |
| 65 | + |
| 66 | + [<Fact>] |
| 67 | + let serdes () = |
| 68 | + let x = Guid.gen () |> SkuId |
| 69 | + let ser = Serdes.Default.Serialize x |
| 70 | + $"\"{x}\"" =! ser |
| 71 | + x =! Serdes.Default.Deserialize ser |
| 72 | + |
| 73 | + let d = Dictionary() |
| 74 | + d.Add(x, "value") |
| 75 | + raises<System.NotSupportedException> <@ Serdes.Default.Serialize d @> |
| 76 | + |
| 77 | +module StringIdConverter = |
| 78 | + |
| 79 | + [<Sealed; AutoSerializable false; StjConverter(typeof<SkuIdConverter>)>] |
| 80 | + type SkuId(value: System.Guid) = inherit FsCodec.StringId<SkuId>(Guid.toStringN value) |
| 81 | + and private SkuIdConverter() = inherit StringIdConverter<SkuId>(Guid.parse >> SkuId) |
| 82 | + |
| 83 | + [<Fact>] |
| 84 | + let comparison () = |
| 85 | + let g = Guid.gen() |
| 86 | + let id1, id2 = SkuId g, SkuId g |
| 87 | + true =! id1.Equals id2 |
| 88 | + id1 =! id2 |
| 89 | + |
| 90 | + [<Fact>] |
| 91 | + let serdes () = |
| 92 | + let x = Guid.gen () |> SkuId |
| 93 | + $"\"{x}\"" =! Serdes.Default.Serialize x |
| 94 | + |
| 95 | + let d = Dictionary() |
| 96 | + d.Add(x, "value") |
| 97 | + raises<System.NotSupportedException> <@ Serdes.Default.Serialize d @> |
| 98 | + |
| 99 | +module StringIdOrKeyConverter = |
| 100 | + |
| 101 | + [<Sealed; AutoSerializable false; StjConverter(typeof<SkuIdConverter>)>] |
| 102 | + type SkuId(value: System.Guid) = inherit FsCodec.StringId<SkuId>(Guid.toStringN value) |
| 103 | + and private SkuIdConverter() = inherit StringIdOrDictionaryKeyConverter<SkuId>(Guid.parse >> SkuId) |
| 104 | + |
| 105 | + [<Fact>] |
| 106 | + let comparison () = |
| 107 | + let g = Guid.gen() |
| 108 | + let id1, id2 = SkuId g, SkuId g |
| 109 | + true =! id1.Equals id2 |
| 110 | + id1 =! id2 |
| 111 | + |
| 112 | + [<Fact>] |
| 113 | + let serdes () = |
| 114 | + let x = Guid.gen () |> SkuId |
| 115 | + $"\"{x}\"" =! Serdes.Default.Serialize x |
| 116 | + |
| 117 | + let d = Dictionary() |
| 118 | + d.Add(x, "value") |
| 119 | + $"{{\"{x}\":\"value\"}}" =! Serdes.Default.Serialize d |
0 commit comments