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.”

  • Thanks for the transcription!

    Surely Java can tell the difference between a key with a null value and the absence of that key, no?

    I mean, you can set up your deserialization to handle nulls in different ways, but a string to object dictionary would capture this, right?

    •  Lysergid   ( @Lysergid@lemmy.ml ) 
      link
      fedilink
      6
      edit-2
      3 months ago

      Kinda, I guess we all can agree it’s more typical to deserialize into POJO where theres is no such thing as missing field. Otherwise why would you choose Java if you don’t use types. This great precondition for various stupid hacks to achieve „patching” resources, like blank strings or negative numbers for positive-only fields or even Optional as a field.

        • That’s exactly not the thing, because nobody broke the contract, they simply interpret it differently in details.

          Having a null reference is perfectly valid json, as long as it’s not explicitly prohibited. Null just says “nothing in here” and that’s exactly what an omission also communicates.

          The difference is just whether you treat implicit and explicit non-existence differently. And neither interpretation is wrong per contract.

          • I think we’re fully in agreement here: if the API doesn’t specify how to handle null values, that omission means they’re perfectly valid and expected.

            Imagine a delivery company’s van exploding if somebody attempts to ship an empty box. That would be a very poorly built van.

          • Null means I’m telling you it’s null.

            Omission means it’s not there and I’m not telling you anything about it.

            There is a world of difference between those two statements. It’s the difference between telling someone you’re single or just sitting there and saying nothing.

            • I (think, at least) the point they’re making is that unless the API contract specifically differentiates between “present and null” and “absent” then there is no difference. (Specifically for field values.)

              • The point I’m making is kind of the opposite, unless the contract explicitly states that they’re the same they should not be treated as the same, because at a fundamental level they are not the same thing even if Java wants to treat them as such.

                  • At the (SQL) database level, if you are using null in any sane way, it means “this value exists but is unknown”. Conflating that with “this value does not exist” is very dangerous. JavaScript, the closest thing there is to a reference implementation for json serialization, drops attributes set to undefined, but preserves null. You seem to be insisting that null only means “explicit omission”, but that isn’t the case. Null means a variety of subtly different things in different contexts. It’s perfectly fine to explicitly define null and missing as equivalent in any given protocol, but assuming it is not.