2013-01-20 14 views
1

當試圖將對象寫入json com.badlogic.gdx.utils.Json(基於jsonbeans)時,我得到StackOverflowError。除了對ShapeRenderer的引用外,它引用的對象和所有對象只包含像float,boolean,int等基本變量。多個對象都引用相同的ShapeRenderer。有一些循環引用(對象都相互引用),但我認爲它應該能夠處理。Libgdx JsonWriter StackOverflowError

這些錯誤的原因是什麼?循環引用了問題嗎?我不能簡單地刪除它們,而無需返回到繪圖板並重新構建應用程序的主要部分。

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.StackOverflowError 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111) 
Caused by: java.lang.StackOverflowError 
    at java.util.regex.Pattern$CharProperty$1.isSatisfiedBy(Unknown Source) 
    at java.util.regex.Pattern$7.isSatisfiedBy(Unknown Source) 
    at java.util.regex.Pattern$7.isSatisfiedBy(Unknown Source) 
    at java.util.regex.Pattern$7.isSatisfiedBy(Unknown Source) 
    at java.util.regex.Pattern$7.isSatisfiedBy(Unknown Source) 
    at java.util.regex.Pattern$CharProperty.match(Unknown Source) 
    at java.util.regex.Pattern$Curly.match0(Unknown Source) 
    at java.util.regex.Pattern$Curly.match(Unknown Source) 
    at java.util.regex.Pattern$CharProperty.match(Unknown Source) 
    at java.util.regex.Matcher.match(Unknown Source) 
    at java.util.regex.Matcher.matches(Unknown Source) 
    at com.badlogic.gdx.utils.JsonWriter$OutputType.quoteName(JsonWriter.java:174) 
    at com.badlogic.gdx.utils.JsonWriter.name(JsonWriter.java:46) 
    at com.badlogic.gdx.utils.JsonWriter.set(JsonWriter.java:113) 
    at com.badlogic.gdx.utils.Json.writeType(Json.java:574) 
    at com.badlogic.gdx.utils.Json.writeObjectStart(Json.java:533) 
    at com.badlogic.gdx.utils.Json.writeValue(Json.java:491) 
    at com.badlogic.gdx.utils.Json.writeFields(Json.java:237) 
    at com.badlogic.gdx.utils.Json.writeValue(Json.java:492) 
    at com.badlogic.gdx.utils.Json.writeFields(Json.java:237) 
    at com.badlogic.gdx.utils.Json.writeValue(Json.java:492) 
    at com.badlogic.gdx.utils.Json.writeFields(Json.java:237) 
    at com.badlogic.gdx.utils.Json.writeValue(Json.java:492) 
    at com.badlogic.gdx.utils.Json.writeFields(Json.java:237) 

這正好爲約1024行:

at com.badlogic.gdx.utils.Json.writeValue(Json.java:492) 
    at com.badlogic.gdx.utils.Json.writeFields(Json.java:237) 

不知道這是否是日誌限制或堆棧限制,我想第一個。

+0

你可以試着將你想寫入的值修剪成一個小例子嗎?您可能能夠找出具體問題。我懷疑一個循環引用的小例子將揭露這個問題... –

+0

我刪除了所有循環引用和對ShapeRenderer的所有引用。我將shaperenderer傳遞給需要渲染每個幀的對象的渲染函數,以及需要時用於直接從父對象獲取的其他一些變量。現在JSON作家和讀者工作,但我仍然不知道爲什麼它不會很好地循環引用。它不會很難被發現,只需存儲每個唯一實例的一個副本,並通過ID引用其餘的實例... – Hatagashira

回答

1

循環引用是問題。這是一個不錯Solution

我認爲,解決這是最好的辦法,就是可能通過重新architeching你的數據結構以某種方式刪除您 循環鏈接, 也許使用某種圖或表而是將實體鏈接到 該物品。

如果你真的必須保持循環引用,那麼我建議 編寫自己的解析器根本無視系列化 業主將意味着車主在序列化對象是 反序列化沒有回來。

0

在某些情況下,您還可以在序列化之前將這些引用設置爲null,然後在反序列化之後立即恢復它們。例如:

class Parent { 
    Array<Son> sons; 

    public void serialize() { 
     clearParents(); 
     ... serialization code ... 
     restoreParents(); 
    } 

    public static Parent deserialize(data) { 
     Parent parent = ... deserialization code ... 
     parent.restoreParents(); 
     return parent; 
    } 

    public void clearParents() { 
     for (Son son :sons) { 
      son.parent = null; 
     } 
    } 

    public void restoreParents() { 
     for (Son son :sons) { 
      son.parent = this; 
     } 
    } 

} 

class Son { 
    Parent parent; 

    public Son (Parent parent) { 
     this.parent = parent; 
     parent.sons.add(this); 
    } 
}