3 ms·
It looks to me like a major difference between this and CSON is that this is safe and CSON is not. Literally the only difference between coffeescript.eval and
by mistercow 13y ago
It looks to me like a major difference between this and CSON is that this is safe and CSON is not.
Literally the only difference between coffeescript.eval and cson.parseSync is that the latter runs in a sandbox. You can't screw up so badly as to cause global side effects, and you can't require modules, but that's where the safety ends.
For example
cson.parseSync("Math.sqrt")(4)
will return 2, and
cson.parseSync("1 while true")
will cause an infinite loop.
That's very powerful for config files; I'd much rather have my config say "y: Math.sqrt 2" than "y: 1.4142135623730951". But it means that you absolutely must not parse CSON that comes from an untrusted source.
Now obviously JSON5 provides very little over JSON as a data-exchange format. In fact, the only practical advantage I see is that you can use Infinity as a value.
I can see two use cases for this:
1. You have a web service for developers, and you want human-editable config files that can be parsed safely on the server side. CSON won't work there, and JSON5 would be nicer to work with than JSON. It's a good thing that JSON5 is a strict superset of JSON, because this is one hell of rare scenario.
2. You want your exchange format to be more human readable for debugging and testing. This use case isn't realistic, however, so long as JSON5.stringify is aliased to JSON.stringify.
- matthewrudy 13y agoThe unsafe eval of objects in CSON seems to be an implementation quirk rathet than a feature. They're working on a static parser. Check out their WIP https://github.com/bevry/cson/issues/33 https://github.com/bevry/cson/issues/33
- mistercow 13y agoThat's good. It would be nice if they'd document the spec before doing this. Right now it's pretty unclear what is and isn't valid CSON, since any CoffeeScript code will run.