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

1 point

Had to solve this with Go recently, which was not as straightforward as I’d hoped! https://www.jvt.me/posts/2024/01/09/go-json-nullable/

permalink
report
reply
2 points

Just what every programming language needs, not one, but two types of null! Because nobody ever said one type was difficult enough.

If I see any of you make this distinction matter for anything other than “PUT vs. PATCH” semantics I’m going to be very angry.

permalink
report
reply
1 point

I do this constantly. undefined: not retrieved yet. null: Error when retrieving. Makes it easy to reason about what the current state of the data is without the need for additional status flags.

permalink
report
parent
reply
11 points
3 points

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

permalink
report
parent
reply
32 points
*

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

permalink
report
reply
52 points

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

permalink
report
parent
reply
6 points

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.

permalink
report
parent
reply
2 points

JSON patch is a dangerous thing to use over a network. It will allow you to change things inside array indices without knowing whether the same thing is still at that index by the time the server processes your request. That’s a recipe for race conditions.

permalink
report
parent
reply
-5 points

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.

permalink
report
parent
reply
8 points

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

permalink
report
parent
reply
38 points

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.

permalink
report
parent
reply
3 points

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?

permalink
report
parent
reply
-1 points

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

permalink
report
parent
reply
8 points

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.

permalink
report
parent
reply
1 point

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.

permalink
report
parent
reply
5 points
*

This is also how it is defined in the JSON Merge Patch RFC.

permalink
report
parent
reply

Programmer Humor

!programmer_humor@programming.dev

Create post

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

  • Keep content in english
  • No advertisements
  • Posts must be related to programming or programmer topics

Community stats

  • 9.4K

    Monthly active users

  • 860

    Posts

  • 33K

    Comments