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

  • Excel@lemmy.megumin.org
    link
    fedilink
    arrow-up
    31
    arrow-down
    9
    ·
    edit-2
    2 days ago

    If you’re branching logic due to the existence or non-existence of a field rather than the value of a field (or treating undefined different from null), I’m going to say you’re the one doing something wrong, not the Java dev.

    These two things SHOULD be treated the same by anybody in most cases, with the possible exception of rejecting the later due to schema mismatch (i.e. when a “name” field should never be defined, regardless of the value).

    • paholg@lemm.ee
      link
      fedilink
      English
      arrow-up
      34
      ·
      2 days ago

      They’re semantically different for PATCH requests. The first does nothing, the second should unset the name field.

      • expr@programming.dev
        link
        fedilink
        arrow-up
        7
        ·
        1 day ago

        Only if using JSON merge patch, and that’s the only time it’s acceptable. But JSON patch should be preferred over JSON merge patch anyway.

        Servers should accept both null and undefined for normal request bodies, and clients should treat both as the same in responses. API designers should not give each bespoke semantics.

        • masterspace@lemmy.ca
          link
          fedilink
          English
          arrow-up
          0
          arrow-down
          3
          ·
          12 hours ago

          Why?

          Because Java struggles with basic things?

          It’s absurd to send that much data on every patch request, to express no more information, but just to appease the shittiness of Java.

    • bleistift2@sopuli.xyzOP
      link
      fedilink
      English
      arrow-up
      3
      ·
      24 hours ago

      I’m sad that there are people who call themselves developers who need to be told this.

    • bleistift2@sopuli.xyzOP
      link
      fedilink
      English
      arrow-up
      30
      ·
      2 days ago

      For those who don’t know:

      Speaking at a software conference in 2009, Tony Hoare hyperbolically apologized for “inventing” the null reference:[26] [27]

      I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

      https://en.wikipedia.org/wiki/Tony_Hoare

      • Ephera@lemmy.ml
        link
        fedilink
        arrow-up
        6
        arrow-down
        1
        ·
        1 day ago

        Huh, so Tony Hoare invented null and then Graydon Hoare invented Rust, immediately terminating the existence of null which does not have a traditional null value.

  • MostlyBlindGamer@rblind.com
    link
    fedilink
    English
    arrow-up
    12
    arrow-down
    1
    ·
    2 days ago

    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?

    • NigelFrobisher@aussie.zone
      link
      fedilink
      arrow-up
      2
      ·
      1 day ago

      You can always bind the JSON to a hashmap implementation, as that’s all JSON is anyway. It’s not pretty but it works.

    • bleistift2@sopuli.xyzOP
      link
      fedilink
      English
      arrow-up
      16
      arrow-down
      1
      ·
      2 days ago

      Sure, Java can tell the difference. But that doesn’t mean that the guy writing the API cares whether or not he adds a key to the dictionary before yeeting it to the client.

    • Lysergid@lemmy.ml
      link
      fedilink
      arrow-up
      6
      ·
      edit-2
      2 days 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.

    • AggressivelyPassive@feddit.de
      link
      fedilink
      arrow-up
      3
      ·
      2 days ago

      It can, but especially during serialization Java sometimes adds null references to null values.

      That’s usually a mistake by the API designer and/or Java dev, but happens pretty often.

      • MostlyBlindGamer@rblind.com
        link
        fedilink
        English
        arrow-up
        2
        ·
        2 days ago

        That’s the thing though, isn’t it? The devs on either side are entering into a contract (the API) that addresses this issue, even if by omission. Whoever breaks the contract must rightfully be ejected into the stratosphere.

        • AggressivelyPassive@feddit.de
          link
          fedilink
          arrow-up
          1
          arrow-down
          1
          ·
          2 days ago

          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.

          • masterspace@lemmy.ca
            link
            fedilink
            English
            arrow-up
            0
            ·
            23 hours ago

            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.

            • AggressivelyPassive@feddit.de
              link
              fedilink
              arrow-up
              0
              ·
              23 hours ago

              Nope.

              If there’s a clear definition that there can be something, implicit and explicit omission are equivalent. And that’s exactly the case we’re talking about here.

              • masterspace@lemmy.ca
                link
                fedilink
                English
                arrow-up
                0
                ·
                edit-2
                18 hours ago

                Sure, in a specific scenario where you decide they’re equivalent they are, congratulations. They’re not generally.

                • AggressivelyPassive@feddit.de
                  link
                  fedilink
                  arrow-up
                  0
                  ·
                  22 hours ago

                  Did you read the comments above?

                  You can’t just ignore context and proclaim some universal truth, which just happens to be your opinion.

          • MostlyBlindGamer@rblind.com
            link
            fedilink
            English
            arrow-up
            4
            ·
            2 days ago

            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.