此代码在.net上运行没有问题,但在使用mono 4.4.1
时崩溃#I __SOURCE_DIRECTORY__
#r "System.dll"
#r "System.Net.Http.dll"
#r "System.Runtime.Serialization"
open System
open System.Net.Http
open System.IO
open System.Text
open System.Runtime.Serialization
open System.Runtime.Serialization.Json
[<DataContract(Name="request")>]
[<CLIMutable>]
type Request =
{
[<field: DataMember(Name = "act")>]
Action: string
[<field: DataMember(Name = "qry")>]
Query: obj
}
let fromJson<'T> (json: string) =
let dcs = DataContractJsonSerializer(typeof<'T>)
use ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(json))
(new StreamWriter(ms)).Write(json)
dcs.ReadObject(ms) :?> 'T
let toJson o =
let dcs = DataContractJsonSerializer(o.GetType())
use ms = new MemoryStream()
dcs.WriteObject(ms, o)
ms.Position <- 0L
(new StreamReader(ms)).ReadToEnd()
{ Action = "Test"; Query = new Object() }
|> toJson
|> printfn "%s"
这是一个最小的失败测试,只需将它扔到fsharpi并将其与fsi结果进行比较。
实际上这只会触发错误:
let test = { Action = "Test"; Query = new Object() }
let dcs = DataContractJsonSerializer(test.GetType())
use ms = new MemoryStream()
dcs.WriteObject(ms, test)