Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 523
Samples.SerializeObjectGraph
Antoine Aubry edited this page Jun 23, 2019
·
3 revisions
Shows how to convert an object to its YAML representation.
usingSystem;usingYamlDotNet.Serialization;usingYamlDotNet.Samples.Helpers;usingXunit.Abstractions;namespaceYamlDotNet.Samples{publicclassSerializeObjectGraph{privatereadonlyITestOutputHelperoutput;publicSerializeObjectGraph(ITestOutputHelperoutput){this.output=output;}[Sample(DisplayName="Serializing an object graph",Description="Shows how to convert an object to its YAML representation.")]publicvoidMain(){varaddress=newAddress{street="123 Tornado Alley\nSuite 16",city="East Westville",state="KS"};varreceipt=newReceipt{receipt="Oz-Ware Purchase Invoice",date=newDateTime(2007,8,6),customer=newCustomer{given="Dorothy",family="Gale"},items=newItem[]{newItem{part_no="A4786",descrip="Water Bucket (Filled)",price=1.47M,quantity=4},newItem{part_no="E1628",descrip="High Heeled \"Ruby\" Slippers",price=100.27M,quantity=1}},bill_to=address,ship_to=address,specialDelivery="Follow the Yellow Brick\n"+"Road to the Emerald City.\n"+"Pay no attention to the\n"+"man behind the curtain."};varserializer=newSerializerBuilder().Build();varyaml=serializer.Serialize(receipt);output.WriteLine(yaml);}}publicclassAddress{publicstringstreet{get;set;}publicstringcity{get;set;}publicstringstate{get;set;}}publicclassReceipt{publicstringreceipt{get;set;}publicDateTimedate{get;set;}publicCustomercustomer{get;set;}publicItem[]items{get;set;}publicAddressbill_to{get;set;}publicAddressship_to{get;set;}publicstringspecialDelivery{get;set;}}publicclassCustomer{publicstringgiven{get;set;}publicstringfamily{get;set;}}publicclassItem{publicstringpart_no{get;set;}publicstringdescrip{get;set;}publicdecimalprice{get;set;}publicintquantity{get;set;}}}receipt: Oz-Ware Purchase Invoice
date: 2007-08-06T00:00:00.0000000
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: &o0
street: >-
123 Tornado Alley
Suite 16
city: East Westville
state: KS
ship_to: *o0
specialDelivery: >-
Follow the Yellow Brick
Road to the Emerald City.
Pay no attention to the
man behind the curtain.