YamlSwift
YamlSwift copied to clipboard
Support serializing YAML
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?
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 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.
It would be great.
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.
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.
@cezheng Maybe a second take on this using Swift 4 Codeable
protocols would help
Please! :) ... this functionality would be a killer!
@rafiki270 Looking into if it's worth spending more time on this vs going with SwiftParsec instead.