Meme transcription:

Panel 1: Bilbo Baggins ponders, “After all… why should I care about the difference between int and String?

Panel 2: Bilbo Baggins is revealed to be an API developer. He continues, “JSON is always String, anyways…”

  • andyburke@fedia.io
    link
    fedilink
    arrow-up
    37
    arrow-down
    2
    ·
    6 months ago

    These JSON memes got me feeing like some junior dev out there is upset because they haven’t read and understood the docs.

  • brian@programming.dev
    link
    fedilink
    arrow-up
    6
    arrow-down
    2
    ·
    6 months ago

    json doesn’t have ints, it has Numbers, which are ieee754 floats. if you want to precisely store the full range of a 64 bit int (anything larger than 2^53 -1) then string is indeed the correct type

    • bleistift2@sopuli.xyzOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      6 months ago

      json doesn’t have ints, it has Numbers, which are ieee754 floats.

      No. numbers in JSON have arbitrary precision. The standard only specifies that implementations may impose restrictions on the allowed values.

      This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754 binary64 (double precision) numbers [IEEE754] is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision. A JSON number such as 1E400 or 3.141592653589793238462643383279 may indicate potential interoperability problems, since it suggests that the software that created it expects receiving software to have greater capabilities for numeric magnitude and precision than is widely available.

      https://www.rfc-editor.org/rfc/rfc8259.html#section-6

  • VeganPizza69 Ⓥ@lemmy.world
    link
    fedilink
    arrow-up
    4
    arrow-down
    1
    ·
    6 months ago

    It’s the API’s job to validate it either way. As it does that job, it may as well parse the string as an integer.

      • bleistift2@sopuli.xyzOP
        link
        fedilink
        English
        arrow-up
        12
        ·
        6 months ago

        Or even funnier: It gets parsed in octal, which does yield a valid zip code. Good luck finding that.

        • kamen@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          6 months ago

          Oof.

          I guess this is one of the reasons that some linters now scream if you don’t provide base when parsing numbers. But then again good luck finding it if it happens internally. Still, I feel like a ZIP should be treated as a string even if it looks like a number.

          • bitfucker@programming.dev
            link
            fedilink
            arrow-up
            3
            ·
            6 months ago

            Yep. Much like we don’t treat phone numbers like a number. The rule of thumb is that if you don’t do any arithmetic with it, it is not a “number” but numeric.

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

              Well, we don’t, but every electonic tables software out in the wild on the other hand…

              /j

              Yes, I know that you can force it to become text by prepending ' to the phone, choose an appropriate format for the cells, etc, etc

              The point is that this often requires meddling after the phone gets displayed as something like 3e10

        • raman_klogius@ani.social
          link
          fedilink
          English
          arrow-up
          3
          ·
          6 months ago

          Who tf decided that a 0 prefix means base 8 in the first place? If a time machine was invented somehow I’m going to cap that man, after the guy that created JavaScript.

          • bleistift2@sopuli.xyzOP
            link
            fedilink
            English
            arrow-up
            4
            ·
            6 months ago

            I’m not sure if you’re getting it, so I’ll explain just in case.

            In computer science a few conventions have emerged on how numbers should be interpreted, depending on how they start:

            • decimal (the usual system with digits from 0 to 9): no prefix
            • binary (digits 0 and 1): prefix 0b, so 0b1001110
            • octal (digits 0 through 7): prefix 0, so 0116
            • hexadecimal (digits 0 through 9 and then A through E): prefix 0x, so 0x8E

            If your zip code starts with 9, it won’t be interpreted as octal. You’re fine.

            • xthexder@l.sw0.com
              link
              fedilink
              arrow-up
              5
              ·
              edit-2
              6 months ago

              Well, you’re right. I wasn’t getting it, but I’ve also never seen any piece of software that would treat a single leading zero as octal. That’s just a recipe for disaster, and it should use 0o116 to be unambiguous

              (I am a software engineer, but was assuming you meant it was hardcoded to parse as octal, not some weird auto-detect)

                • xthexder@l.sw0.com
                  link
                  fedilink
                  arrow-up
                  5
                  ·
                  edit-2
                  6 months ago

                  Interesting that strtol in C does that. I’ve always explicitly passed in base 10 or 16, but I didn’t know it would auto-detect if you passed 0. TIL.

              • Doc Avid Mornington@midwest.social
                link
                fedilink
                English
                arrow-up
                2
                ·
                6 months ago

                It’s been a long time, but I’m pretty sure C treats a leading zero as octal in source code. PHP and Node definitely do. Yes, it’s a bad convention. It’s much worse if that’s being done by a runtime function that parses user input, though. I’m pretty sure I’ve seen that somewhere in the past, but no idea where. Doesn’t seem likely to be common.