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

3

6

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.

Breakup answered 16/10, 2015 at 6:35 Comment(3)
"they can't find solution" - JSON as format allows to have null values, so any JSON lib in any language should support them.Briefs
If you are interested in transforming JSON substituting all nulls by empty qoutes then you can use JSON token stream approach and change every null token by "". It should be very efficient.Briefs
@Briefs I think, that transforming JSON is a totally wrong idea, and what I need - is a view from the outside.Breakup
M
3

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.

Montenegro answered 16/10, 2015 at 7:2 Comment(0)
M
1

The idea of serializing null as '' sounds crazy to me (ok, I am more a Java guy, but created many REST APIs). If deserializing null is a problem for the frontend I would suggest to leave those values out.

This has three advantages:

  1. No unused keys returned, the response becomes more clean and compact.

  2. The frontend has no hassle as it would simply not deserialize it.

  3. Leaving unused out makes API changes more easy as you don't need to handle unknown properties when first left blank before removing them

Most frameworks support this kind of handling (like Jackson ObjectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)).

Mcgrath answered 16/10, 2015 at 6:46 Comment(1)
Yes, it's crazy for me too! You know, i feeled myself like a heretic in a moment of descussion of this problem %)Breakup
H
0

Converting null to '' breaks the semantic of the object and sounds like a bad practice. Both Jackson and Gson allow null values and let you decide if to ignore field with null or empty values. You can decide case by case without breaking the object semantic

Harmonize answered 16/10, 2015 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.