Serialization 用protobuf网反序列化F#歧视联合
使用以下SO答案作为指导: 我已经想出了一种方法来序列化一个简单的F#歧视联盟。不幸的是,我无法让它在不引发Serialization 用protobuf网反序列化F#歧视联合,serialization,f#,protobuf-net,discriminated-union,Serialization,F#,Protobuf Net,Discriminated Union,使用以下SO答案作为指导: 我已经想出了一种方法来序列化一个简单的F#歧视联盟。不幸的是,我无法让它在不引发堆栈溢出异常的情况下序列化DU的第二个案例。我做错了什么 代码 开放系统 开放系统 open System.Collections.Generic 开放式ProtoBuf [] [] [] 类型DU= |好的,布尔 |智力差 具有 重写此.ToString()= 与此匹配 |好的i->i.ToString() |错误的s->s.ToString() [] 键入Person(姓名:strin
堆栈溢出异常的情况下序列化DU的第二个案例。我做错了什么
代码
开放系统
开放系统
open System.Collections.Generic
开放式ProtoBuf
[]
[]
[]
类型DU=
|好的,布尔
|智力差
具有
重写此.ToString()=
与此匹配
|好的i->i.ToString()
|错误的s->s.ToString()
[]
键入Person(姓名:string,年龄:int,du:du)=
让可变名称=名称
让可变年龄=年龄
设可变du=du
new()=Person(“,0,良好的true)
[]
请告诉我你的名字
使用get()=name
并设置(v)=name异常发生在哪里?堆栈?调试模式下的唯一输出是:未知模块中发生“System.StackOverflowException”类型的未处理异常。
如上代码注释所述,在调用反序列化(fileStream2)时触发异常。
发生异常时调用堆栈是什么?调用堆栈(Visual Studio,调试模式)是空白的!这有点像代码中发生的,我的程序没有关于元信息的信息。你可能想考虑一下它与StimBuf在分级大小和性能上的竞争,并且很好地支持了FF类型。异常发生在哪里?堆栈?调试模式下的唯一输出是:未知模块中发生“System.StackOverflowException”类型的未处理异常。
如上代码注释所述,在调用反序列化(fileStream2)时触发异常。
发生异常时调用堆栈是什么?调用堆栈(Visual Studio,调试模式)是空白的!这有点像代码中发生的,我的程序没有关于元信息的信息。你可能想考虑一下,它与StimBuf在分级大小和性能上有竞争性,并且很好地支持了FF类型。
open System
open System.IO
open System.Collections.Generic
open ProtoBuf
[<ProtoContract>]
[<ProtoInclude(100, "Program+DU+Good")>]
[<ProtoInclude(200, "Program+DU+Bad")>]
type DU =
| Good of bool
| Bad of int
with
override this.ToString() =
match this with
| Good i -> i.ToString()
| Bad s -> s.ToString()
[<ProtoContract; Serializable>]
type Person(name:string, age:int, du:DU) =
let mutable name = name
let mutable age = age
let mutable du = du
new() = Person("",0,Good true)
[<ProtoMember(1)>]
member this.Name
with get() = name
and set(v) = name <- v
[<ProtoMember(2)>]
member this.Age
with get() = age
and set(v) = age <- v
[<ProtoMember(3)>]
member this.DU
with get() = du
and set(v) = du <- v
override this.ToString() = this.Name + ", " + this.Age.ToString() + ", " + this.DU.ToString()
[<EntryPoint>]
let main argv =
// typeof vs typedefof ?
ProtoBuf.Meta.RuntimeTypeModel.Default.[typeof<DU>.GetNestedType("Good")].Add("item").UseConstructor <- false
ProtoBuf.Meta.RuntimeTypeModel.Default.[typeof<DU>.GetNestedType("Bad")].Add("item").UseConstructor <- false
//RuntimeTypeModel.Default.[typeof<DU>].CompileInPlace() // doesn't seem to make any difference
let p1 = Person("Sam", 38, Good false)
let p2 = Person("Cand", 34, Bad 99)
let filePath = @"C:\Temp\protocol.bin"
if not (File.Exists filePath) then
let fs = File.Create(filePath)
fs.Close()
use fileStream1 = new System.IO.FileStream(filePath, FileMode.Truncate, FileAccess.Write)
ProtoBuf.Serializer.Serialize<Person>(fileStream1, p2) // p1 does work because it uses the first case of the DU
fileStream1.Close()
use fileStream2 = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read)
let result = ProtoBuf.Serializer.Deserialize<Person>(fileStream2) // stackoverflow exception ONLY for p2
fileStream2.Close()
printfn "%A" result
Console.ReadLine() |> ignore
0 // return an integer exit code