VictorAlbertos / ReactiveCache

A reactive cache for Android and Java which honors the reactive chain.
Apache License 2.0
240 stars 24 forks source link

Generic types? #6

Closed PaulWoitaschek closed 7 years ago

PaulWoitaschek commented 8 years ago

I'm using moshi for data serialization.

Now I added my custom Optional<T> class I want to serialize.

However I get an IllegalArgumentException of Platform T annotated [] requires explicit JsonAdapter to be registered

Caused by: java.lang.IllegalArgumentException: Platform T annotated [] requires explicit JsonAdapter to be registered
at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:41)
at com.squareup.moshi.Moshi.adapter(Moshi.java:94)
at com.squareup.moshi.ClassJsonAdapter$1.createFieldBindings(ClassJsonAdapter.java:81)
at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:65)
at com.squareup.moshi.Moshi.adapter(Moshi.java:94)
at com.squareup.moshi.StandardJsonAdapters$ObjectJsonAdapter.toJson(StandardJsonAdapters.java:322)
at com.squareup.moshi.JsonAdapter$1.toJson(JsonAdapter.java:75)
at com.squareup.moshi.ClassJsonAdapter$FieldBinding.write(ClassJsonAdapter.java:204)
at com.squareup.moshi.ClassJsonAdapter.toJson(ClassJsonAdapter.java:173)
at com.squareup.moshi.JsonAdapter$1.toJson(JsonAdapter.java:75)
at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:44)
at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:50)
at io.victoralbertos.jolyglot.MoshiSpeaker.toJson(MoshiSpeaker.java:58)
    at io.rx_cache.internal.Disk.save(Disk.java:111)
    at io.rx_cache.internal.Disk.saveRecord(Disk.java:57)
    at io.rx_cache.internal.cache.SaveRecord.save(SaveRecord.java:50)
at io.rx_cache.internal.cache.TwoLayersCache.save(TwoLayersCache.java:45)
at io.rx_cache.internal.ProcessorProvidersBehaviour$7.call(ProcessorProvidersBehaviour.java:137)
at o.rx_cache.internal.ProcessorProvidersBehaviour$7.call(ProcessorProvidersBehaviour.java:119)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)

Moshi is registered with my factory producing optionals:

public final class OptionalFactory implements JsonAdapter.Factory {

   @Override public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
      if (type instanceof ParameterizedType && Optional.class == ((ParameterizedType) type).getRawType()) {
         Type innerType = ((ParameterizedType) type).getActualTypeArguments()[0];
         JsonAdapter a = moshi.adapter(innerType);
         //noinspection unchecked
         return new OptionalAdapter<>(a);
      }
      return null;
   }
}

but type instanceof ParameterizedType always evaluates to false.

VictorAlbertos commented 8 years ago

I don't use Moshi -sorry, so I'm not familiar with its factories. In any case, I think if there were some problem here, it will be related with Jolyglot, specifically with the moshi implementation as json provider.

If you will, you can try to narrow the problem and provide a failing test to point me out to the right direction.

Just to be sure, you supplied your instance of Moshi when you created the MoshiSpeaker instance, right?

Moshi moshi = new Moshi.Builder()
    .add(new OptionalFactory())
    .build();
Jolyglot jolyglot = new MoshiSpeaker(moshi) ;