F# 如何序列化和反序列化元组
如何将JSON序列化为F#中的元组(反之亦然) JSON模式F# 如何序列化和反序列化元组,f#,datacontractjsonserializer,F#,Datacontractjsonserializer,如何将JSON序列化为F#中的元组(反之亦然) JSON模式 { "name": "test", "type": "document", "id": "e3c7373c-f4bc-4ffa-9a01-7c7d9f83e4cf" } 元组 理想情况下,使用从此处截取的函数: // https://gist.github.com/theburningmonk/2071722 open System.Runtime.Serialization.Json //我将这两行从使用ASCII改为
{
"name": "test",
"type": "document",
"id": "e3c7373c-f4bc-4ffa-9a01-7c7d9f83e4cf"
}
元组
理想情况下,使用从此处截取的函数:
// https://gist.github.com/theburningmonk/2071722
open System.Runtime.Serialization.Json
//我将这两行从使用ASCII改为使用UTF8,
//用于UNICODE支持。这也是微软的相似之处
//例如使用。
让toString=System.Text.Encoding.UTF8.GetString
让toBytes(x:string)=System.Text.Encoding.UTF8.GetBytes x
让我们序列化json(json:string)=
让jsonSerializer=newDataContractJSONSerializer(typedefof//从这里截取的函数:
// https://gist.github.com/theburningmonk/2071722
open System.Runtime.Serialization.Json
//我将这两行从使用ASCII改为使用UTF8,
//对于UNICODE支持。这也是Microsoft的类似之处
//例如使用。
让toString=System.Text.Encoding.UTF8.GetString
让toBytes(x:string)=System.Text.Encoding.UTF8.GetBytes x
让我们序列化json(json:string)=
让jsonSerializer=new DataContractJsonSerializer(typedefofWhat JSON您应该为元组(“测试”,“文档”)
?对不起,我没听清楚,问题是将JSON序列化到元组点会有信息丢失。所以这不是“序列化”,因为根据定义是“序列化”必须是可重构的。例如:{“name”:“test”,“type”:“doc”}
最多只能是(string*obj)*(string*obj)
/对于元组(“test”,“document”),您应该看到什么JSON
?很抱歉,没有让您明白,问题是如何将JSON序列化为元组点-会有信息丢失。因此这不是“序列化”,因为根据定义,“序列化”必须是可重构的。例如:{“name”:“test”,“type”:“doc”}
最多只能是(string*obj)*(string*obj)
/plus一个用于使用LinqPad:Dit不是一个tupletrue,但是将其转换为一个是很简单的:(document2.id,document2.name,document2.type`)
。另外一个用于使用LinqPad:Dit不是一个tupletrue,但是将其转换为一个是很简单的:(document2.id,document2.name,document2.type`)
。
let document = ("test", "document", "e3c7373c-f4bc-4ffa-9a01-7c7d9f83e4cf")
// 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