Search code examples
javajsonlibgdxstack-overflowcircular-reference

Libgdx JsonWriter StackOverflowError


I'm getting a StackOverflowError when trying to write an object to json with com.badlogic.gdx.utils.Json, which is based on jsonbeans. The object and all objects it references only contain primitive variables like float, boolean, int, etc except for references to a ShapeRenderer. Multiple objects all reference the same ShapeRenderer. There are some circular references (objects both having a reference to each other) but i assume it should be able to handle that.

What could be the cause of these errors? Are the circular references the problem? I can't simply remove them without going back to the drawing board and restructuring major parts of my app.

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)

This goes on for about 1024 lines:

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

Not sure whether that's the log limit or the stack limit, i guess the first one.


Solution

  • The circular references are the problem. This is a nice Solution:

    I think the best way to address this is, is to probably remove your circular link by re-architeching your data structure in some way, maybe using some sort of map or table instead to link the Entity to the Item.

    If you really must keep the circular references, then I'd recommend writing your own parser as simply ignoring the owner on serialization will mean the owner is not returned when the serialized objects are deserialized.