Skip to content

Commit

Permalink
Merge pull request #824 from 64J0/64J0-patch-1
Browse files Browse the repository at this point in the history
F# samples
  • Loading branch information
EdwardCooke authored Sep 1, 2023
2 parents 1d01099 + 94b8aeb commit e856ff3
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
119 changes: 119 additions & 0 deletions YamlDotNet.Samples.Fsharp/DeserializeObjectGraph.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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>]
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 ()
20 changes: 20 additions & 0 deletions YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\YamlDotNet\YamlDotNet.csproj">
<Project>{BF32DE1B-6276-4341-B212-F8862ADBBA7A}</Project>
<Name>YamlDotNet</Name>
</ProjectReference>
</ItemGroup>

<ItemGroup>
<Compile Include="DeserializeObjectGraph.fs" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions YamlDotNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YamlDotNet.Analyzers.Static
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YamlDotNet.Core7AoTCompileTest", "YamlDotNet.Core7AoTCompileTest\YamlDotNet.Core7AoTCompileTest.csproj", "{DEB5099E-D216-438B-86A7-03674F9185EF}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "YamlDotNet.Samples.Fsharp", "YamlDotNet.Samples.Fsharp\YamlDotNet.Samples.Fsharp.fsproj", "{C047392D-6B20-47CD-9FE6-D0FA326FD262}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,6 +65,10 @@ Global
{DEB5099E-D216-438B-86A7-03674F9185EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEB5099E-D216-438B-86A7-03674F9185EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEB5099E-D216-438B-86A7-03674F9185EF}.Release|Any CPU.Build.0 = Release|Any CPU
{C047392D-6B20-47CD-9FE6-D0FA326FD262}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C047392D-6B20-47CD-9FE6-D0FA326FD262}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C047392D-6B20-47CD-9FE6-D0FA326FD262}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C047392D-6B20-47CD-9FE6-D0FA326FD262}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit e856ff3

Please sign in to comment.