Moshi: Deserialize a parameterized object without knowing its associated type.

Created on 19 Jun 2016  Â·  5Comments  Â·  Source: square/moshi

Given these classes:

public class Mock {

}

public class MockParameterized<T> {
  private final T t;

  public MockParameterized(T t) {
    this.t = t;
  }

}

Is it possible to deserialize a MockParameterized object wihtout supplying its associated Type?

JsonAdapter<T> jsonAdapter = moshi.adapter(MockParameterized.class)
jsonAdapter.fromJson(jsonString)

With the previous snippet Moshi throws this IllegalArgumentException:

Platform T annotated [] requires explicit JsonAdapter to be registered.

But, using Gson, when supplying only the MockParameterized.class, it ends up instantiating an object which its generic field is a type of com.google.gson.internal.LinkedTreeMap

With Jackson, the unknown type will be mapped to a java.util.LinkedHashMap.

So, is there any specifyc reasson for this behaviour? Is there a way using Moshi to instantiate a parameterized object without knowing its type?

It seems a detail, but for me is a really big issue. I'm trying to give support to Moshi for my cache library, but, because some internal details, I need to instantie an object without knowing its type, so I'll be glad if you could give me some advice.

Most helpful comment

I am working on this.... slowly

On Sat, Jun 18, 2016, 7:00 PM Víctor Albertos [email protected]
wrote:

Given these classes:

public class Mock {

}
public class MockParameterized {
private final T t;

public MockParameterized(T t) {
this.t = t;
}

}

Is it possible to deserialize a MockParameterized object wihtout
supplying its associated Type?

JsonAdapter jsonAdapter = moshi.adapter(MockParameterized.class)
jsonAdapter.fromJson(jsonString)

With the previous snippet Moshi throws this IllegalArgumentException:

Platform T annotated [] requires explicit JsonAdapter to be registered.

But, using Gson, when supplying only the MockParameterized.class, it ends
up instantiating an object which its generic field is a type of
com.google.gson.internal.LinkedTreeMap
https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java

With Jackson, the unknown type will be mapped to a java.util.LinkedHashMap
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html.

So, is there any specifyc reasson for this behaviour? Is there a way using
Moshi to instantiate a parameterized object without knowing its type?

It seems a detail, but for me is a really big issue. I'm trying to give
support to Moshi for my cache library, but, because some internal details,
I need to instantie an object without knowing its type, so I'll be glad if
you could give me some advice.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/172, or mute the thread
https://github.com/notifications/unsubscribe/AAEEESpnuAnb1oHBn0ij94n2jLxEMBHuks5qNHiFgaJpZM4I5DSG
.

All 5 comments

I am working on this.... slowly

On Sat, Jun 18, 2016, 7:00 PM Víctor Albertos [email protected]
wrote:

Given these classes:

public class Mock {

}
public class MockParameterized {
private final T t;

public MockParameterized(T t) {
this.t = t;
}

}

Is it possible to deserialize a MockParameterized object wihtout
supplying its associated Type?

JsonAdapter jsonAdapter = moshi.adapter(MockParameterized.class)
jsonAdapter.fromJson(jsonString)

With the previous snippet Moshi throws this IllegalArgumentException:

Platform T annotated [] requires explicit JsonAdapter to be registered.

But, using Gson, when supplying only the MockParameterized.class, it ends
up instantiating an object which its generic field is a type of
com.google.gson.internal.LinkedTreeMap
https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java

With Jackson, the unknown type will be mapped to a java.util.LinkedHashMap
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html.

So, is there any specifyc reasson for this behaviour? Is there a way using
Moshi to instantiate a parameterized object without knowing its type?

It seems a detail, but for me is a really big issue. I'm trying to give
support to Moshi for my cache library, but, because some internal details,
I need to instantie an object without knowing its type, so I'll be glad if
you could give me some advice.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/172, or mute the thread
https://github.com/notifications/unsubscribe/AAEEESpnuAnb1oHBn0ij94n2jLxEMBHuks5qNHiFgaJpZM4I5DSG
.

Does this work?

Type type = Types.newParameterizedType(MockParameterized.class, Object.class);
JsonAdapter<MockParameterized<?>> jsonAdapter = moshi.adapter(type);

It does, thanks :)

Actually, the 3 libraries behaviour similarly adopting this approach.

I don't close the issue because it seems that this feature is planing to be implemented, eventually.

I'm unsure what else we’d implement actually.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rephiscorth picture rephiscorth  Â·  5Comments

PaulWoitaschek picture PaulWoitaschek  Â·  4Comments

cdongieux picture cdongieux  Â·  4Comments

stefanmedack picture stefanmedack  Â·  4Comments

abler98 picture abler98  Â·  4Comments