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
    34
    arrow-down
    9
    ·
    edit-2
    9 months 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
      40
      ·
      8 months 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
        6
        ·
        8 months 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.

    • sik0fewl@lemmy.ca
      link
      fedilink
      arrow-up
      8
      arrow-down
      3
      ·
      9 months ago

      Ya, having null semantics is one thing, but having different null and absent/undefined semantics just seems like a bad idea.

      • Username@feddit.de
        link
        fedilink
        arrow-up
        31
        arrow-down
        1
        ·
        9 months ago

        Not really, if absent means “no change”, present means “update” and null means “delete” the three values are perfectly well defined.

        For what it’s worth, Amazon and Microsoft do it like this in their IoT offerings.

        • expr@programming.dev
          link
          fedilink
          arrow-up
          5
          ·
          8 months ago

          Zalando explicitly forbids it in their RESTful API Guidelines, and I would say their argument is a very good one.

          Basically, if you want to provide more fine-grained semantics, use dedicated types for that purpose, rather than hoping every API consumer is going to faithfully adhere to the subtle distinctions you’ve created.

          • masterspace@lemmy.ca
            link
            fedilink
            English
            arrow-up
            1
            ·
            edit-2
            8 months ago

            They’re not subtle distinctions.

            There’s a huge difference between checking whether a field is present and checking whether it’s value is null.

            If you use lazy loading, doing the wrong thing can trigger a whole network request and ruin performance.

            Similarly when making a partial change to an object it is often flat out infeasible to return the whole object if you were never provided it in the first place, which will generally happen if you have a performance focused API since you don’t want to be wasting huge amounts of bandwidth on unneeded data.

        • eyeon@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          8 months ago

          it does feel ambiguous though as even what you outlined misses a 4th case. if null means delete, how do I update it to set the field to null?

        • 0x0@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          8 months ago

          It gets more fun if we’re talking SQL data via C API: is that 0 a field with 0 value or an actual NULL? Oracle’s Pro*C actually has an entirely different structure or indicator variables just to flag actual NULLs.

        • lad@programming.dev
          link
          fedilink
          English
          arrow-up
          1
          arrow-down
          2
          ·
          8 months ago

          Except, if you use any library for deserialization of JSONs there is a chance that it will not distinguish between null and absent, and that will be absolutely standard compliant. This is also an issue with protobuf that inserts default values for plain types and enums. Those standards are just not fit too well for patching

          • masterspace@lemmy.ca
            link
            fedilink
            English
            arrow-up
            3
            arrow-down
            1
            ·
            8 months ago

            I’ve never once seen a JSON serializer misjudge null and absent fields, I’ve just seen developers do that.

            • lad@programming.dev
              link
              fedilink
              English
              arrow-up
              1
              ·
              8 months ago

              Well, Jackson before 2.9 did not differentiate, and although this was more than five years ago now, this is somewhat of a counter example

              Also, you sound like serializers are not made by developers