How to write serializer for Array<>?

908 views Asked by At

I have no idea how to approach this. I need to write a serializer for an Array<> object found in libgdx.

public static void initialize(Server server) {
    Kryo kryo = server.getKryo();
    kryo.register(ServerData.class);
    kryo.register(PlayerNick.class);
    kryo.register(MoveLeft.class);
    kryo.register(MoveRight.class);
    kryo.register(MoveUp.class);
    kryo.register(MoveDown.class);
    kryo.register(StandUp.class);
    kryo.register(ButtonLeft.class);
    kryo.register(ButtonRight.class);  
    //
    kryo.register(PlayerData.class);  
    kryo.register(Vector2.class);
    kryo.register(Array.class);  
    kryo.register(PlayerDataPacket.class);  
}
public static void initialize(Client client) {
    Kryo kryo = client.getKryo();
    kryo.register(ServerData.class);
    kryo.register(PlayerNick.class);
    kryo.register(MoveLeft.class);
    kryo.register(MoveRight.class);
    kryo.register(MoveUp.class);
    kryo.register(MoveDown.class);
    kryo.register(StandUp.class);
    kryo.register(ButtonLeft.class);
    kryo.register(ButtonRight.class); 
    //
    kryo.register(PlayerData.class);  
    kryo.register(Vector2.class);
    kryo.register(Array.class);  
    kryo.register(PlayerDataPacket.class);  
}
public static class PlayerData {
    public String name;
    public int id;
    public Vector2 position;
}

public static class PlayerDataPacket {
    public Array<PlayerData> playerList = new Array<PlayerData>();
}

That's how my usual serialization is done. Here, I need to write a custom serializer for PlayerDataPacket.

I have found this: https://groups.google.com/forum/#!topic/kryonet-users/9z4Ix2JO7JA but it seems to be outdated since it can't find a few methods, plus it doesn't say how to do it with Array<>.

1

There are 1 answers

0
EpicPandaForce On

I wrote an example for sending a float array over the network here using KryoNet:

https://stackoverflow.com/a/24339801/2413303

 Server server = new Server();
  Kryo kryo = server.getKryo();
  kryo.register(float[].class);
  server.start();
  server.bind(2300, 2301);
  server.addListener(new Listener() {
   public void received(Connection connection, Object object)
   {
      if(object instanceof float[])
      {
        float[] array = (float[])object;
        for(int i = 0; i < array.length; i++)
        {
           System.out.println("" + array[i]);
        }
      }        
   }});
  Client client = new Client();
  Kryo kryo = client.getKryo();
  kryo.register(float[].class);
  client.addListener(new Listener() {
    public void connected(Connection connection)
    {
       connection.sendTCP(new float[] {5, 6, 7, 8});
    }
  };
  client.connect(5000, "127.0.0.1”, 2300, 2301);