3

My POJO:

public class Catalog {

private Integer id;
private Doc doc;
private List<Image> images;

If incoming json is like this:

{
    "catalog": {
        "id": 1,
        "doc": {
            "url": "some_url",
            "deletedAt": "2017-10-20T14:10:01.797Z",
            "updatedAt": "2017-10-20T14:10:01.797Z"
        },
        "images": []
    }
}

GSON success deserilaize from json to POJO. OK!

But if incoming json is like this:

{
    "catalog": {
        "id": 1,
        "doc": {
            "url": "some_url",
            "deletedAt": "2017-10-20T14:10:01.797Z",
            "updatedAt": "2017-10-20T14:10:01.797Z"
        },
        "images": null
    }
}

I get errror:

onFailure: Error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 113 column 17 path $[0].some[0].catalog.images
 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 113 column 17 path $[0].offers[0].catalog.images
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
    at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 113 column 17 path $[0].offers[0].catalog.images
    at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)

This is because field "images" is null.

How to ignore (not deserialize) field when it value is null?

Or maybe there is another good solution? E.g. convert null to empty list.

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • 2
    It's not duplicate. I need deserialize (not serialize) – Alexei Oct 23 '17 at 12:49
  • Have you tried initialising your list? Gson should know the difference between null and empty list and for a quick example it seemed to work. – zoku Oct 23 '17 at 13:30
  • Yes but it not help. private List images = new ArrayList(); – Alexei Oct 23 '17 at 13:42
  • Have you checked [this answer](https://stackoverflow.com/questions/33435683/deserialize-with-gson-and-null-values) – miradham Oct 23 '17 at 13:59
  • I write custom gson JsonDeserializer. And check field "images" for null. And do what I need. But this is not good solution. Imagine if in incoming json has 10 null values in different nodes. So I need to create 10 custom JsonDeserializer? It's not good. – Alexei Oct 24 '17 at 07:24

0 Answers0