Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null field in JSON: empty quotes, null value, or remove field?

I (server java developer) and two my colleagues (ios and Android developers) have a problem (in fact a dispute).

Opinion of the mobile developers: i have to replace in JSON, which they retrieving from my server, all null fields(this field can be field for my custom object) to empty quotes(note, that Java is a language with a static types). Reason of this: they can't find solution, which allows deserialize my JSON (map to object), if it will be have null values.

My opinion: java serializers can't manipulate on value-level, only on type level. So, I can write serializer only for some class. And, I don't know why, but empty quotes instead of null seems to me very bad practice, which can create many problems in future.

like image 275
Ivan Timoshin Avatar asked Oct 16 '15 06:10

Ivan Timoshin


People also ask

Is empty string null in JSON?

Oracle is very clear about this limitation in their JSON documentation : Because Oracle SQL treats an empty string as NULL there is no way to construct an empty JSON string ( "" ).

How do you indicate null in JSON?

To include null values in the JSON output of the FOR JSON clause, specify the INCLUDE_NULL_VALUES option.

How do I ignore null values in JSON object?

In order to ignore null fields at the class level, we use the @JsonInclude annotation with include. NON_NULL.


1 Answers

Let's suppose that your are trying to infer types to your fields, thus an hypothetical one named f usually contains numbers.

That makes sense to me, for you are combining a strongly typed language with a one that is not.

Once it happens that f is actually null server side, does it make sense to you to put an empty string in place of a null value? Not to me.

I usually invite my team to be strongly coherent about this kind of problem, for it could be even more tricky for a newcomer having such a mutable field. Moreover, being honest, an empty string does not tell me that you don't have an actual value, instead it looks to me that you had one and it was an empty string!!

That said, the approach that involves null values sounds to me far more coherent.

Besides the other proposals already discussed in the other responses, another solution could be to wipe out those fields, but it breaks the structure of their parent object and still can lead to problems that are far worse than having nulls there.

In the worst case, if they don't want to deal with null values, they can introduce a layer that substitute them with default ones to push around their side. That way, you'll ever manage to say that the api are meaningful.

like image 87
skypjack Avatar answered Sep 20 '22 12:09

skypjack