r/ProgrammerHumor • • 11d ago • Bravo 1

Meme commentsInJson

Post image
6.0k Upvotes

202 comments sorted by

View all comments

Show parent comment

16

u/Turbulent_Stick1445 10d 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.

4

u/Sockoflegend 10d 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 

3

u/zoinkability 10d 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”

2

u/nameless_pattern 10d ago

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

4

u/Turbulent_Stick1445 10d 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 10d 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 10d ago

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

1

u/Turbulent_Stick1445 9d 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 10d ago

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