0

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 {}

OscarSanhueza
  • 317
  • 2
  • 13

1 Answers1

1

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.

matthewrwilton
  • 123
  • 1
  • 6
  • This kind of solve the problem, but next is that I´m using attribute MarshalAs on the fields, and this attribute can't be used with properties. – OscarSanhueza Jan 30 '15 at 13:11
  • It's a bit complicated when I'm using the same structure to download what is in the YAML field to an realtime application which needs this marshaled structures. Do you known any reason why YamlDotNet doesn't serialize also fields? – OscarSanhueza Jan 30 '15 at 13:59
  • I am not sure why it does not serialise fields. The `GetProperties()` method in [YamlAttributesTypeInspector](https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/YamlAttributesTypeInspector.cs) has the logic that determines what properties get serialised. – matthewrwilton Jan 31 '15 at 20:37
  • I think in your case you may have to create a `IYamlTypeConverter` implementation for your struct and register this when serialising by calling the `RegisterTypeConverter` method on the `Serializer`. I haven't had to do this before but [GuidConverter](https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/Converters/GuidConverter.cs) looks like a good example of how to create one. – matthewrwilton Jan 31 '15 at 20:38
  • My problem is that it is really painful to make a new IYamTypeConverter to every one of my types, I had more than 20 structures which I download from yaml files to the realtime application where i need to marshall. Would be easier to just serialize fields, at least [yamlserializer](https://yamlserializer.codeplex.com/) is working with fields. – OscarSanhueza Sep 17 '15 at 06:54