YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Add format option to YamlMember to define Flow/Block serialization

Open mdmoura opened this issue 7 years ago • 6 comments

I have the following class:

public class Post {
  public Int32 Id { get; set; }
  public String Title { get; set; }
  public List<String> Tags { get; set; }
  public List<Author> Authors { get; set; }
}

public class Author {
  public Int32 Id { get; set; }
  public String Name { get; set; }
}

When I serialize it I get:

id: 10
title: Using YAML
tags:
- Programming
- YAML
authors:
-id: 2
 name: Mary
-id: 8
 name: John

I would like the property tags to be serialized as flow like:

tags: [Programming,YAML]

More control on serialization format would be great because it will help people on editing them,

Maybe adding a new option to YAMLMember as follows:

[YamlMember(Format = Flow)]

Does this make sense?

mdmoura avatar Mar 31 '18 11:03 mdmoura

I agree that this would be helpful to be able to set the SequenceStyle / MappingStyle. I just don't know what name to give to that property. It seems redundant to have two properties, one for sequences and the other for mappings. Maybe the name should be the opposite of "Scalar", such as "Structure" ? Since we already have the ScalarStyle property, we would have a StructureStyle property, accepting the same values as SequenceStyle / MappingStyle. What do you think?

aaubry avatar Mar 31 '18 15:03 aaubry

Maybe an attribute named YamlStyle that would accept the enum with values Flow, Block or Any which would be the default value as in MappingStyle?

mdmoura avatar Mar 31 '18 16:03 mdmoura

I'd rather use the existing YamlMember attribute instead of adding a new one. Previously an effort was made to unify different attributes into a single one, and I don't think that we should deviate from this.

aaubry avatar Mar 31 '18 17:03 aaubry

Is this problem currently solved?

ismael-moralesch avatar Dec 05 '18 20:12 ismael-moralesch

I agree that this would be helpful to be able to set the SequenceStyle / MappingStyle. I just don't know what name to give to that property. It seems redundant to have two properties, one for sequences and the other for mappings. Maybe the name should be the opposite of "Scalar", such as "Structure" ? Since we already have the ScalarStyle property, we would have a StructureStyle property, accepting the same values as SequenceStyle / MappingStyle. What do you think?

i would like to use it like so: [YamlMember(SequenceStyle = SequenceStyle.Flow)] and [YamlMember(MappingStyle = MappingStyle.Flow)]

Will try to impl...

lqdchrm avatar Dec 24 '19 23:12 lqdchrm

Is there a more hacky way to somehow control this?

jtheisen avatar Aug 17 '22 06:08 jtheisen