r/ProgrammerHumor • • 4d ago • Bravo 1

Meme commentsInJson

Post image
5.8k Upvotes

205 comments sorted by

View all comments

Show parent comment

15

u/Turbulent_Stick1445 4d ago

This is what I do, but there are two problems with it:

  1. A parser will end up storing the comments, wasting memory.
  2. You run into JSON's problems with multiline string constants if your comment is anything but trivial.

It gets ugly fast.

3

u/zoinkability 4d ago

Plus: maybe you don’t want the client to read your internal comments like “todo: close this security issue” or “added because the client is an idiot”

3

u/Sockoflegend 3d ago

We use node where I work so it just happened to be easier to have config type json files just be a JS import 

2

u/nameless_pattern 4d ago

You could keep them just in Dev deployments?  lot of messy trade-off choices

3

u/Turbulent_Stick1445 4d ago

The #1 place I've seen comments in JSON are where someone makes the mistake of using JSON for a configuration file format. The lack of support for comments is ironically why even XML is a better choice for that. Regardless, you can understand why people would want comments there outside of just dev environments.

(I just learned as of two hours ago that YAML can be used with JSON syntax so I guess it's academic, just use YAML...)

1

u/AnnoyedVelociraptor 3d ago

In Rust if you map a JSON object to a struct, and you don't have a field mapped for comments it gets dropped.

1

u/them0use 3d ago

No problem, just make `comments` an array! I am dead inside.

1

u/Turbulent_Stick1445 3d ago

I have done exactly that :( Now I know YAML can be written like JSON though I'm probably going to just switch to that in future as YAML has almost as much support as JSON. I'd only been avoiding it because I thought the indenting-is-meaningful syntax was mandatory.

1

u/d_maes 3d ago

3. Only works if you control the schema, or the target does not enforce a strict schema.