jet / FsCodec

F# Event-Union Contract Encoding with versioning tolerant converters supporting System.Text.Json and Newtonsoft.Json
https://github.com/jet/dotnet-templates
Apache License 2.0
83 stars 19 forks source link

feat(NewtonsoftJson): backport Serdes ctor auto* options from STJ side of the house #98

Open bartelink opened 1 year ago

bartelink commented 1 year ago

Backport the autoTypeSafeEnumToJsonString and autoUnionToJsonObject options to avoid the nasty surprised the default impl causes when (not if!) people fall into the default rendering trap for things lke:

a) TypeSafeEnums e.g. type ProductId = ProductA | ProductB (where you just want "ProductA", as the TypeSafeEnumconverter would do if applied explicitly) b) Unions that should render as a JSON object (record)

    type UnionThatShouldBeAnObject =
        | SimpleProduct of master: ProductId
        | PairedProduct of {| primary: ProductId; backup: ProductId |}

will render as { "Case": "PairedProduct", "primary": "ProductA", "backup": "ProductA"} or { "Case": "PairedProduct", "master": "ProductA"}, as it would if you applied the UnionConverter explicitly

96 is a very important related safety feature too

i.e.prevent:

"items": [
        {
          "serviceId": "5c8795be52e34e82883d61babed19513",
          "serviceKind": {
            "Case": "ProductA"
          }
        },
        {
          "serviceId": "8a55ebbf0d404485b50da95bdb53f7b3",
          "serviceKind": {
            "Case": "ProductB"
          }
        }
      ]
    },

and default to

"items": [
                            {
                                "serviceId": "5c8795be52e34e82883d61babed19513",
                                "serviceKind": "ProductB"
                            },
                            {
                                "serviceId": "8a55ebbf0d404485b50da95bdb53f7b3",
                                "serviceKind": "ProductA"
                            }
                        ]

related: https://github.com/JamesNK/Newtonsoft.Json/issues/1662#issuecomment-1646777848