Search code examples
f#datacontractjsonserializer

how to serialize and deserialize a tuple


How to serialize a JSON to tuple within F# (and vice versa)?

JSON Schema

{
  "name": "test",
  "type": "document",
  "id": "e3c7373c-f4bc-4ffa-9a01-7c7d9f83e4cf"
}

Tuple

let document = ("test", "document", "e3c7373c-f4bc-4ffa-9a01-7c7d9f83e4cf")

Ideally using DataContractJsonSerializer


Solution

  • // Functions snipped from here:
    // https://gist.github.com/theburningmonk/2071722
    
    open System.Runtime.Serialization.Json
    
    // I changed these two lines from using ASCII to using UTF8,
    // for UNICODE support. This is also what Microsoft's similar
    // examples use.
    let toString = System.Text.Encoding.UTF8.GetString
    let toBytes (x : string) = System.Text.Encoding.UTF8.GetBytes x
    
    let serializeJson<'a> (x : 'a) = 
        let jsonSerializer = new DataContractJsonSerializer(typedefof<'a>)
        use stream = new MemoryStream()
        jsonSerializer.WriteObject(stream, x)
        toString <| stream.ToArray()
    
    let deserializeJson<'a> (json : string) =
        let jsonSerializer = new DataContractJsonSerializer(typedefof<'a>)
        use stream = new MemoryStream(toBytes json)
        jsonSerializer.ReadObject(stream) :?> 'a
    
    // End of snip
    
    [<CLIMutable>]
    [<DataContract>]
    type MyType = {
        [<DataMember>] name: string
        [<DataMember>] ``type``: string
        [<DataMember>] id: string
        }
    
    let document = { name = "test"; ``type`` = "document"; id = "e3c7373c-f4bc-4ffa-9a01-7c7d9f83e4cf" }
    let json = serializeJson<MyType> document
    json.Dump() // LINQPad output
    
    let json2 = """{"id":"e3c7373c-f4bc-4ffa-9a01-7c7d9f83e4cf","name":"test","type":"document"}"""
    let document2 = deserializeJson<MyType> json2
    document2.Dump() // LINQPad output