派生类属性未在Kotlin中序列化

时间:2019-02-19 07:50:52

标签: kotlin gson

我正在尝试在Kotlin中使用Gson序列化派生对象,并且在序列化时,我仅获得父类属性Gson忽略了所有派生类属性。 这是我的代码:

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);
}

//基类:

public class Vehicle {
int number;
String name;
}

子类:

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类

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

因此在主类中,当我序列化SampleClass响应时,我得到的输出为(忽略的Tire值)

{“车辆”:[{“数字”:1,“名称”:“ somename”},{“数字”:1,“名称”:“ somename”}],“状态”:1}

并且当用Java编写相同的代码时,我得到的输出为:

{“状态”:1,“车辆”:[{“轮胎”:4,“编号”:1,“名称”:“某名称”},{“轮胎”:4,“编号”:1, “ name”:“ somename”}]}

任何人都可以帮助我弄清楚我在这段Kotlin代码中做错了什么。

注意:我已经使用了intellij转换器将代码转换为Kotlin

这是Java代码

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);

    }

}

基类:

public class Vehicle {
int number;
String name;
}

派生类:

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类:

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

    this.status = status;
    this.vehicles = vehicles;
}
}

1 个答案:

答案 0 :(得分:0)

您的代码无法编译,这就是为什么我不知道问题出在哪里的原因。

我对Kotlin中代码的主张如下

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)

现在是您想得到的

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