-2

Here explained deserialization for classes with Generic types https://stackoverflow.com/a/8829656/4553806. Now suppese we I have some situation, but my Class has two constructors with different count of parameters.

    class TestClass(val par1:String, val par2:String)
    class TestClass(val par1:String)

So I know that my object is type of TestClass, but I don't know with which constructor it initialized.

How to deserialize?

Using this way

val fooType = object : TypeToken<Myclass<String>>() {}.type;
gson.fromJson(json, fooType);

or this

val fooType = object : TypeToken<Myclass<String, String>>() {}.type);
gson.fromJson(json, fooType);

could cause exception if objects fields count doesn't match.

If any one has experience with this case, please mention it here.

Tigran Babajanyan
  • 1,967
  • 1
  • 22
  • 41

1 Answers1

0

GSON is very easy to use, just set your serializedName and use the GSON converter to populate it. Here is a simple tutorial to follow.

https://www.tutorialspoint.com/gson/gson_quick_guide.htm

Sam
  • 5,342
  • 1
  • 23
  • 39