- cross-posted to:
- programmer_humor@programming.dev
- cross-posted to:
- programmer_humor@programming.dev
Meme transcription: Panel 1. Two images of JSON, one is the empty object, one is an object in which the key name
maps to the value null
. Caption: “Corporate needs you to find the difference between this picture and this picture”
Panel 2. The Java backend dev answers, “They’re the same picture.”
- jsomae ( @jsomae@lemmy.ml ) 3•4 days ago
This is true in Lua as well (substituting ‘nil’ for 'null)
- remotelove ( @remotelove@lemmy.ca ) 12•5 days ago
I don’t get it. The key still gets declared, but it’s value is null. “name” in an empty object would return undefined, not null, correct?
(Yes, this joke whooshed, but I am curious now.)
Indeed, and that turns out to be a problem if the JavaScript expects the key not to be there, but instead it is there. And then you try to tell the backend dev that the key shouldn’t be there, but he’ll try to convince you that it’s the same whether the key is not there or whether it’s assigned
null
and then you wonder if he’s messing with you, but actually he isn’t and then the only thing keeping you sane is bitching about it in meme form on lemmy.Update your typescript definitions to make the field
String|undefined|null
and use operators like?.
. It’s the only way to stay sane as a frontend developer.If you’re using Typescript yet, may God bless your soul (and you can still use all the nice nullability operators on most cases).
For many uses it is semantically the same.
But for cases where you need to know if something was intentionally set to null or was simply not set, the difference is enormous.
there is no “undefined” in java. this would either be a map containing the key value pair (“name”, null) or it would be mapped to an object of some class with an attribute “name” which can hold a null value. in any case {} wont equal {“name”:null}.
- deadbeef79000 ( @deadbeef79000@lemmy.nz ) 6•5 days ago
Well. To Java that’s just a string of utf-8 characters, assuming you haven’t bastardised the encoding, and it’s just yanked out of an HTTP entity. So of course they’re different.
If you’re using some json parser and object mapping library (like Jackson) then all bets are off 'cause it could be configured any which way.
On every other language and library it’s whatever the defined behaviour is.
3/10
- apprehentice ( @apprehentice@lemmy.enchanted.social ) English6•5 days ago
C# devs be like
public string? Name { get; set; }
- hedgehog ( @hedgehog@ttrpg.network ) 5•5 days ago
If your Java dev is using Jackson to serialize to JSON, they might not be very experienced with Jackson, or they might think that a Java object with a null field would serialize to JSON with that field omitted. And on another project that might have been true, because Jackson can be configured globally to omit null properties. They can also fix this issue with annotations at the class/field level, most likely
@JsonInclude(Include.NON\_NULL)
.More details: https://www.baeldung.com/jackson-ignore-null-fields
Ah yes the difference between “unset” and “intentionally set to null”, the bane of API devs who work in languages that don’t inherently distinguish between the two.
- beefsquatch ( @beefsquatch@programming.dev ) 3•5 days ago
{"name": undefined}
is also different, checking with something like hasOwn - Gamma ( @GammaGames@beehaw.org ) English1•5 days ago
That’s what undefined is for
Undefined is not part of JSON specification. It’s also not a thing in Java.
- Gamma ( @GammaGames@beehaw.org ) English1•4 days ago
Yeah my bad, it would still require the java dev to leave the keys out
To be fair, just because
obj.foo
is undefined, that doesn’t mean the key is missing. It could also be assigned the value undefined.const obj = { foo: undefined }
vsconst obj = {}