1

I am trying to serialize derived object using Gson in Kotlin and on serialization I am getting only parent class attributes Gson is ignoring all derived class attributes. Here is my code:

fun main(args: Array<String>) {
    val somename = Sample(1, "somename")
    val car = Car(somename, 4)
    val car2 = Car(somename, 4)
    val vehicles = object : ArrayList<Vehicle>() {

    }
    vehicles.add(car)
    vehicles.add(car2)
    val sampleClassResponse = SampleClassResponse(1, vehicles)
    val gson = Gson()
    val x = gson.toJson(sampleClassResponse)
    println(x)
    //        System.out.println(car.tyre);
}

// Base class:

public class Vehicle {
int number;
String name;
}

Sub class:

import Sample.Sample;

public class Car extends Vehicle {
    int tyre;
    public Car(Vehicle sample, int tyre) {
        super(sample.number, sample.name);
        this.tyre = tyre;
    }
}

SampleResponse class

public class SampleClassResponse {
    private int status;
    private List<Vehicle> vehicles;
    public SampleClassResponse(int status, List<Vehicle> vehicles){
        this.status = status;
        this.vehicles = vehicles;
    }
}

So here in the main class when I am serializing SampleClass response I am getting output as(with ignored Tyre values)

{"vehicles":[{"number":1,"name":"somename"},{"number":1,"name":"somename"}],"status":1}

and when same code is written in java I am getting output as:

{"status":1,"vehicles":[{"tyre":4,"number":1,"name":"somename"},{"tyre":4,"number":1,"name":"somename"}]}

Could anyone help me in figuring out what am i doing wrong in this kotlin code.

Note: I have used intellij converter to convert code to Kotlin

Here is the java code

public class MainObject {
public static void main(String[] args) {
    Sample somename = new Sample(1, "somename");
    Car car = new Car(somename, 4);
    Car car2 = new Car(somename, 4);
    ArrayList vehicles = new ArrayList<Vehicle>() {

    };
    vehicles.add(car);
    vehicles.add(car2);
    SampleClassResponse sampleClassResponse = new 
    SampleClassResponse(1, vehicles);
    Gson gson = new Gson();
    String x = gson.toJson(sampleClassResponse);
    System.out.println(x);

    }

}

Base Class:

public class Vehicle {
int number;
String name;
}

Derived class:

public class Car extends Vehicle {
int tyre;
public Car(Sample sample, int tyre) {
    this.number = sample.number;
    this.name = sample.name;
    this.tyre = tyre;
}
}

SampleResponse class:

public class SampleClassResponse {
int status;
List<Vehicle>  vehicles = new ArrayList<>();
public SampleClassResponse(int status, List<Vehicle> vehicles){

    this.status = status;
    this.vehicles = vehicles;
}
}
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
megha
  • 81
  • 1
  • 4
  • Post the "same code is written in java". – Alexey Romanov Feb 19 '19 at 08:22
  • Have edited the code with java code also – megha Feb 19 '19 at 08:32
  • See this: https://stackoverflow.com/questions/19588020/gson-serialize-a-list-of-polymorphic-objects – Cililing Feb 19 '19 at 09:06
  • Possible duplicate of [Gson serialize a list of polymorphic objects](https://stackoverflow.com/questions/19588020/gson-serialize-a-list-of-polymorphic-objects) – Cililing Feb 19 '19 at 09:07
  • For me with the same library its working in java I don't know how.For deserialization I agree that we need RuntimeTypeAdapterFactory but I don't understand do we really need for serialization. – megha Feb 19 '19 at 09:36
  • I ran your code, and it returns the correct results in Kotlin: `{"status":1,"vehicles":[{"tyre":4,"number":1,"name":"somename"},{"tyre":4,"number":1,"name":"somename"}]}`. As your original examples didn't compile, my guess is the problem is within your code, and not with Gson. – Alexey Soshin Feb 19 '19 at 14:58

1 Answers1

0

your code does not compile and that's why I do not know where the problem is.

my proposition of the code in the kotlin is as follows

fun main(args: Array<String>) {
    val vehicle = Vehicle("somename", 1)
    val car = Car(vehicle, 4)
    val car2 = Car(vehicle, 4)
    val vehicles = listOf(car, car2)

    val response = SampleClassResponse(1, vehicles)
    val json = Gson().toJson(response)
    println(json)
}

class SampleClassResponse(val status: Int, val vehicles: List<Vehicle>)

open class Vehicle(var name: String?, var number: Int = 0)
class Car(sample: Vehicle, var tyre: Int) : Vehicle(sample.name, sample.number)

now is what you would like to get

{"status":1,"vehicles":[{"tyre":4,"name":"somename","number":1},{"tyre":4,"name":"somename","number":1}]}
DEV-Jacol
  • 567
  • 3
  • 10