YamlSwift icon indicating copy to clipboard operation
YamlSwift copied to clipboard

Support serializing YAML

Open cezheng opened this issue 9 years ago • 8 comments

I took a look through the source but couldn't find a way to serialize the Yaml struct to a string. Any plans to support this?

cezheng avatar Oct 20 '15 16:10 cezheng

I initially planned for this feature. It even saves tokenization result for later use in serialization to keep original formatting of input string.

The problem is I'm very busy right now, but I appreciate a pull request for this feature.

behrang avatar Oct 20 '15 17:10 behrang

@behrang Thanks for the quick reply. Since the YAML data in my project happen to have kinda simple structure I'm using a very specific method to serialize them. I think I'll take some time to dig into your source once I have some free time.

cezheng avatar Oct 21 '15 10:10 cezheng

It would be great.

behrang avatar Oct 21 '15 11:10 behrang

If you were to receive a PR for serialization, do you have any guidance on where to start understanding the existing code (e.g. could you explain the tokenization result a bit)? Have you got any specific class structure you'd like to see it as? I don't generally do many open source contributions, so maybe I'm overthinking it.

Dan2552 avatar Jan 12 '16 10:01 Dan2552

No, you're correct. I try to describe it a little bit.

First, it was written when swift was at 1.0. And I tried to use a functional approach after I learned haskell. So there are a lot of operators for bind and etc like haskell and some monadic features.

Second, the final result is a Yaml enum. After tokenization and parsing, a Yaml enum is created. This is the important thing. You may read that structure and try to serialize it according to Yaml spec. This should be easy as Yaml rules are simple. For example when you see a Yaml dictionary, you may start with writing key: value\n at the current indentation level.

Third, the hard part is keeping the serialized document the same as the original one. For example if an Array is written inline, and you ignore this fact and serialize it with each item in a new line, the final document would be different syntactically (but not semantically.) To add this feature (although it is not required) you should consider tokenization results while serializing.

Tokenization result is [TokenMatch] and TokenMatch is a tuple with 2 fields: (type: TokenType, match: String). So it is a list of things that have been found on the input document. While serializing, you can traverse this list along with the Yaml enum to be serialized and use the same token match when necessary. For example when an Indent is needed, you can check tokenization result and use the indentation used in the original document (whether it is a 2 space string, a tab, 2 tabs, etc...)

Good luck.

behrang avatar Jan 18 '16 08:01 behrang

@cezheng Maybe a second take on this using Swift 4 Codeable protocols would help

seivan avatar Sep 20 '17 11:09 seivan

Please! :) ... this functionality would be a killer!

rafiki270 avatar Oct 14 '17 23:10 rafiki270

@rafiki270 Looking into if it's worth spending more time on this vs going with SwiftParsec instead.

seivan avatar Oct 17 '17 15:10 seivan