I have class GamePresenter with collection of Players:
public class GamePresenter extends MvpBasePresenter<IMainGameView> {
public ArrayList<BoxPlayer> BoxPlayersColl;
public GamePresenter()
{
TestPlayerInit testPlayerInit=new TestPlayerInit();
BoxPlayersColl=testPlayerInit.BoxPlayersColl;
}
@Override
public String toString() {
return "GamePresenter [data1=" + BoxPlayersColl + "]";
}
}
TestPlayerInit
simple create all Players, insert is into BoxPlayerCollection.
So, each BoxPlayer have some fields and Map of another objects:
public class BoxPlayer {
public Player PlayerObj;
public Integer Id;
public String Description;
// getters,setters...
public Map<Integer,PlayerData> IntToPlayerData;
}
Player
class:
public int Id;
public String NickName;
// getters setters
public Player(int id,String nickName)
{
Id=id;
NickName=nickName;
}
And PlayerData is simple POJO:
public class PlayerData{
public BoxPlayer Player1;
public BoxPlayer Player2;
public VotedBoxPlayer(BoxPlayer player1, BoxPlayer player2)
{
Player1=player1;
Player2=player2;
}
}
So, i want to serialize it and store in SharedPrederence.
I use Gson
lib:
public String serializeGamePresenter(GamePresenter gamePresenter)
{
Gson gson=new Gson();
String gamePresenterJSON= gson.toJson(_gamePresenter);
return gamePresenterJSON;
}
But, when i use this method- application freeze, memory allocation crash application.
Can you help me, how to serialize this object to JSON?
PS when i try to serialize ArrayList
BoxPlayerColl- it works.
You are not showing all of your information, like what is TestPlayerInit? Regardless, you have created an issue with the fact that you are:
The class BoxPlayer
has references to PlayerData
in the public Map<Integer,PlayerData> IntToPlayerData;
Player Data has variables of type BoxPlayer
In Summary, when the json serialization is occurring, you are getting into a cyclic reference loop, an endless loop, causing the JVM to crash.
You need to evaluate the structure of your objects. There should not be a case where class A references B that references A again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.