Can someone help me with this. How to use YamlDotNet
to deserialize an object?
My code:
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!
If I write:
let conf = de.Deserialize(stream)
I have:
val conf : obj = dict [("a","a");("b","b")]
I wish I can have working F# code of this C# example https://dotnetfiddle.net/HD2JXM
I rewrote the code by reference (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);
Print:
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.
The writing style is not F#, but it's easy to fix
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With