Sublime Forum

Comments and the SerializedDataConverter plugin

#1

Is there an option to preserve comments when using the SerializedDataConverter plugin to convert JSON to YAML and viceversa?

0 Likes

#2

Short answer, no.

SerializedDataConverter leverages existing libraries that parse JSON or YAML etc. These libraries discard comments because they serve no real purpose. In all honesty, JSON doesn’t support comments. The only reason SerializedDataConverter recognizes comments and strips them is because I wanted it to work on Sublime JSON files which do support comments, but comments are not in the JSON spec. It would probably be more appropriate to say it parses a derivative of JSON.

But preserving location of comments and re-injecting them into the converted format would be a lot of work, and is something I’m not interested in pursuing.

0 Likes

#3

I see. But wouldn’t it be sufficient to exchange // with # at the beginning of lines?

0 Likes

#4

I see. But wouldn’t it be sufficient to exchange // with # at the beginning of lines?

Consider the actual conversion process of Python’s json lib and the 3rd party yaml lib black boxes.

  • I strip comments since comments are not part of JSON. I could save them in some structure for arguments sake.
  • I pass valid JSON into json and out comes a Python dictionary. At this point I know what the comments are, but I don’t know exactly where they are associated in the returned dictionary. Do they trail certain items, to they proceed certain items? All such meta data is lost.
  • I now pass the dictionary into yaml and out comes yaml text, but where do I stick the comments? And how?

The answer would be to create my own JSON parser that takes meaningless comments and associates them in some manner to the output structure, and then create my own YAML parser that reconstructs the YAML myself and inserts the comments. This would need to work the other way as well. Out of the two, YAML is by far the hardest to parse.

You can see this is not a simple replace // with #. To preserve the comments and know where in the file format the comments go, I have to manually parse these files myself. And frankly I’m not interested in all that work for a free plugin. Serialized data is converted, not comments. If it was trivial as you suggested, I’d have no problems doing it, unfortunately it is not trivial.

0 Likes

#5

An alternative would be to preprocess the comments so that they become some valid JSON, and then post-process the resulting file to convert back into comments. But I see your point perfectly and I thank you for your excellent plugin already (which is actually amazingly fast)

0 Likes

#6

That idea of inserting them as some valid object has been brought up before. And while preprocessing JSON to find comments is easy enough, preprocessing YAML is not, and I don’t actually search for YAML comments as the parser strips them out. YAML is awesome, but a syntax nightmare to parse.

If motivated, I’m sure in time “something” could be accomplished, but I have absolutely no desire or motivation to do this as I support too much as is. My motivation for features in the various different plugins I support is mainly driven by what I actually use. There’s just not the motivation there for me to tackle this, sorry. I’m just picky about what I spend my time working on. Feel free to fork and modify and maintain your own version if this feature is really important to you, it’s just not a feature I want to personally maintain.

I am glad you find the plugin useful though :slight_smile:.

0 Likes