2016-08-24 6 views
0

現在私はGSONで遊んでいて、自分で解決できない問題が発生しました。GSONはJSONに表示されないクラス属性を無視する必要があります

私は、これらの3つのクラス持っている: 1つの抽象クラスCustomEntityを

public abstract class CustomEntity { 
    private View customView; 

    public CustomEntity() {} 

    public void setCustomView(View customView) { 
     this.customView = customView; 
    } 

    public View getCustomView() { 
     return customView; 
    } 
} 

CustomEntity

public class LastChange extends CustomEntity { 

    public Config config; 

    public LastChange() {} 

    @Override 
    public String toString() { 
     return "LastChange:" + config.toString(); 
    } 

    public Config getConfig() { 
     return config; 
    } 

    public void setConfig(Config config) { 
     this.config = config; 
    } 
} 

そして第三のクラス

public class Config extends CustomEntity { 

    public String config; 
    public String nav_items; 

    public Config() {} 

    @Override 
    public String toString() { 
     return "config data:" + config + ", " + nav_items; 
    } 

    public String getConfig() { 
     return config; 
    } 

    public void setConfig(String config) { 
     this.config = config; 
    } 

    public String getNav_items() { 
     return nav_items; 
    } 

    public void setNav_items(String nav_items) { 
     this.nav_items = nav_items; 
    } 
} 

コンフィグから延びている別のクラスのLastChange MainActivityでは、私はフォロイをデシリアライズしようとしましたGSONを使用してJSONをLastChangeオブジェクトに変換します。

String lastChangeJson = "{\"config\":{\"config\":\"2016-07-20 15:32:14\",\"nav_items\":\"2016-08-24 12:36:06\"},\"background_images\":{\"background_url_landscape\":\"2015-07-28 17:21:56\",\"background_url\":\"2015-07-28 17:21:56\",\"icon_for_accessory_view\":\"2015-07-28 17:21:56\",\"icon_for_route_view\":\"2015-07-28 17:21:56\",\"background_url_landscape_big\":\"2015-07-28 17:21:56\",\"background_url_big\":\"2015-07-28 17:21:56\"},\"nav_content\":[{\"last_change\":\"2016-06-29 11:06:16\",\"pageId\":\"10262\"},{\"last_change\":\"2016-08-24 12:36:06\",\"pageId\":\"10264\"},{\"last_change\":\"2016-08-09 16:13:03\",\"pageId\":\"10378\"},{\"last_change\":\"2016-08-09 16:13:03\",\"pageId\":\"10263\"},{\"last_change\":\"2016-07-20 15:32:14\",\"pageId\":\"10265\"}]}"; 

CustomEntity lastChangeEntity = gson.fromJson(lastChangeJson, LastChange.class); 

上記のコードは私に次の例外与える:

java.lang.SecurityException: Can't make method constructor accessible 
at java.lang.reflect.Constructor.setAccessible(Constructor.java:336) 
at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:101) 
at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:83) 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:99) 
at com.google.gson.Gson.getAdapter(Gson.java:423)... 

をしかし、私はクラスCustomEntityとそのゲッターとセッターからの属性「のCustomView」を削除した場合、直列化復元が正常に動作します。

誰かが私のjsonに表示されない場合、クラス属性を無視するようGSONにどのように伝えることができますか?

ありがとうございます。

答えて

1

新しいgsonインスタンスの構築:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 

注釈を使用すると、@Exposeにシリアライズするすべてのフィールド:

@Expose 
public Config config; 

編集: は一つの小さなサイドノート - ビューへの参照を維持しないようにしてください(またはコンテキスト関連のオブジェクト)をモデルに追加します。このモデルのインスタンスを静的な方法で保持し、プレゼンテーションとデータレイヤを混在させるようなにおいがする場合は、コードの可読性と保守性のために決してうまくいかない場合、メモリリークが発生する可能性があります。

+0

よろしくお願いいたします。それは働いている。 ;-) – jennymo

関連する問題