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

时间:2018-01-04 22:09:23

标签: java json serialization gson

我有这个Java类:

class Car {
     int mileage;
     int id;
}

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

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

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

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

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

2 个答案:

答案 0 :(得分:2)

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

第一 - 使用自定义序列化

第二 - 使用JsonAdapter注释 - 更简单

使用自定义序列化

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

}

使用@JsonAdapter注释

在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"}

答案 1 :(得分:0)

您可以这样尝试:

new GsonBuilder()
  .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
  .registerTypeAdapter(Integer.class, (JsonSerializer<Integer>)
    (integer, type, jsonSerializationContext) -> new       
      JsonPrimitive(String.valueOf(integer)))
  .excludeFieldsWithoutExposeAnnotation().create();