F# 和 YamlDotNet

F# and YamlDotNet

有人可以帮我解决这个问题吗?如何使用YamlDotNet反序列化一个对象?

我的代码:

open YamlDotNet 
open YamlDotNet.Serialization
open System 
open System.IO

let ss1 (obj, f) =   
    use tw = File.CreateText(f)  
    let s = new Serializer()
    s.Serialize(tw, obj)

type tobject() =
    member this.a = "a"
    member this.b = "b"

let f = "xxx.yml"
let obj = new tobject()
ss1(obj, f)

let de = new Deserializer()
let file = new FileStream(f, FileMode.Open, FileAccess.Read)  
let stream = new StreamReader(file)
// let conf = de.Deserialize(stream)
let conf = de.Deserialize<tobject>(stream)  // YamlException occur here!

如果我写:

let conf = de.Deserialize(stream)

我有:

val conf : obj = dict [("a","a");("b","b")]

我希望我能拥有这个 C# 示例的有效 F# 代码 https://dotnetfiddle.net/HD2JXM

我参考重写了代码 (https://dotnetfiddle.net/HD2JXM)in F#:

open System;
open System.Collections.Generic;
open YamlDotNet.Serialization
open YamlDotNet.Serialization.NamingConventions
open System.IO

let Document = """---
            receipt:    Oz-Ware Purchase Invoice
            date:        2007-08-06
            customer:
                given:   Dorothy
                family:  Gale

            items:
                - part_no:   A4786
                  descrip:   Water Bucket (Filled)
                  price:     1.47
                  quantity:  4

                - part_no:   E1628
                  descrip:   High Heeled ""Ruby"" Slippers
                  price:     100.27
                  quantity:  1

            bill-to:  &id001
                street: |-
                        123 Tornado Alley
                        Suite 16
                city:   East Westville
                state:  KS

            ship-to:  *id001

            specialDelivery: >
                Follow the Yellow Brick
                Road to the Emerald City.
                Pay no attention to the
                man behind the curtain.
..."""

type Customer() =
    member val Given  = "" with get,set
    member val Family = "" with get,set

type Address() = 
    member val Street  = "" with get,set
    member val City    = "" with get,set
    member val State   = "" with get,set

type OrderItem() = 
    [<YamlMember(Alias = "part_no")>]
    member val PartNo   = "" with get,set
    member val Descrip  = "" with get,set
    member val Price    = 0M with get,set
    member val Quantity = 0 with get,set

type Order() = 
    member val Receipt = "" with get,set
    member val Date = new DateTime() with get,set
    member val Customer = new Customer() with get,set
    member val Items = new List<OrderItem>() with get,set

    [<YamlMember(Alias = "bill-to")>]
    member val BillTo = new Address() with get,set

    [<YamlMember(Alias = "ship-to")>]
    member val ShipTo = new Address() with get,set

    member val SpecialDelivery = "" with get,set

let input = new StringReader(Document)

let deserializer = new Deserializer(namingConvention = new CamelCaseNamingConvention())

let f = "xxx.yml"
let file = new FileStream(f, FileMode.Open, FileAccess.Read)  
let stream = new StreamReader(file)

let order = deserializer.Deserialize<Order>(stream)
stream.Close();

Console.WriteLine("Order");
Console.WriteLine("-----");
Console.WriteLine();
order.Items.ForEach(fun item -> Console.WriteLine("{0}\t{1}\t{2}\t{3}", item.PartNo, item.Quantity, item.Price, item.Descrip))

Console.WriteLine();
Console.WriteLine("Shipping");
Console.WriteLine("--------");
Console.WriteLine();
Console.WriteLine(order.ShipTo.Street);
Console.WriteLine(order.ShipTo.City);
Console.WriteLine(order.ShipTo.State);
Console.WriteLine();

Console.WriteLine("Billing");
Console.WriteLine("-------");
Console.WriteLine();
if(order.BillTo = order.ShipTo) then Console.WriteLine("*same as shipping address*");
else 
    Console.WriteLine(order.ShipTo.Street);
    Console.WriteLine(order.ShipTo.City);
    Console.WriteLine(order.ShipTo.State);

Console.WriteLine();
Console.WriteLine("Delivery instructions");
Console.WriteLine("---------------------");
Console.WriteLine();
Console.WriteLine(order.SpecialDelivery);

打印:

Order
-----

A4786   4       1,47    Water Bucket (Filled)
E1628   1       100,27  High Heeled ""Ruby"" Slippers

Shipping
--------

123 Tornado Alley
Suite 16
East Westville
KS

Billing
-------

*same as shipping address*

Delivery instructions
---------------------

Follow the Yellow Brick Road to the Emerald City. Pay no attention to the man be
hind the curtain.

写作风格不是F#,但很容易修复