Java GSON - 将int序列化为json文件的字符串

问题描述 投票:1回答:1

我有这个Java类:

class Car {
     int mileage;
     int id;
}

当我告诉gson序列化它时,它当然会将其序列化为:

{
  "mileage": 123,
  "id": 12345678
}

但是,如果我想将其序列化为:

{
  "mileage": "123",
  "id": "12345678"
}

假设我的成员从int更改为String,不是一个选项,有没有办法告诉gson将这些int成员序列化为json文件的字符串?

java json serialization gson
1个回答
2
投票

有很多方法可以实现您的愿望。我将分享两种方式。

首先 - 使用自定义序列化

SECOND - 使用JsonAdapter注释 - 更简单

Using a custom serialization

public static class CarSerializer implements JsonSerializer<Car> {
    public JsonElement serialize(final Car car, final Type type, final JsonSerializationContext context) {
        JsonObject result = new JsonObject();
        result.add("mileage", new JsonPrimitive(Integer.toString(car.getMileage())));
        result.add("id", new JsonPrimitive(Integer.toString(car.getId())));

        return result;
    }
}

要调用它,只需调整代码或使用构造函数使用以下代码

    Car c = new Car(123, 123456789);
    com.google.gson.Gson gson = new 
    GsonBuilder().registerTypeAdapter(Car.class, new CarSerializer())
            .create();
    System.out.println(gson.toJson(c));

输出应该是

{"mileage":"123","id":"12345678"}

示例1的完整代码:

public class SerializationTest {

    public static class Car {
        public int mileage;
        public int id;

        public Car(final int mileage, final int id) {
            this.mileage = mileage;
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setId(final int id) {
            this.id = id;
        }

        public String getMileage() {
            return mileage;
        }    

        public void setMileage(final String mileage) {
            this.mileage = mileage;
        }

    }    

    public static class CarSerializer implements JsonSerializer<Car> {
        public JsonElement serialize(final Car car, final Type type, final JsonSerializationContext context) {
        JsonObject result = new JsonObject();
        result.add("mileage", new JsonPrimitive(Integer.toString(car.getMileage())));
        result.add("id", new JsonPrimitive(Integer.toString(car.getId())));

        return result;
    }
}

public static void main(final String[] args) {
    Car c = new Car(123, 123456789);
    com.google.gson.Gson gson = new 
    GsonBuilder().registerTypeAdapter(Car.class, new CarSerializer())
            .create();
    System.out.println(gson.toJson(c));
}

}

Using a @JsonAdapter annotation

在Car类上使用JsonAdapter Annotation

@JsonAdapter(CarAdapter.class)
public class Car {
    public int mileage;
    public int id;
}

创建自定义适配器

public  class CarAdapter extends TypeAdapter<Car> {

    @Override
    public void write(JsonWriter writer, Car car) throws IOException {
        writer.beginObject();

        writer.name("mileage").value(car.getMileage());
        writer.name("id").value(car.getId());  
        writer.endObject();
    }

    @Override
    public Car read(JsonReader in) throws IOException {
        // do something you need
        return null;
    }

}

要使用此方法进行序列化,请使用类似的方法

Car c = new Car(123, 123456789);
Gson gson = new Gson();    
String result = gson.toJson(c);

在这种情况下打印result应该输出

{"mileage":"123","id":"12345678"}
© www.soinside.com 2019 - 2024. All rights reserved.