Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
323 stars 44 forks source link

How handle full cases from first letter #150

Open kyryloboiev opened 1 year ago

kyryloboiev commented 1 year ago

Сan FSharp.SystemTextJson work with case like this when I have only 1 letter or not full word and I want to return

Example: in json I have "Mail" or "m" and I want to return Comunication.Mailing

open System
open System.Text.Json
open System.Text.Json.Serialization

[<JsonFSharpConverter(JsonUnionEncoding.FSharpLuLike)>]
type Comunication =
    | [<JsonName"MAILING">] Mailing
    | [<JsonName "STANDART>] Standart

type Robot = {
     Code : int
     Model : Comunication
}

[<EntryPoint>]
let main args =

    let r2d2 = { Code = 123; Model = AddressType.Mailing }
    let x = JsonSerializer.Deserialize<Robot>( """{"Code":123,"Model":"M"}"""
    0

[] let **should return Mailing** () = Assert.Equal(r2d2, JsonSerializer.Deserialize( """{"Code":123,"Model":"M"}""", options))

Tarmil commented 1 year ago

There is no support for partial values, but you can put several names explicitly: [<JsonName("MAILING", "MAIL", "M")>].