-
-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Differentiate between JSON 'key = null' and 'key not set' #39
Comments
If you want to omit empty field, field need to be pointer or primitive type, I try to create a tool to solve this problem. |
Although @honeycruse example is for marshaling, the problem is at the receiving end. After unmarshaling, how to know if the field is null because it was set to nul or because it was missing. If you use a pointer it will be nil in both cases, so it does not solve the problem. A nice solution would be to have another field in the struct in addition to the |
Yep, it would be nice to implement this feature. Good implementation of you talk about I saw in pgtype.Status link |
@guregu is there any interest in tackling this problem with this library? |
We are looking at solving this as part of oapi-codegen/runtime#26 for a similar issue for use with OpenAPI. Would it be of interest to expose this code as a library on its own, so these cases (as well as #67) can be filled? |
I've released this library as https://github.com/oapi-codegen/nullable - it's not at all reliant on OpenAPI, and can be used as standalone, or feel free to copy-paste the (Apache-2.0) code for your own usages! This handles:
And both marshalling and unmarshalling 👏 |
What is the difference between the field that we set to null and the field that was missing? In both cases, the result will be null. Or nil. For what purposes do we need to know that the field was explicitly set to null? In my opinion, it's just an empty meaning, and whether we put it up ourselves or it's just missing, it doesn't really matter. And we just take the meaning that is, that is, nil. |
Not true, there are semantic differences for some use cases. It may not be the case for everyone, but given the folks requesting it ☝️ as well as users of oapi-codegen requiring it, I've written about it in more detail in https://www.jvt.me/posts/2024/01/09/go-json-nullable/ which goes alongside the library release, and for instance is used by https://www.rfc-editor.org/rfc/rfc7386 to mean different things:
The library we've written handles all three cases, for marshalling and unmarshalling 👍 |
@jamietanna I'm sorry, but I still didn't understand the difference between not set and null. And how should we take this into account in the processing of values. If you look at the list of types used Then, as I understand it, null is used to indicate that the field isn't set. And as a result, it should be used as the first case. |
Via this feature request, there's the idea that these are two different expressions: {
} and {
"field": null
} Go, by default, only allows us to say whether something is there ( This library's types allow making that easier, but we still miss out on the expression of "it is there, but it is explicitly set to Not sure if anyone else who's looking for this feature have their own use-cases they'd like to share? |
The result will be used in the future. To do this, we conduct But what in the end? https://go.dev/play/p/N_dMIfMvGts
Thus, the result is identical. Whether we used a null value or an empty value. |
I can understand why people want this. Usually they have PATCH APIs where they want to differentiate between "don't touch this value" and "set this value to null". I try to avoid making these kinds of APIs but they do exist. You can get this by using e.g. |
In JSON there are effectively two types of null.
This is important when constructing a PATCH endpoint because:
There is a more detailed discussion of this issue here, with example code of how to solve the problem:
https://www.calhoun.io/how-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided/
It would be nice if the null library can support this.
The text was updated successfully, but these errors were encountered: