fsprojects / FSharp.Json

F# JSON Reflection based serialization library
Apache License 2.0
226 stars 26 forks source link

Add new UnionMode #58

Open dtymakhov opened 2 years ago

dtymakhov commented 2 years ago

I added a new AsValue to the UnionMode. This will be useful when the field can be one of a type, such as a int or a string. But this cannot be deserialised.

For example:

  [<JsonUnion(Mode = UnionMode.AsValue)>]
    type AsValueUnion =
    | StringCase of string
    | IntCase of int

    type RecordWithAsValueUnion = {
        StringValue : AsValueUnion
        IntValue : AsValueUnion
    }

  let record = { StringValue = StringCase "String"; IntValue = IntCase 1 }

will be serialised like a {"StringValue":"String","IntValue":1}