node-comment-json icon indicating copy to clipboard operation
node-comment-json copied to clipboard

Is it possible to insert a comment after a value?

Open blhoward2 opened this issue 3 years ago • 3 comments

After a json has been parsed, is it possible to insert a new comment that will then be retained? If so, how? I'm pulling my hair out...

blhoward2 avatar Oct 08 '22 16:10 blhoward2

I am also curious about this. Also i want to know how to add comments when building json object at runtime (not loading/parsing).

So for example i have this code:

const {stringify} = require('comment-json');
let obj = { name: "peter"; age: 20; };
// TODO: how to add comment before property "name" here before calling stringify ?
const str = stringify(obj);

psulek avatar Apr 06 '23 09:04 psulek

@blhoward2 @kaelzhang I found a wacky way to do this (ugly but working):

obj[Symbol.for('before:name')] = [{type: 'LineComment', inline: false, value: 'my custom comment'}];

will stringify to:

{ 
  // my custom comment
  name: "peter"; 
  age: 20; 
};

psulek avatar Apr 06 '23 09:04 psulek