fra*_*ail 20 java constructor gson deserialization
public class UserAction {
private final UUID uuid;
private String userId;
/* more fields, setters and getters here */
public UserAction(){
this.uuid = UUID.fromString(new com.eaio.uuid.UUID().toString());
}
public UserAction(UUID uuid){
this.uuid = uuid;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserAction other = (UserAction) obj;
if (this.uuid != other.uuid && (this.uuid == null || !this.uuid.equals(other.uuid))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + (this.uuid != null ? this.uuid.hashCode() : 0);
return hash;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Gson对这个类进行serilize和反序列化.就像今天一样,我必须在这个对象中添加一个最终的UUID.我没有问题序列化.我需要public UserAction(UUID uuid)在反序列化时强制gson使用构造函数.我怎样才能做到这一点?
Tom*_*icz 22
您可以实现自定义JsonDeserializer并将其注册到GSON.
class UserActionDeserializer implements JsonDeserializer<UserAction> {
public UserAction deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return new UserAction(UUID.fromString(json.getAsString());
}
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(UserAction.class, new UserActionDeserializer());
Run Code Online (Sandbox Code Playgroud)
请记住,此代码尚未经过测试.
Pro*_*uce 13
解决这个问题的另一种方法是利用这样一个事实:在反序列化过程中,Gson将使用JSON中找到的新值来破坏构造函数设置的任何值,因此只需使用InstanceCreator,它专门存在"创建类的实例"没有定义no-args构造函数." 这种方法效果特别好时要只是用构造函数的参数值分配给字段,并且不执行任何有效性检查或以其他方式进行任何有意义的基于状态的处理.
此外,这种方法不需要进一步的自定义反序列化 - 不需要自定义实现JsonDeserializer.这对于引入自定义解串器以解决一个小问题然后需要"近距离"处理其他JSON元素的情况是有利的,这可能是非平凡的.
话虽如此,这是一个使用首选UserAction构造函数的工作解决方案,但只传递空引用.稍后将设置JSON的实际值.(Gson并不关心这个uuid领域应该是最终的.)
import java.lang.reflect.Type;
import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
public class Foo
{
public static void main(String[] args)
{
UserAction action = new UserAction(UUID.randomUUID());
action.setUserId("user1");
String json = new Gson().toJson(action);
System.out.println(json);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(UserAction.class, new UserActionInstanceCreator());
Gson gson = gsonBuilder.create();
UserAction actionCopy = gson.fromJson(json, UserAction.class);
System.out.println(gson.toJson(actionCopy));
}
}
class UserActionInstanceCreator implements InstanceCreator<UserAction>
{
@Override
public UserAction createInstance(Type type)
{
return new UserAction(null);
}
}
class UserAction
{
private final UUID uuid;
private String userId;
public UserAction()
{
throw new RuntimeException("this constructor is not used");
}
public UserAction(UUID uuid)
{
this.uuid = uuid;
}
void setUserId(String userId)
{
this.userId = userId;
}
}
Run Code Online (Sandbox Code Playgroud)