Fable.SimpleJson icon indicating copy to clipboard operation
Fable.SimpleJson copied to clipboard

Is there an equivalent of JsonConverter in Fable.SimpleJson?

Open lfr opened this issue 4 years ago • 2 comments

This is an example from the doc:

let person = { Name = "John"; Age = 34 }

let serialized =
    [ "name", JString person.Name
      "age", JNumber (float person.Age) ]
    |> Map.ofList
    |> JObject
    |> SimpleJson.toString

This is clear enough when I want to serialize a specific person record, but suppose I want to define how a type like Person should be serialized in general, whenever it's the value of a member like in the record below:

type MarriedCouple =
    {
        Spouse1 : Person
        Spouse2 : Person
    }

Is there any way to automatically serialize MarriedCouple using some pre-defined serialization definition for the type Person like the JsonConverters from JSON.NET or System.Text.Json?

lfr avatar Jan 02 '21 16:01 lfr

@Zaid-Ajaj for additional context I'm asking this because I made my library FSharp.ValidationBlocks compatible with Fable and now I'm just trying to ensure the types from that library serialize correctly so the users don't have to do anything else

lfr avatar Jan 05 '21 07:01 lfr

Hello, are there any news on this? This would be an immensely useful feature :)

aronerben avatar Oct 18 '22 09:10 aronerben

Is there any way to automatically serialize MarriedCouple using some pre-defined serialization definition for the type Person like the JsonConverters from JSON.NET or System.Text.Json?

Unless I don't understand the question, this is already possible: Json.serialize will do exactly that for any given (serializable) value

Zaid-Ajaj avatar Dec 02 '22 15:12 Zaid-Ajaj