Meme transcription:

Panel 1: Bilbo Baggins ponders, “After all… why should I care about the difference between int and String?

Panel 2: Bilbo Baggins is revealed to be an API developer. He continues, “JSON is always String, anyways…”

107 points
*

To whoever does that, I hope that there is a special place in hell where they force you to do type safe API bindings for a JSON API, and every time you use the wrong type for a value, they cave your skull in.

Sincerely, a frustrated Rust dev

permalink
report
reply
37 points

“Hey, it appears to be int most of the time except that one time it has letters.”

throws keyboard in trash

permalink
report
parent
reply
16 points

Rust has perfectly fine tools to deal with such issues, namely enums. Of course that cascades through every bit of related code and is a major pain.

permalink
report
parent
reply
23 points

Sadly it doesn’t fix the bad documentation problem. I often don’t care that a field is special and either give a string or number. This is fine.

What is not fine, and which should sentence you to eternal punishment, is to not clearly document it.

Don’t you love when you publish a crate, have tested it on thousands of returned objects, only for the first issue be “field is sometimes null/other type?”. You really start questioning everything about the API, and sometimes you’d rather parse it as serde::Value and call it a day.

permalink
report
parent
reply
4 points

True, and also true.

permalink
report
parent
reply
1 point

This man has interacted with SAP.

permalink
report
parent
reply
10 points

The worst thing is: you can’t even put an int in a json file. Only doubles. For most people that is fine, since a double can function as a 32 bit int. But not when you are using 64 bit identifiers or timestamps.

permalink
report
parent
reply
34 points

That’s an artifact of JavaScript, not JSON. The JSON spec states that numbers are a sequence of digits with up to one decimal point. Implementations are not obligated to decode numbers as floating point. Go will happily decode into a 64-bit int, or into an arbitrary precision number.

permalink
report
parent
reply
-3 points

What that means is that you cannot rely on numbers in JSON. Just use strings.

permalink
report
parent
reply
4 points

Relax, it’s just JSON. If you wanted to not be stringly-typed, you’d have not used JSON.

(though to be fair, I hate it when people do bullshit types, but they got a point in that you ought to not use JSON in the first place if it matters)

permalink
report
parent
reply
2 points

As if I had a choice. Most of the time I’m only on the receiving end, not the sending end. I can’t just magically use something else when that something else doesn’t exist.

Heck, even when I’m on the sending end, I’d use JSON. Just not bullshit ones. It’s not complicated to only have static types, or having discriminant fields

permalink
report
parent
reply
4 points

You HAVE to. I am a Rust dev too and I’m telling you, if you don’t convert numbers to strings in json, browsers are going to overflow them and you will have incomprehensible bugs. Json can only be trusted when serde is used on both ends

permalink
report
parent
reply
2 points

This is understandable in that use case. But it’s not everyday that you deal with values in the range of overflows. So I mostly assumed this is fine in that use case.

permalink
report
parent
reply
1 point

Well, apart from float numbers and booleans, all other types can only be represented by a string in JSON. Date with timezone? String. BigNumber/Decimal? String. Enum? String. Everything is a string in JSON, so why bother?

permalink
report
parent
reply
2 points

I got nothing against other types. Just numbers/misleading types.

Although, enum variants shall have a label field for identification if they aren’t automatically inferable.

permalink
report
parent
reply
3 points

Well, the issue is that JSON is based on JS types, but other languages can interpret the values in different ways. For example, Rust can interpret a number as a 64 bit int, but JS will always interpret a number as a double. So you cannot rely on numbers to represent data correctly between systems you don’t control or systems written in different languages.

permalink
report
parent
reply
1 point
*

No problem with strings in JSON, until some smart developer you get JSONs from decides to interchangeably use String and number, and maybe a boolean (but only false) to show that the value is not set, and of course null for a missing value that was supposed to be optional all along but go figure that it was

permalink
report
parent
reply
48 points

“1” + “1”

permalink
report
reply
48 points

“11”

permalink
report
parent
reply
23 points

strings are in base two, got it

permalink
report
parent
reply
38 points

Wouldn’t the answer be “10” in that case?

permalink
report
parent
reply
9 points

That’s unary.

permalink
report
parent
reply
1 point

Strings are in base whatever roman numerals are.

permalink
report
parent
reply
7 points

int(“11”)

permalink
report
parent
reply
43 points

These JSON memes got me feeing like some junior dev out there is upset because they haven’t read and understood the docs.

permalink
report
reply
46 points

"true"

permalink
report
parent
reply
26 points

You guys have docs?

permalink
report
parent
reply
1 point

The code is my bible.

permalink
report
parent
reply
6 points

The schema is this SQL statement

permalink
report
parent
reply
0 points

You guys can read?

permalink
report
parent
reply
14 points

Timing is about right for it to be a batch of newly minted CS grads getting into their first corporate jobs.

permalink
report
parent
reply
6 points

Comments? Comments? Who needs comments?

permalink
report
parent
reply
20 points

I’ll have you know all of my code is stringly typed.

permalink
report
reply
1 point

All my binary code is stringy too.

permalink
report
parent
reply
18 points

CBOR for life, down with JSON.

permalink
report
reply
9 points

If there are no humans in the loop, sure, like for data transfer. But for, e.g., configuration files, i’d prefer a text-based solution instead of a binary one, JSON is a nice fit.

permalink
report
parent
reply
9 points
*

What, no! Use TOML or something for config files.

permalink
report
parent
reply
6 points

TOML

Interesting… me likes it.

permalink
report
parent
reply
1 point

Yaml is more human readable/editable, and it’s a superset of json!

permalink
report
parent
reply
2 points

Until someone cannot tell the difference between tab and space when configuring or you miss one indentation. Seriously, whoever thinks indentation should have semantic meaning for computers should burn in hell. Indentation is for us, humans, not computers. You can write a JSON with or without indentation if you want. Also, use JSON5 to have comments and other good stuff for a config file.

permalink
report
parent
reply
2 points

Yaml is just arcane bullshit to actually write as a human. Nor is it intuitively clear how yaml serializes.

permalink
report
parent
reply
1 point

Yaml is cancer.

permalink
report
parent
reply
1 point

It’s entirely disingenuous because who the hell is throwing JSON into YAML without converting it? Oh wow, I changed the file extension and it still works. I’m so glad we changed to YAML!

permalink
report
parent
reply
1 point

What I’d like for a configuration language is a parser that can handle in-place editing while maintaining whitespace, comments, etc. That way, automatic updates don’t clobber stuff the user put there, or (alternatively) have sections of ### AUTOMATIC GENERATION DO NOT CHANGE###.

You need a parser that handles changes on its own while maintaining an internal representation. Something like XML DOM (though not necessarily that exact API). There’s a handful out there, but they’re not widespread, and not on every language.

permalink
report
parent
reply
3 points

Hell, no. If I wanted to save bytes, I’d use a binary format, or just fucking zip the JSON. Looking at a request-response pair and quickly understanding the transferred data is invaluable.

permalink
report
parent
reply
3 points

If you’re moving away from text formats, might as well use a proper serialisation tool like protobuf…

permalink
report
parent
reply
-1 points

Yaml?

permalink
report
parent
reply
1 point

For the love of all things pure, holy, and just, please do not use YAML in your APIs…

permalink
report
parent
reply
1 point

Fine, and if you don’t use json in your API because of the deficiency highlighted in the meme, what format do you use in your API?

permalink
report
parent
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

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

Community stats

  • 8K

    Monthly active users

  • 393

    Posts

  • 4K

    Comments