Closed idleberg closed 8 years ago
Hi!
There already is cson.dumps
that generates valid CSON documents. (And they also happen to be valid JSON documents. Yay! :) ).
In all seriousness though, I understand that you're looking for something that would use the non-flow syntax for objects. It would be fairly easy to create a naïve "real-CSON" implementation of cson.dumps
, but under such implementation cson.dumps(cson.loads(s))
would strip away all comments and thus wouldn't be useful for making edits into human-written CSON files.
And if a file is both generated and parsed by a machine, why not use plain JSON?
So I guess my question is, what is your usecase?
I'm already using cson.dumps
, but the result looks like JSON to me, not CSON.
My use-case is an plug-in for Sublime Text, which converts snippets so they can be used in Atom. Now, Atom works fine with either JSON or CSON, but ultimately the decision for the format lies with the user who converts the files. Since I offer the plugin for Atom (written in CoffeeScript) and Sublime Text (written in Python), I would expect to get the same results.
CoffeeScript conversion (Atom):
Python conversion (Sublime Text):
I would expect the latter conversion to ommit curly braces and commas – like in the screenshot above.
I have to admit my knowledge of Python is rather limited, but Ito me the relevant segment in my code looks about right:
array = {}
for item in completions:
try:
array[item['trigger']] = {'prefix': item['trigger'], 'body': item['contents']}
except KeyError:
pass
atom = {scope: (array)}
selection = sublime.Region(0, self.view.size())
self.view.replace(edit, selection, cson.dumps(atom, sort_keys=True, indent=4, separators=(',', ': ')))
the result looks like JSON to me, not like CSON
Yes, it is JSON, I was trying to make a joke: since every JSON document is also a CSON document, serializing to JSON gives you a valid CSON document. I understand that's not what you want.
I will write a proper implementation for cson.dumps
so that the output looks more CSON-ish.
Thanks for the issue!
I've added a better serializer in 0.5. Please try it out (upgrade with pip install --upgrade cson
) and if it doesn't work for you, please reopen the issue.
Thank you! For the most part it's working, though I noticed a small anomaly. When I convert a dictionary containing the key .source.c
, the produced CSON is invalid since the key is not wrapped in single-quotes (or any other type of quotes). However, when I convert a JSON into CSON, the key is wrapped in such quotes.
I'm not sure about the exact specification, I think keys can ommit quotes when they contain alphanumeric characters, otherwise they should be wrapped in quotes.
Update: Actually, I ran into some cases where JSON to CSON conversion produces invalid CSON, again it was keys with non-alphanumeric characters that weren't wrapped in quotes. If it's easier, would you consider wrapping all keys into quotes?
That's weird, I cannot repro. This:
{ ".source.c": 1 }
serializes as
'.source.c': 1
which should be correct. Could you open a new issue and provide a minimal example which reproduces the problem?
Do you have plans to add an equivalent for
json.dumps
for CSON?