YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Implement option to write sequences out in flow stlye for only certain fields with primitive arrays

Open szahn opened this issue 12 years ago • 8 comments
trafficstars

Sequences are currently being written out as blocks. There should be a public variable in the emitter to set the style to flow on certain fields that hold arrays with primitive values.

See http://yaml.org/spec/1.0/#id2564419

szahn avatar Dec 08 '12 00:12 szahn

Is there documentation on how to implement your own visitor pattern during implementation or intercept the event emitter such that you can define a style (flow or block) for a particular field in the object graph?

szahn avatar Dec 08 '12 00:12 szahn

I was able to set the sequence style to flow only on primitive arrays by adding this code to the VisitSequenceStart() method in AnchorAssigningObjectGraphVisitor class. However, there should be a better way to do this, perhaps making it easier to pass in your own visitor when instantiating the serializer class.

if (sequenceType.IsArray && elementType.IsPrimitive) { info.Style = Core.Events.SequenceStyle.Flow; }

szahn avatar Dec 08 '12 01:12 szahn

The Serializer class is meant to be a simple facade for the various classes that collaborate to the serialization process. However, it is still work in progress and that's the reason why it is not yet possible to alter the visitors that will be used, but this feature is definitely on top of the list. Your requirement of emitting arrays as flow sequences is the kind of usage that I had in mind when I started this refactoring.

aaubry avatar Dec 10 '12 16:12 aaubry

Now that version 2.0.0 is out, I think we should work on making the Serializer more extensible. I will keep you comments in mind while working on it. Thanks

aaubry avatar May 29 '13 23:05 aaubry

Is flow style for sequences implemented in the current version? I was using YamlDotNet to dump objects but since I've got a few relatively large byte arrays it is making the dump next to unreadable (which defeats the purpose of Yaml :-) ) I was expecting something in SerializerOptions but didn't find anything, then stumbled on this issue. Any news?

loudenvier avatar Aug 15 '15 20:08 loudenvier

Is it there yet, or does anyone have some sort of hack/fix? Much appreciated.

zeroregard avatar Feb 07 '18 07:02 zeroregard

Anyone stumbling upon this, I was able to do it with this fork https://github.com/xoofx/SharpYaml

You then apply this attribute on the fields you want to have a different style: [SharpYaml.Serialization.YamlStyle(YamlStyle.Flow)]

zeroregard avatar Feb 08 '18 09:02 zeroregard

@mathiassiig Is it possible to submit this as a pull request to the main branch? Especially with GitHub Actions YAML using this in places, it would be useful to have this functionality.

samsmithnz avatar Oct 12 '19 19:10 samsmithnz

This issue appears to be abandoned, I'm going to close it. I believe this is already implemented.

EdwardCooke avatar Jan 13 '23 20:01 EdwardCooke

I can't see any information in this issue or linked issues that this is implemented. Is it, and if so, how can it be accomplished?

Also, is there any way to set max line length for flow-style arrays (making them break when the max length has been reached)?

cmeeren avatar Aug 17 '23 21:08 cmeeren

We don’t have a way of doing that in yamldotnet that I’m aware of. We’re very grateful for pull requests if you want to create one.

EdwardCooke avatar Aug 17 '23 22:08 EdwardCooke

Thanks. Unfortunately swamped. I found a (hacky?) workaround: In Visit(YamlSequenceNode sequence), check the NodeType of the items of the sequence and check if they are YamlNodeType.Scalar.

In any case, it seems this issue was closed erroneously then. You may consider opening it, for clarity.

cmeeren avatar Aug 17 '23 22:08 cmeeren

I closed because there was no comment for 3 years.

EdwardCooke avatar Aug 17 '23 22:08 EdwardCooke