Cannot deserialize nested JSON using Retrofit (Android)
<p>I have JSON wrapped by "_embedded" tag like below:</p>

<pre><code>{
  "_embedded" : {
    "events" : [ { ... }]}
</code></pre>

<p>I'm using retrofit with interface:</p>

<pre><code>public interface IEventRest {
    @GET("/events/search/findByPlaceId")
    Observable&lt;List&lt;Event&gt;&gt; getEventList(@Query("placeId")String placeId);
}
</code></pre>

<p>And this is my REST class:</p>

<pre><code>public class EventRest implements IEventRest {
Gson gson = new GsonBuilder()
        .registerTypeAdapter(Event[].class, new MyDeserializer())
        .create();

private Retrofit getRetrofitClient() {
    final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    final OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    return new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(UrlUtils.URL)
            .client(client)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
}

@Override
public Observable&lt;List&lt;Event&gt;&gt; getEventList(String placeId) {
    final IEventRest placeRest = getRetrofitClient().create(IEventRest.class);

    return placeRest.getEventList(placeId)
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.newThread());
}

private class MyDeserializer implements JsonDeserializer&lt;List&lt;Event&gt;&gt; {
    @Override
    public List&lt;Event&gt; deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
            throws JsonParseException {
        JsonElement content = je.getAsJsonObject().get("_embedded");

        Type collectionType = new TypeToken&lt;Collection&lt;Event&gt;&gt;(){}.getType();
        return new Gson().fromJson(content, collectionType);

    }
}
}
</code></pre>

<p>But calling placeRest.getEventList(placeId) throws exception:</p>

<pre><code>java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
  at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:57)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
  at java.lang.Thread.run(Thread.java:818)
Caused by: rx.exceptions.OnErrorNotImplementedException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:386)
  at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:383)
  at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44)
  at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:152)
  at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
  at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
  at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
  at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) 
  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
  at java.lang.Thread.run(Thread.java:818) 
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351)
  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 retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
  at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
  at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
  at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
  at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
  at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
  at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
  at rx.Subscriber.setProducer(Subscriber.java:211)
  at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
  at rx.Subscriber.setProducer(Subscriber.java:205)
  at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
  at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
  at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
  at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
  at rx.Observable.unsafeSubscribe(Observable.java:9860)
  at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
  at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:221)  
</code></pre>

<p>Could you help me to find out what is wrong? Thank you in advance.</p>