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

标签 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 文件?

最佳答案

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

首先 - 使用自定义序列化

第二 - 使用 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"}

关于Java GSON - 将 int 作为字符串序列化为 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104306/

相关文章:

java - Spring Webflow : How to change the destination of a transition in a child flow

java - 更改添加到堆栈的变量的值而不更改堆栈?

jquery - 访问带有哈希 (#) 前缀的 JSON 变量

c# - 连载问题

java - Spring 的 JdbcTemplate 和事务

javascript - 如何从简单的 json 制作所需的 json 树

javascript - 如何将用户推送到对象,PHP 到 JSON

Java+ jackson +XML : serialize a java object properties as XML elements with same names

php - 使用 PHP 序列化 XML 数据以存储在 MySQL 中

java - Quartz 调度程序无法启动 cron 作业