OscarSanhueza
OscarSanhueza

Reputation: 317

Using objects inside objects

Trying to use the serializer of YAMLDOTNET, having some problems when I have and object which is not composed of just strings but also has an special object inside.

When serializing I will just get a {} string. If for example on the Serializing an object graph sample we define a structure Address. Then we create a new object of the class Address inside, which is eventually assign in the receipt, the results will be also a {} on the address field on the yaml file.

The sample code can be also seen here. This will create an output that looks like:

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 {}
ship_to: *o0

So the bill_to will appear as {}

Upvotes: 0

Views: 581

Answers (1)

matthewrwilton
matthewrwilton

Reputation: 133

YamlDotNet.Serialization.Serializer does not serialise fields into the YAML output. It works in the example, because that is using a dynamic object and street, city and state are properties of that object.

If you change the fields in your Address to properties they will be serialised e.g.

public struct Address
{
    public string street { get; set; }
    public string city { get; set; }
    public string state { get; set; }
}

Using properties instead of public fields is also best practice.

Upvotes: 1

Related Questions