0

我使用的是Apache Collection的BidiMap,它提供了DualHashBidiMap類。我必須爲這個項目使用這個類。如何使用GSON反序列化自定義地圖

使用它序列化沒有問題。但是我有反序列化問題!

下面是一個示例類:

package com.description; 

import org.apache.commons.collections4.BidiMap; 
import org.apache.commons.collections4.bidimap.DualHashBidiMap; 

public class Sample { 

    private String id; 
    private String adress; 

    BidiMap<Integer, String> items = new DualHashBidiMap<Integer, String>(); 


    public Sample() { 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getAdress() { 
     return adress; 
    } 

    public void setAdress(String adress) { 
     this.adress = adress; 
    } 

    public BidiMap<Integer, String> getItems() { 
     return items; 
    } 

    public void setItems(BidiMap<Integer, String> items) { 
     this.items = items; 
    } 
} 

及主方法

Sample sample = new Sample(); 
     sample.setId("12312xoa01"); 
     sample.setAdress("Houston, 43.1"); 
     BidiMap<Integer, String> items = new DualHashBidiMap<Integer, String>(); 
     items.put(1, "gloves"); 
     items.put(90, "boots"); 
     sample.setItems(items); 

     try { 
      String result = gson.toJson(sample); 
      System.out.println("result : "+result); 
      Sample sample2 = gson.fromJson(result, Sample.class); 
      System.out.println("address : "+sample2.getAdress()); 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 

**


**

result : {"id":"12312xoa01","adress":"Houston, 43.1","items":{"1":"gloves","90":"boots"}} 
java.lang.IllegalArgumentException: Can not set org.apache.commons.collections4.BidiMap field com.description.Sample.items to java.util.LinkedHashMap 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) 
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) 
    at java.lang.reflect.Field.set(Field.java:741) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:118) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216) 
    at com.google.gson.Gson.fromJson(Gson.java:879) 
    at com.google.gson.Gson.fromJson(Gson.java:844) 
    at com.google.gson.Gson.fromJson(Gson.java:793) 
    at com.google.gson.Gson.fromJson(Gson.java:765) 
    at Main.main(Main.java:79) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

回答

0

這不是那麼簡單,但可以做到:

public class BidiMapTypeAdapterFactory implements TypeAdapterFactory { 

    @Override 
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { 
     if (!BidiMap.class.isAssignableFrom(type.getRawType())) { 
      return null; 
     } 
     final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); 
     return new TypeAdapter<T>() { 
      @Override 
      public void write(JsonWriter out, T value) throws IOException { 
       delegate.write(out, value); 
      } 

      @Override 
      public T read(JsonReader in) throws IOException { 
       return (T) new DualHashBidiMap<>((Map) delegate.read(in)); 
      } 
     }; 
    } 
} 

註冊TypeAdapterFactory:

GsonBuilder b = new GsonBuilder().registerTypeAdapterFactory(new BidiMapTypeAdapterFactory()); 
Gson gson = b.create(); 

現在可以運行你的樣品,它應該工作。

0

爲什麼你需要使用BidiMap?

檢查這些:

deserializing generics with gson

How to deserialize a ConcurrentMap with Gson

嘗試使用地圖或LinkedHashMap或地圖手動解析到BidiMap後輸入,以檢查問題是否解決。編寫TypeAdapter也是一個選項。

順便說一句:您的JSON是不是一個Map<Integer,String>Map<String,String>地圖{"1":"gloves","90":"boots"}

我以前試過GSON,並將其與Java本機類型的作品很好。我從來沒有嘗試解析其他庫,因爲我認爲你需要一個TypeAdapter。