Show me a better text format for serializing

3 years ago by onlinepersona to c/programmer_humor

Discover the magic of the internet at Imgur, a community powered entertainment destination. Lift your spirits with funny jokes, trending memes, entertaining gifs, inspiring stories, viral videos, and so much more from users.

And I'll show you YAML

(a continuation of this post)

suy 58 points 3 years ago path: 0 5291502, hotness: undefined, score: 58, children: 1
zorronsfw 1 point 3 years ago
path: 0 5291502 5298743, hotness: undefined, score: 1, children: 0
magic_lobster_party 52 points 3 years ago

JSON for serialization all the way. It’s simple and to the point. It does one thing and does it well. There’s little room for annoying surprises. Any JSON can easily be minified and prettified back and forth. If you want it in binary format you can convert it to BSON.

Yaml is too much of a feature creep. It tries to do way too many things at the same time. There are so many traps to fall into if you’re not cautious enough. The same thing can be written in multitudes of ways.

path: 0 5294140, hotness: undefined, score: 52, children: 8
jjjalljs 29 points 3 years ago

Yes, but whoever decided that json can't have trailing commas has my ire.

{ "a": 1,
  "b": 2,  <-- nope
}

There was some other pitfall I can't remember around missing keys and undefined, too, but I can't remember it now.

path: 0 5294140 5296515, hotness: undefined, score: 29, children: 5
adambowles 8 points 3 years ago

Trailing commas are supported in json5, as well as comments

path: 0 5294140 5296515 5306099, hotness: undefined, score: 8, children: 0
Sgtmoustache -2 points 3 years ago

I'm pretty sure you can have trailing commas...

path: 0 5294140 5296515 5305609, hotness: undefined, score: -2, children: 3
Bjornir 2 points 3 years ago

You can't but some parser allow them. But those that do do not respect the standard.

path: 0 5294140 5296515 5305609 5385244, hotness: undefined, score: 2, children: 1
Sgtmoustache 1 point 3 years ago

Good to know. Must be why I thought you could. Thanks.

path: 0 5294140 5296515 5305609 5385244 5452421, hotness: undefined, score: 1, children: 0
adambowles 1 point 3 years ago
path: 0 5294140 5296515 5305609 5306094, hotness: undefined, score: 1, children: 0
kool_newt 3 points 3 years ago
path: 0 5294140 5308765, hotness: undefined, score: 3, children: 1
kogasa 5 points 3 years ago

Toml

path: 0 5294140 5308765 5314327, hotness: undefined, score: 5, children: 0
NewPerspective 41 points 3 years ago

My problem with yaml is if you truncate it at any random spot, there's a high likelihood it's still valid yaml. I don't like the idea that things can continue without even knowing there's a problem. The single opening and closing curly braces enclosing a json object is all it takes to at least know you didn't receive the entire message. Toml has the same issue. I'll stick with json when it makes sense.

path: 0 5292543, hotness: undefined, score: 41, children: 2
pimeys 8 points 3 years ago

Add a schema to it and you get XML. The ultimate serialization format.

path: 0 5292543 5293332, hotness: undefined, score: 8, children: 1
marcos 13 points 3 years ago

Quite like YAML, XML has too many stuff in it. While a lot of parsers are not standard compliant and safe, if there's any chance the stuff you include on your code can evolve into a fully featured parser, including it is something to avoid.

There is this language called KDL that looks interesting.

path: 0 5292543 5293332 5296184, hotness: undefined, score: 13, children: 0
calcopiritus 39 points 3 years ago

Serializing? For serializing you probably want performance above all else. I'm saying this without checking any benchmark, but I'm sure yaml is more expensive to parse than other formats where indentation don't have meaning.

For human readability: it has to be readable (and writeable) by all humans. I know (a lot of people) that dislike yaml, toml and XML. I don't know of a single person that struggles to read/write json, there is a clear winner.

path: 0 5300944, hotness: undefined, score: 39, children: 15
DrM 20 points 3 years ago

JSON would be perfect if it allowed for comments. But it doesn't and that alone is enough for me to prefer YAML over JSON. Yes, JSON is understandable without any learning curve, but having a learning curve is not always bad. YAML provides a major benefit that is worth the learning curve and doesn't have the issues that XML has (which is that there is no way to understand an XML without also having the XSD for it)

path: 0 5300944 5304967, hotness: undefined, score: 20, children: 11
Michal 25 points 3 years ago

Json should also allow for trailing commas. There's no reason for it not too. It's annoying having to maintain commas.

path: 0 5300944 5304967 5305723, hotness: undefined, score: 25, children: 6
DerArzt 4 points 3 years ago

And also a standard date time type!

path: 0 5300944 5304967 5305723 5306054, hotness: undefined, score: 4, children: 5
dfyx 6 points 3 years ago

What is wrong with ISO 6801 strings?

path: 0 5300944 5304967 5305723 5306054 5311976, hotness: undefined, score: 6, children: 4
frezik 4 points 3 years ago

JSON5 has comments, among fixing a few other shortsighted limitations of the original.

path: 0 5300944 5304967 5334418, hotness: undefined, score: 4, children: 0
kogasa 1 point 3 years ago

If a comment isn't part of the semantic content of a JSON object it has no business being there. JSON models data, it's not markup language for writing config files.

You can use comments in JSON schema (in a standardized way) when they are semantically relevant: https://json-schema.org/...

For the data interchange format, comments aren't part of the JSON grammar but the option to parse non-JSON values is left open to the implementation. Many implementations do detect (and ignore) comments indicated by e.g. # or //.

path: 0 5300944 5304967 5314322, hotness: undefined, score: 1, children: 2
frezik 2 points 3 years ago

JSON models data, it’s not markup language for writing config files.

JavaScript package management promptly said otherwise. JSON is a config format no matter if you like it or not.

path: 0 5300944 5304967 5314322 5335733, hotness: undefined, score: 2, children: 1
kogasa 1 point 3 years ago

I've disagreed with JavaScript before, what makes you think I won't do it again?

Anyway, anything using JSON as a config language will also certainly use a JSON interpreter that can ignore comments. Sure that's "implementation specific," but so is a config file. You wouldn't use "MyApplication.config.json" outside the context of MyApplication loading its own configuration, so there's no need for it to be strictly compliant JSON as long as it plays nicely with most text editors.

path: 0 5300944 5304967 5314322 5335733 5342632, hotness: undefined, score: 1, children: 0
kool_newt 16 points 3 years ago
path: 0 5300944 5308720, hotness: undefined, score: 16, children: 0
kogasa 1 point 3 years ago

Serializing isn't necessarily about performance, or we'd just use protobuf or similar. I agree Json is a great all rounder. Combine with JSON object schema to define sophisticated DSLs that are still readable, plain JSON. TOML is nice as a configuration language, but its main appeal (readability) suffers when applied to complex modeling tasks. XML is quite verbose and maybe takes the "custom DSL" idea a little too far. YAML is a mistake.

path: 0 5300944 5314180, hotness: undefined, score: 1, children: 0
Blackmist 1 point 3 years ago

I don't know why we're fucking about trying to use text editors to manipulate structured data.

Yeah, it's convenient to just be able to use a basic text editor, but we're not trying to cram it all on a floppy disk here. I'm sure we could have a nice structured data editor somewhere for all those XML, JSON and YAML files we're supposed to maintain every day.

path: 0 5300944 5337994, hotness: undefined, score: 1, children: 0
synae 25 points 3 years ago

For serializing? I'd probably just go with json.

For content meant to be written or edited by humans? YAML all day baby

path: 0 5293649, hotness: undefined, score: 25, children: 3
Andy 5 points 3 years ago

Ever tried NestedText? It's like basic YAML but everything is a string (types are up to the code that ingests it), and you never ever need to escape a character.

path: 0 5293649 5294064, hotness: undefined, score: 5, children: 2
synae 9 points 3 years ago

I've got too many consumers that I don't control which dictate their input formats. And to be quite honest, "types are up to the code that ingests it" sounds like a huge negative to me.

path: 0 5293649 5294064 5294499, hotness: undefined, score: 9, children: 1
Andy 2 points 3 years ago

Ah, well I love that policy (types being in code, not configs). FWIW I sometimes use it as a hand-edited document, with a small type-specifying file, to generate json/yaml/toml for other programs to load.

path: 0 5293649 5294064 5294499 5295567, hotness: undefined, score: 2, children: 0
MagnoliaMayhem 18 points 3 years ago

Json. Your move, Joker.

path: 0 5296299, hotness: undefined, score: 18, children: 2
theterrasque 14 points 3 years ago

puts the json in the yaml parser

Your move, foolish mortal

path: 0 5296299 5300428, hotness: undefined, score: 14, children: 1
derpgon 7 points 3 years ago

For those uninitiated, every JSON is a valid YAML, since YAML is just a superset of JSON.

path: 0 5296299 5300428 5308196, hotness: undefined, score: 7, children: 0
colourlessidea 16 points 3 years ago

YAML is pretty good for readability, pretty awful for writability

path: 0 5291667, hotness: undefined, score: 16, children: 5
synae 10 points 3 years ago

Interesting, I find that the other reasonable options are far less writable than yaml

path: 0 5291667 5293517, hotness: undefined, score: 10, children: 0
NewPerspective 8 points 3 years ago

Rule of thumb: valid json is valid yaml. If you're ever unsure, do it the old fashioned way.

path: 0 5291667 5292563, hotness: undefined, score: 8, children: 3
intensely_human 1 point 3 years ago

I really don’t see how that’s true.

You’re telling me this is valid yaml?

{ firstName: “Intensely” }

How is that yaml?

path: 0 5291667 5292563 5326350, hotness: undefined, score: 1, children: 2
NewPerspective 4 points 3 years ago path: 0 5291667 5292563 5326350 5337228, hotness: undefined, score: 4, children: 0
Fal 0 points 3 years ago

No, because that's not valid json

path: 0 5291667 5292563 5326350 5326474, hotness: undefined, score: 0, children: 0
nxdefiant 13 points 3 years ago

If you have a choice to start from scratch, TOML is probably the better option.

path: 0 5291925, hotness: undefined, score: 13, children: 0
urquell 9 points 3 years ago

So much json here. All wrong, it's csv

path: 0 5327061, hotness: undefined, score: 9, children: 0
Windex007 6 points 3 years ago

Genuinely curious what features OP is looking for, specifically for serialization as per the post, that has resulted in the conclusion being yaml.

path: 0 5294752, hotness: undefined, score: 6, children: 0
kool_newt 5 points 3 years ago
path: 0 5308818, hotness: undefined, score: 5, children: 3
onlinepersona 4 points 3 years ago

text?

path: 0 5308818 5310180, hotness: undefined, score: 4, children: 2
kool_newt 4 points 3 years ago
path: 0 5308818 5310180 5325274, hotness: undefined, score: 4, children: 1
technom 1 point 3 years ago

Protobuf is also not a proper binary alternative for Yaml. Protobuf needs a schema in the form of its IDL. Cbor and messagepack might be more analogous.

path: 0 5308818 5310180 5325274 5332168, hotness: undefined, score: 1, children: 0
Artyom 5 points 3 years ago

Yaml is a great, human-readible file format. Unless there's an exclamation point in it, then it is an illegible Eldrich horror.

path: 0 5298700, hotness: undefined, score: 5, children: 0
JoYo 1 point 3 years ago

I didn't even know there was a difference.

path: 0 5291742, hotness: undefined, score: 1, children: 0
programmer_humor
programmer_humor

@programming.dev

login for more options
33694
2569
7739

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics
    • If the mod doesn't find it funny, you're banned. Ha-ha!... For real: do not use the community for "statements". There are other places for such content. Keep it chill and funny.

go to feed...