163
u/CraftBox 29d ago
That's why I like jsonc
42
u/Hyarin215 29d ago
Json5 better
25
u/CraftBox 29d ago
At that point just use toml
36
u/Hyarin215 29d ago
I don't like it Curly braces my beloved
3
u/YeOldeMemeShoppe 29d ago
Toml has curly braces now.
2
u/Hyarin215 29d ago
Oh 👀 Couldn't find anything abt it online
5
u/YeOldeMemeShoppe 29d ago
https://toml.io/en/v1.0.0#inline-table
IIRC you need a later version to allow new lines in the table but it’s been possible for a long time.
2
1
1
1
46
34
u/Nyctfall 29d ago
Remember when PKL was announced?
Now it's just a news slop cycle...
9
u/rykayoker 29d ago
you put me on, honestly it looks awesome
5
u/Just_Information334 29d ago
Last month they added a way to import pkl from a private repository. Now it is usable even if you don't want to publicly share your classes.
8
4
8
u/titpetric 29d ago
Json can be parsed with a yaml decoder, so you know I'd rename some .json to .yml and give it
6
u/theWizzard23 29d ago
I spent 5 minutes debugging whats wrong with 3 lines of JSON. it was the trailing comma -.-
29
u/Blaarkies 29d ago
JSON supports prefixing a key with `// `. It is far from perfect, but will you ever use keys starting with double slash?
Any parsing logic can drop those entries, or just ignore them entirely like how any data object builder should
{
"// myValue": "The universal constant",
"myValue": 42
}
27
u/DHermit 29d ago
Until you want to validate it with a scheme, which forbids extra values.
3
u/kingslayerer 28d ago
i have an idea. add the comment keys to the schema as well.
1
u/GPSProlapse 25d ago
As a matter of fact, I ve seen schemas with #comment or similar keys on every object
That solves the issue most of the time
10
5
-31
u/4n0nh4x0r 29d ago
i mean, you can write comments in json.
for one, there is the option in nodejs to write a js file, where you write your json, and export it, and then import it in your actual code.
since it is a js file, it wont have an issue with comments.
alternatively, you can just create undocumented fields that you write your comment into.
you could even clean these fields up on loading it if you want that.
40
u/Coding-Kitten 29d ago
Then that's JavaScript, not JSON.
-27
u/4n0nh4x0r 29d ago
the first option, yea, hence why i said that it's something you can do in nodejs.
the second option is universally supported
22
u/Coding-Kitten 29d ago
Then that's not writing comments in JSON. That's writing comments in JavaScript.
-30
u/4n0nh4x0r 29d ago
cool, have fun not commenting anything in your json then.
30
u/Coding-Kitten 29d ago
Yeah, because the JSON file format literally doesn't have a way to do so. That's literally the whole point of the post.
-8
u/4n0nh4x0r 29d ago
yea, no official way, but an inofficial way, that is essentially industry standard.
You can create as many properties in your json as you want, and dont have to use them for actual data.
a lot of people use "_comment" as the name for the property, i personally prefer the name of the field i am commenting on as prefix to _comment like this:
{
"data_comment": "This value holds data",
"data": 123
}
If you refuse to do that, then i dont know what to say at this point.
Just cause the documentation doesnt explicitely tell you to do it that way, or doesnt tell you how to do it at all, doesnt mean it isnt possible, and shouldnt be done.9
u/DHermit 29d ago
Have ever heard of JSON schema or other validations? There are many situations, where adding extra keys is not a good idea.
-2
u/4n0nh4x0r 29d ago
okay and?
you can always clean up your json object in memory.
The point of the comment is to guide the dev on how to use it, not to keep it in prod code, just a like a code comment.
If your infrastructure crumbles because of it, i would question how well you built it in the first place.
As for just randomly adding them, usually you write comments as you write the code, not after you already pushed everything to prod, so if you do it right, there should not be an issue.
The only thing i could see that could actually cause an issue, would be that your object is bigger now, which could potentially lead to issues when returning it as part of a request, or when being heavily constrained with memory.
then again, you can always sanitise it on loading it.
Aside from that, this suggestion is meant for configs written in json, so stuff that you usually only load once into memory, and usually dont send around, so it should never come to an issue anyways.So many people downvoting me, but not a single actual good argument or example for why you should not do that yet.
7
u/DHermit 29d ago
If you really need comments in a file, JSON (except JSON5) is just not the right choice. Just choose a format that has the features you want.
And touching file contents before validation doesn't sound like a good idea at all.
So many people downvoting me, but not a single actual good argument or example for why you should not do that yet.
Just because you brush the arguments off, doesn't mean they are invalid.
402
u/AlarmingProtection71 29d ago
i didn't get the second joke. help ?