bevry / cson

CoffeeScript-Object-Notation. Same as JSON but for CoffeeScript objects.
Other
1.34k stars 56 forks source link

Saving comments. #67

Closed KieronWiltshire closed 5 years ago

KieronWiltshire commented 9 years ago

So I'd really love the ability to modify CSON objects directly, perhaps something like this:

var cson = require('cson');
var csonObj = cson.load('myFile');

if (!csonObj.get('some-property').exists()) {
    csonObj.add('some-property').comment('blablabla').save();
    cson.save('myFile', csonObj);
}
rdeforest commented 7 years ago

if you don't mind extending your schema, you could dictate that when a property's value is an object with a $ key, the value of the $ property is the "real" value and all other keys are meta information.

Your example above would generate something like...

    'some-property':
      comment: "blablabla"
      $: undefined

All existing files would still have the same meaning as long as they have no $ keys. If there are any $ keys the conversion is relatively simple:

    currencyStuff: $: "dollar sign"

    # would  be re-mapped to
    currencyStuff: $: $: "dollar sign"

Alternatively, the CSON implementation could use Symbol('comment') to store comments in memory and then represent them with the usual # syntax. That might be simpler, assuming CSON doesn't allow Symbols. I didn't see any reference in source/index.coffee.

balupton commented 5 years ago

This seems a question for https://github.com/groupon/cson-parser