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

  • bleistift2@sopuli.xyzOP
    link
    fedilink
    English
    arrow-up
    0
    ·
    6 days ago

    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.

    • PeriodicallyPedantic@lemmy.ca
      link
      fedilink
      arrow-up
      0
      ·
      5 days ago

      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.

    • Aceticon@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      5 days ago

      If I remember it correctly, per the JSON definition when a key is present but not expected it should be ignored.

      The reason for that is to maintain compatibility between versions: it should be possible to add more entries to the data and yet old versions of the software that consumes that data should still continue to operate if all the data they’re designed to handle is still there and still in the correct format.

      Sure, that’s not a problem in the blessed world of web-based frontends where the user browser just pulls the client code from the server so frontend and backend are always in synch, but is a problem for all other kinds of frontend out there where the life-cycle of the client application and the server one are different - good luck getting all your users to update their mobile apps or whatever whenever you want to add functionality (and hence data in client-server comms) to that system.

      (Comms API compatibility is actually one of the big problems in client-server systems development)

      So it sounds like an issue with the way your JavaScript library handles JSON or your own implementation not handling per-spec the presence of data which you don’t use.

      Granted, if the server side dev only makes stuff for your frontend, then he or she needs not be an asshole about it and can be more accomodating. If however that data also has to serve other clients, then I’m afraid you’re the one in the wrong since you’re demanding that the backwards compatibility from the JSON spec itself is not used by anybody else - which as I pointed out is a massive problem when you can’t guarantee that all client apps get updated as soon as the server gets updated - because you couldn’t be arsed to do your implementation correctly.

      • bleistift2@sopuli.xyzOP
        link
        fedilink
        English
        arrow-up
        0
        ·
        5 days ago

        So it sounds like an issue with […] handling per-spec the presence of data which you don’t use.

        The trouble is, in this specific use case, the data may either be there or it may not be, depending on authorization. I’m checking specifically if the key is present to determine whether the user has access to that data (and display a toggle for that data), or if the user mustn’t see it and thus doesn’t need the toggle.

        The wrong assumption was that if the key is there, then the value is not null. That assumption was invalidated by an update on the server side. Of course I could’ve checked if the value of the key is nullish. But it’s still annoying to have a breaking frontend because the backend changed how it serves its data.

        • Aceticon@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          5 days ago

          That sounds like an error in the specification of the client-server API or an erroneous implementation on the server side for the last version: nothing should be signaled via presence or absence of fields when using JSON exactly because, as I described in my last post, the standard with JSON is that stuff that is not present should be ignore (i.e. it has no meaning at all) for backwards compatibility, which breaks if all of the sudden presence or absence are treated as having meaning.

          Frankly that there isn’t a specific field signalling authorized/not-authorized leads me to believe that whomever has designed that API isn’t exactly experienced at that level of software design: authorization information should be explicit, not implicit, otherwise you end up with people checking for not-in-spec side effects like you did exactly for that reason (i.e. “is the no data being returned because of user not authorized or because there was indeed no data to retunr?”), which is prone to break since not being properly part of the spec means any of the teams working on it might interpret things differently and/or change them at any moment.

          • bleistift2@sopuli.xyzOP
            link
            fedilink
            English
            arrow-up
            0
            ·
            5 days ago

            Frankly that there isn’t a specific field signalling authorized/not-authorized

            The instance I was bitching about was this: There’s a lot of region-specific data coming from the backend. But the user is only authorized for certain regions. So for instance the North-American guy gets this object: { "CA": [/* list of stuff */], "US": [/* list of stuff */], "MX": [ /* list of stuff */ ]}, while the US-only guy only gets {"US": [ /* list of stuff */] }. Are you suggesting that the response should also include flags isCaPresent, isUsPresent, isMxPresent for every country?

            The issue with null vs not present surfaced because I, the frontend, checked if the returned object contained the key "CA" and then tried to iterate over the list of stuff, which happened to be null, which is hard to iterate over. I agree that I could’ve checked if the key was present and not null.

            The meme, however, was lamenting that the backend developer, refuses to acknowledge that these two JSONs are different,since they only see their POJOs, where both map to CA: null, US: [], MX: null.

    • 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).

    • essteeyou@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      6 days ago

      I’ve worked back and front end, and with a lot of developers, and I don’t think anyone would say they’re the same. Software devs are some of the most pedantic people out there.