【问题标题】:Gson serialize exclude key from HashmapGson 序列化 Hashmap 中的排除键
【发布时间】:2014-09-17 15:55:25
【问题描述】:

我的课是这样的

public class GiftCard {
    Map<String, Object> extendedProperties;
    String expiryDate;
    double originalAmount;
    String cardNumber;
}

hashmap 有很多值,包括

当我执行log(gson.json(giftCard)) 时,它也会打印密码。

如何防止 pin 被记录(同时在 extendedProperties 中记录其他值)?

【问题讨论】:

    标签: json serialization gson


    【解决方案1】:

    您不想更改原始引脚值,您需要它,对吧?然后你应该使用 toString 方法。修改您的 GiftCard 类。请参阅下面的代码和其中的 cmets。实际上那里的 cmets 比 code 多:

    public class GiftCard implements Cloneable {
        Map<String, Object> extendedProperties;
        String expiryDate;
        double originalAmount;
        String cardNumber;
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            GiftCard cloned = (GiftCard)super.clone();
            // the default behavior of clone() is to return a shallow copy of the object. 
            // This means that the values of all of the original object’s fields are copied to the fields of the new object.
            // If the fields are primitive types, the changes made to this object will not be reflected to cloned ones.
            // But if the fields are not primitive, only their references are cloned, 
            // which means any change on that fields, are applied to original and the cloned objects.
            // So here we have to get a copy of your hash map.
            cloned.extendedProperties = new HashMap<String, Object>(this.extendedProperties);
            return cloned;
        }
    
        @Override
        public String toString() {
            try {
                GiftCard giftCardToPrint = null;
    
                GsonBuilder gsonBuilder = new GsonBuilder();
                Gson gson = gsonBuilder.create();
    
                if(this.extendedProperties != null && this.extendedProperties.get("pin") != null) {
                    // You don't want to change the original pin value so clone original instance, see clone method.
                    giftCardToPrint = (GiftCard) this.clone();  
                    giftCardToPrint.extendedProperties.put("pin", "*");
                } else {
                    giftCardToPrint = this;
                }
    
                return gson.toJson(giftCardToPrint);
            } catch (Exception e) {
                e.printStackTrace();
                return super.toString();
            }
    
        }
    }
    

    试试下面的代码:

    System.out.println("giftCard toString:" + giftCard.toString());
    System.out.println("giftCard toJson  :" + gson.toJson(giftCard));
    System.out.println("giftCard.pin     :" + giftCard.extendedProperties.get("pin"));
    

    输出:

    giftCard toString:{"cardNumber":"1111 2222 3333 4444","extendedProperties":{"pin":"*"},"originalAmount":0.0}
    giftCard toJson  :{"cardNumber":"1111 2222 3333 4444","extendedProperties":{"pin":"1234"},"originalAmount":0.0}
    giftCard.pin     :1234
    

    【讨论】:

      猜你喜欢
      • 2023-01-19
      • 2013-01-18
      • 1970-01-01
      • 2021-05-26
      • 2016-05-10
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      相关资源
      最近更新 更多