r/ProgrammerHumor • • 9d ago

Meme commentsInJson

Post image
6.0k Upvotes

202 comments sorted by

View all comments

Show parent comment

65

u/the-liquidian 9d ago

JSONC was created for this.

86

u/AnotherPillow 9d ago

More annoying to load. Most languages have builtin json parsing, but rarely jsonc and similar support

9

u/Devatator_ 9d ago

C#'s System.Text.Json will reject comments by default but you can configure it to accept and even parse them if you want to read their content

19

u/Slight-Violinist-575 9d ago

Comments are for humans lmao

9

u/Devatator_ 9d ago

I mean someone could make some fucked up preprocessor using comments

8

u/Slight-Violinist-575 9d ago

I am sure someone could, and I am sure someone eventually will, and we will pay for it

4

u/Jonathan_the_Nerd 9d ago

That's exactly why JSON doesn't have comments. Some implementers were putting parsing directives in comments. That risked destroying interoperability. Rather than changing the spec to require stripping out comments before parsing, Douglas Crockford just disallowed comments altogether.

2

u/Slight-Violinist-575 9d ago

That’s an interesting read, thank you sir/ma’am

5

u/Turbulent_Stick1445 9d ago

There is a use case for reading comments (though it really needs to be optional) which is if your code is editing the JSON rather than just getting information for it.

eg:

configObject = loadJSON(".config/myapp/settings.json");
configObject.enableCrashes = true;
configObject.write(".config/myapp/settings.json");

But that means a fairly ugly JSON parser that would have to store a lot of positional information. I mean, it can be done, but...

1

u/TerminalVector 9d ago edited 9d ago

Man, what was it? Backspace? Backslash? Some kind of html templating language I had the displeasure to work with in one of my first jobs that used magic comments. I remember my onboarding buddy telling me they didn't do anything, and then discovering that no, they in fact drove the layout of the entire page. He'd been with the company over a year, and wasn't an idiot. It just doesn't make any sense and he hadn't really checked.

1

u/Slight-Violinist-575 9d ago

Jade/Pug? I once was a padawan and thought that was hella cool when I first learned about it.

Then life thought me to appreciate explicit boundaries, in every sense.

1

u/TerminalVector 9d ago

It was I thought it started with a B and was a typographical symbol, but maybe I'm misremembering. Underscore? That was a thing for sure but I don't remember if it was the same thing.

Edit: Knockout, that was it. Had it confused with Underscore.js. Not sure why I thought it started with a B

1

u/YeOldeMemeShoppe 9d ago
// #[agreed]

3

u/Scolmann 9d ago

It's not too hard to strip out comments with a regex, but yeah generally you wouldn't bother with stuff like that

2

u/the-liquidian 9d ago

Or use an existing tool to transpile it.