Questions tagged [typetoken]

28 questions
126
votes
7 answers

How to use TypeToken + generics with Gson in Kotlin

I'm unable to get a List of generic type from a custom class (Turns): val turnsType = TypeToken>() {}.type val turns = Gson().fromJson(pref.turns, turnsType) it said: cannot access '' it is 'public /*package*/' in 'TypeToken'
Juan Saravia
  • 6,812
  • 5
  • 24
  • 38
29
votes
1 answer

What is Type & TypeToken?

I have the following method in my project public void execute(final int apiId, final ResponseHandler handler, final Type type) and the type is determined using TypeToken as follows final Type serviceErrorType = new TypeToken<>() { …
Android
  • 462
  • 1
  • 5
  • 12
10
votes
2 answers

Create TypeToken for type argument of generic super type

I'm implementing a Gson TypeAdapter for two dependend generic models. interface ModelA {} interface ModelB extends ModelA> {} For this I need to get the TypeToken and TypeAdapter. By doing Type type = ((ParameterizedType)…
tynn
  • 33,607
  • 8
  • 80
  • 121
3
votes
1 answer

DexOpt: illegal method access with Gson TypeToken

I don't know since when, but now when I deploy my app to an android device, I see these logs in the logcat console: 08-31 10:01:45.437 2157-2219/com.company.clap I/dalvikvm: DexOpt: illegal method access (call…
Jean-François Côté
  • 3,750
  • 10
  • 48
  • 82
3
votes
2 answers

How to achieved an type-token ratio dictionary in NLTK ordered by fileids?

So I'm writing a program that will help me find the type-to-token ratio of all the the inaugural speeches of the presidents, and save it in the dictionary ttr. But for comparison's sake, I need the dictionary created at the end to go in the order of…
Auborey
  • 93
  • 1
  • 10
2
votes
1 answer

Getting Gson TypeToken from class name as a String

I want to do something like this - deserialize a class with generic type using Gson. The following code works like a charm. Type type = new TypeToken>() {}.getType(); ActionTask task = (ActionTask) gson.fromJson(json,…
BQffen
  • 63
  • 1
  • 7
2
votes
2 answers

NoClassDefFoundError: com/google/common/reflect/TypeToken

I have been using this api. The API is a Java wrapper for Mailchimp API with maven dependency. com.ecwid ecwid-mailchimp 2.0.1.0 I didn’t have…
WowBow
  • 5,709
  • 15
  • 55
  • 96
2
votes
1 answer

What are the effects of inner functions on instantiated objects?

The code below fails when constructing new TypeParameter[K]() {} Only the location of the mapToken differs. package net.ailive; import org.junit.Test; import com.google.common.reflect.TypeParameter; import…
user48956
  • 11,390
  • 14
  • 67
  • 125
1
vote
0 answers

How to read a parametrized List from file by using Gson?

I am trying to write a convenient method to read a List of objects from different files. Right now I have this one: public List parseList(String file) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try (Reader…
Oleksandr
  • 2,821
  • 6
  • 28
  • 59
1
vote
1 answer

Avoid double conversion from integer while reading data using Gson

I am saving data using json in aerospike: new Gson().toJson(data) While reading, I am using typetoken: Type type = new TypeToken>() { }.getType(); Map> myMap = new…
learner
  • 3,560
  • 3
  • 42
  • 87
1
vote
1 answer

Get generic type of T in Kotlin

I have this generic method: class Deserializer : ResponseDeserializable { override fun deserialize(reader: Reader): T? { val type = object: TypeToken() {}.type return Gson().fromJson(reader, type) } } but the…
William
  • 390
  • 6
  • 21
1
vote
1 answer

How do I perform Casting with TypeTokens in Java?

I am passing a TypeToken to a method. The code below works but I am afraid that by not explicitly defining Object list as something more specific is going to haunt me later but I cannot find a way to define it as or cast it as anything else without…
1
vote
0 answers

Text Parse Exception when Converting Gson response in Android

Hi I have problem with converting json response to my object when change device language!!! actually it is correct when my device set on english language but when i change the device language (for example to persian),i have exception!!! Please help…
MMM
  • 19
  • 1
  • 9
0
votes
0 answers

How can I create an instance of the custom type internally in a generic class on Android?

I am working on a generic class, which will fetch data remotely and convert it into object of the specified type. The following is the sample code: class A { int a; } class B extends A { int b; } class GA { private final…
Bill David
  • 63
  • 5
0
votes
1 answer

how to De-serialize a List> using Gson library

I need to De-serialize this Json string into a POJO. Json: [ { "first" : { "value" : 12, "suit" : { "name" : "heart" } }, "second" : { "value" : 12, …
Nate Schreiner
  • 551
  • 6
  • 14
1
2