Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F# samples #824

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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