仅在特定字段上使用自定义解串器? [英] Use a custom deserializer only on certain fields?

查看:106
本文介绍了仅在特定字段上使用自定义解串器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gson,是否可以在特定的字段上使用自定义的解串器/序列化器? 用户指南显示了如何为整个类型注册适配器,而不是针对特定领域。我之所以要这样做,是因为我解析了一个自定义日期格式并将其存储在 long 成员字段中(作为Unix时间戳),所以我不想注册所有 Long 字段的类型适配器。



有没有办法做到这一点?

解决方案

我还将日期值存储为我的对象中的 long ,以方便防御性复制。我还希望在序列化对象时仅覆盖日期字段,而不必写出流程中的所有字段。这是我想出的解决方案。不知道这是处理这种情况的最佳方式,但它似乎表现得很好。



DateUtil 类是一个自定义类,用于获取 Date 解析为字符串

  public final class Person {
private final String firstName;
private final String lastName;
私人最后的出生日期;

私人人物(字符串firstName,字符串lastName,日期birthDate){
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate.getTime();
}

public static Person Person getInstance(String firstName,String lastName,Date birthDate){
return new Person(firstName,lastName,birthDate);

$ b $ public String toJson(){
return new GsonBuilder()。registerTypeAdapter(Person.class,new PersonSerializer())。create()。toJson(this);
}

public static class PersonSerializer实现JsonSerializer< Person> {
@Override
public JsonElement serialize(Person person,Type type,JsonSerializationContext context){
JsonElement personJson = new Gson()。toJsonTree(person);
personJson.getAsJsonObject()。add(birthDate,new JsonPrimitive(DateUtil.getFormattedDate(new Date(policy.birthDate),DateFormat.USA_DATE)));
return personJson;





当这个类被序列化时, birthDate 字段以格式化字符串而不是 long 值。


With gson, is it possible to use a custom deserializer / serializer only on certain fields? The user guide shows how to register an adapter for an entire type, not for specific fields. The reason why I want this is because I parse a custom date format and store it in a long member field (as a Unix timestamp), so I don't want to register a type adapter for all Long fields.

Is there a way to do this?

解决方案

I also store Date values as long in my objects for easy defensive copies. I also desired a way to override only the date fields when serializing my object and not having to write out all the fields in the process. This is the solution I came up with. Not sure it is the optimal way to handle this, but it seems to perform just fine.

The DateUtil class is a custom class used here to get a Date parsed as a String.

public final class Person {
  private final String firstName;
  private final String lastName;
  private final long birthDate;

  private Person(String firstName, String lastName, Date birthDate) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDate = birthDate.getTime();
  }

  public static Person getInstance(String firstName, String lastName, Date birthDate) {
    return new Person(firstName, lastName, birthDate);
  }

  public String toJson() {
    return new GsonBuilder().registerTypeAdapter(Person.class, new PersonSerializer()).create().toJson(this);
  }

  public static class PersonSerializer implements JsonSerializer<Person> {
    @Override
    public JsonElement serialize(Person person, Type type, JsonSerializationContext context) {
      JsonElement personJson = new Gson().toJsonTree(person);
      personJson.getAsJsonObject().add("birthDate", new JsonPrimitive(DateUtil.getFormattedDate(new Date(policy.birthDate), DateFormat.USA_DATE)));
      return personJson;
    }
  }
}

When the class is serialized, the birthDate field is returned as a formatted String instead of the long value.

这篇关于仅在特定字段上使用自定义解串器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆