从Java对象到JSON对象的自定义转换 [英] Custom Conversion from Java Object to JSON Object
问题描述
我有以下代码:
Gson gson = new Gson();
String json = gson.toJson(criteria.list()); // list由Hibernate传递
结果如下所示:
{creationTime:0,enabled:true,id:1,loginDuration:0,online:false,userName:someone}
我想在JSON响应中添加新的属性(DT_RowId,它与id具有相同的值)。最终结果应该如下所示:
{creationTime:0,enabled:true,id:1,loginDuration:0,online :false,userName:someone,DT_RowId = 1}
$ b UPDATED
为了解决这个问题,我在实体上创建了一个包含@Transient注解的字段。
...
@Transient
私人长DT_RowId;
public void setId(long id){
this.id = id;
this.DT_RowId = id;
}
...
然而,setId函数从未被调用过。有人能给我启发吗?
GSON不会调用你的getters和setters。它通过反射直接访问成员变量。要完成您正在尝试的操作,您需要使用GSON自定义串行器/解串器。 GSON在定制串行器/解串器上的文档提供一些例子来说明如何做到这一点。
下面是一个使用JUnit测试的实例,演示了如何实现它:
Entity.java
public class实体{
protected long creationTime;
保护布尔启用;
保护长ID;
保护长loginDuration;
保护布尔线上;
保护字符串userName;
保护长DT_RowId;
$ b EntityJsonSerializer.java
import java.lang.reflect.Type;
import com.google.gson。*;
public class EntityJsonSerializer实现了JsonSerializer< Entity> {
@Override
public JsonElement serialize(Entity entity,Type typeOfSrc,JsonSerializationContext context){
entity.DT_RowId = entity.id;
Gson gson = new Gson();
return gson.toJsonTree(entity);
}
}
JSONTest.java
import static org.junit.Assert。*;
import org.junit.Test;
import com.google.gson。*;
public class JSONTest {
@Test
public final void testSerializeWithDTRowId(){
Entity entity = new Entity();
entity.creationTime = 0;
entity.enabled = true;
entity.id = 1;
entity.loginDuration = 0;
entity.online = false;
entity.userName =someone;
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Entity.class,new EntityJsonSerializer());
Gson gson = builder.create();
String json = gson.toJson(entity);
String expectedJson ={\creationTime \:0,\enabled\:true,\id \:1,\loginDuration \:0,\\ \\ online\:假,\ userName\:\ someone\,\ DT_RowId\:1};
assertEquals(expectedJson,json);
}
}
I have the following code
Gson gson = new Gson();
String json = gson.toJson(criteria.list()); // list is passed by Hibernate
The result would be something like this:
{creationTime:0, enabled:true, id:1, loginDuration:0, online:false, userName:someone}
I would like to add new attribute (DT_RowId which has the same value as id) within the JSON response. The end result should be like this:
{creationTime:0, enabled:true, id:1, loginDuration:0, online:false, userName:someone, DT_RowId=1}
UPDATED
I created a field with @Transient annotation on the entity in order to solve this issue.
...
@Transient
private long DT_RowId;
public void setId(long id) {
this.id = id;
this.DT_RowId=id;
}
...
However the setId function was never been called. Can someone enlighten me on this ?
解决方案 GSON won't call your getters and setters. It accesses member vars directly via reflection. To accomplish what you are trying to do, you will need to use a GSON custom serializer/deserializer. The GSON docs on custom serializers/deserializers provide some examples for how to do this.
Here is a working example with a passing JUnit test that demonstrates how to do it:
Entity.java
public class Entity {
protected long creationTime;
protected boolean enabled;
protected long id;
protected long loginDuration;
protected boolean online;
protected String userName;
protected long DT_RowId;
}
EntityJsonSerializer.java
import java.lang.reflect.Type;
import com.google.gson.*;
public class EntityJsonSerializer implements JsonSerializer<Entity> {
@Override
public JsonElement serialize(Entity entity, Type typeOfSrc, JsonSerializationContext context) {
entity.DT_RowId = entity.id;
Gson gson = new Gson();
return gson.toJsonTree(entity);
}
}
JSONTest.java
import static org.junit.Assert.*;
import org.junit.Test;
import com.google.gson.*;
public class JSONTest {
@Test
public final void testSerializeWithDTRowId() {
Entity entity = new Entity();
entity.creationTime = 0;
entity.enabled = true;
entity.id = 1;
entity.loginDuration = 0;
entity.online = false;
entity.userName = "someone";
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Entity.class, new EntityJsonSerializer());
Gson gson = builder.create();
String json = gson.toJson(entity);
String expectedJson = "{\"creationTime\":0,\"enabled\":true,\"id\":1,\"loginDuration\":0,\"online\":false,\"userName\":\"someone\",\"DT_RowId\":1}";
assertEquals(expectedJson, json);
}
}
这篇关于从Java对象到JSON对象的自定义转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!