Hacker News new | ask | show | jobs
by masklinn 1652 days ago
> How do you implement a PUT endpoint where a missing JSON value is treated different than a null JSON value?

Tbf that’s a pain in the ass everywhere unless you’re reifying it as a map (so manipulating a json dom).

Iirc in rust the “complete” way to do this for a struct (as opposed to a map) with serde require two options and a bespoke deserializer.

1 comments

It's much easier to do in dynamically typed languages or by using maps - agreed. Unfortunately we have other design decisions that force us into using structs for deserialization on this endpoint (part of our validation strategy.)

The Go answer is a struct that contains an IsDefined boolean (i.e. your first option), and a pointer-to-value (i.e. your second option.)

This is fine if you need it, but having to write this same logic over and over again for every type gets old... especially if your validators are tied to your types (i.e. a type per field.)

> Unfortunately we have other design decisions that force us into using structs for deserialization on this endpoint (part of our validation strategy.)

Oh I’m not blaming you, sorry if it came across that way. I’m aware of the issue because i’ve been hit by the exact same (hence having been made aware of the rust workaround), can’t say I was a happy camper.