如何序列化和反序列化元组

时间:2018-04-01 10:10:55

标签: f# datacontractjsonserializer

如何在F#中将JSON序列化为元组(反之亦然)?

JSON架构

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

元组

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

理想情况下使用DataContractJsonSerializer

1 个答案:

答案 0 :(得分:2)

// 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