Java GSON-将int作为字符串序列化为json文件 [英] Java GSON - serialize int as strings to json file
问题描述
我有这个Java类:
class Car {
int mileage;
int id;
}
当我告诉gson对其进行序列化时,它当然会将其序列化为:
When I tell gson to serialize it, it of course serializes it to:
{
"mileage": 123,
"id": 12345678
}
但是如果我要将其序列化为:
But what if I want to serialize it to:
{
"mileage": "123",
"id": "12345678"
}
假设无法将我的成员从int更改为String,是否有办法告诉gson将这些int成员作为字符串序列化到json文件?
Assuming changing my members from int to String, is not an option, is there a way to tell gson to serialize those int members as strings to the json file?
推荐答案
可能有很多方法可以实现您想要的目标. 我将分享两种方式.
There are likely many ways to achieve what you desire. I will share two ways.
第一-使用自定义序列化
秒-使用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;
}
}
要调用此代码,只需修改您的代码或将以下代码与构造函数一起使用
To call this, simply adapt your code or use the following code with a constructor
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));
}
}
在Car类上使用JsonAdapter注释
Use the JsonAdapter Annotation on the Car class
@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;
}
}
要使用此方法进行序列化,请使用类似的
To serialize, using this method, use something like this
Car c = new Car(123, 123456789);
Gson gson = new Gson();
String result = gson.toJson(c);
在这种情况下,应打印result
Printing result
in this case should output
{"mileage":"123","id":"12345678"}
这篇关于Java GSON-将int作为字符串序列化为json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!