forked from aaubry/YamlDotNet
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeserializeObjectGraph.fs
120 lines (95 loc) · 2.85 KB
/
DeserializeObjectGraph.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module YamlDotNet.Samples.Fsharp.DeserializeObjectGraph
open System
open System.Collections.Generic
open System.IO
open YamlDotNet.Serialization
open YamlDotNet.Serialization.NamingConventions
[<CLIMutable>]
type Customer = { Given: string; Family: string }
[<CLIMutable>]
type OrderItem =
{ [<YamlMember(Alias = "part_no", ApplyNamingConventions = false)>]
PartNo: string
Descrip: string
Price: decimal
Quantity: int }
[<CLIMutable>]
type Address =
{ Street: string
City: string
State: string }
[<CLIMutable>]
[<NoComparison>]
type Order =
{ Receipt: string
Date: DateTime
Customer: Customer
Items: List<OrderItem>
[<YamlMember(Alias = "bill-to", ApplyNamingConventions = false)>]
BillTo: Address
[<YamlMember(Alias = "ship-to", ApplyNamingConventions = false)>]
ShipTo: Address
SpecialDelivery: string }
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.
..."
let main () =
let input = new StringReader(Document)
let deserializer =
DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build()
let order = deserializer.Deserialize<Order>(input)
printfn "Order"
printfn "-----"
printfn ""
order.Items.ForEach(fun item -> printfn $"{item.PartNo}\t{item.Quantity}\t{item.Price}\t{item.Descrip}")
printfn ""
printfn "Shipping"
printfn "--------"
printfn ""
printfn "%A" order.ShipTo.Street
printfn "%A" order.ShipTo.City
printfn "%A" order.ShipTo.State
printfn ""
printfn "Billing"
printfn "-------"
printfn ""
if (order.BillTo = order.ShipTo) then
printfn "*same as shipping address*"
else
printfn "%A" order.ShipTo.Street
printfn "%A" order.ShipTo.City
printfn "%A" order.ShipTo.State
printfn ""
printfn "Delivery instructions"
printfn "---------------------"
printfn ""
printfn "%A" order.SpecialDelivery
main ()