Serialization on Class

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Sanity1993
Posts: 26
Joined: 30 Jan 2018, 21:14

Serialization on Class

Postby Sanity1993 » 05 Apr 2018, 10:24

Hello,
Been a few months since i got stuck. The scenario i am in. A client want X ammount of a class. So basically 5 instances of a dog. Dog has sets of properties, hashmaps etc. How would i send that back efficiently?

The main class is:

Code: Select all

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package altarianonline.model.Pets;

import altarianonline.model.Buff1.Buffs;
import altarianonline.model.Character.CharacterProperty;
import altarianonline.model.Interfaces.Item;
import altarianonline.model.Interfaces.Pet;
import altarianonline.model.Interfaces.Skill;
import altarianonline.model.Interfaces.Skin;
import com.smartfoxserver.v2.protocol.serialization.SerializableSFSType;
import java.util.Map;

public class RpgPet implements SerializableSFSType
{
   private static final String TAB = "\t";
   private static final String NEW_LINE = "\n";
   
   private String name;
   private String type;
   private int skin;
        private Double x;
        private Double y;
        private Double z;
        private Double rot;
       
        private Map<String, PetProperty> MainCompose;
        private Map<String, PetProperty> SubCompose;
        private Map<String, PetProperty> IniCompose;
       
   private Map<String, Skill> skills;
        private Map<String, Buffs> buff;
   
       
   public RpgPet()
    {
       // TODO Auto-generated constructor stub
    }
   
   public RpgPet(String name, String type, int skin, Double x, Double y, Double z, Double rot)
    {
       this.name = name;
       this.type = type;
       this.skin = skin;
            this.x = x;
            this.y = y;
            this.z = z;
            this.rot = rot;
           
           
    }
        public Double getX()
    {
        return x;
    }
        public void setX(Double x)
    {
        this.x = x;
    }
        public Double getY()
    {
        return y;
    }
        public void setY(Double y)
    {
        this.y = y;
    }
        public Double getZ()
    {
        return z;
    }
        public void setZ(Double z)
    {
        this.z = z;
    }
        public Double getRot()
    {
        return rot;
    }
        public void setRot(Double rot)
    {
        this.rot = rot;
    }
   public String getName()
    {
       return name;
    }
   public void setName(String name)
    {
       this.name = name;
    }
   public String getType()
    {
       return type;
    }
   public void setType(String type)
    {
       this.type = type;
    }
   public int getSkin()
    {
       return skin;
    }
   public void setSkin(int skin)
    {
       this.skin = skin;
    }
        public Map<String, Buffs> getBuff()
    {
       return buff;
    }
   public void setBuff(Map<String, Buffs> buff)
    {
       this.buff = buff;
    }
        public Map<String, PetProperty> getMainCompose()
    {
       return MainCompose;
    }
   public void setMainCompose(Map<String, PetProperty> maincompose)
    {
       this.MainCompose = maincompose;
    }
        public Map<String, PetProperty> getSubCompose()
    {
       return SubCompose;
    }
   public void getSubCompose(Map<String, PetProperty> subcompose)
    {
       this.SubCompose = subcompose;
    }
        public Map<String, PetProperty> getIniCompose()
    {
       return IniCompose;
    }
   public void getIniCompose(Map<String, PetProperty> inicompose)
    {
       this.IniCompose = inicompose;
    }
   public Map<String, Skill> getSkills()
    {
       return skills;
    }
   public void setSkills(Map<String, Skill> skills)
    {
       this.skills = skills;
    }
   
   @Override
   public String toString()
   {
      StringBuilder sb = new StringBuilder();
      sb.append(NEW_LINE);
      sb.append("+------------------------------------+").append(NEW_LINE);
      sb.append(" Pet: ").append(name).append(NEW_LINE);
                sb.append(TAB).append("Type: ").append(type).append(NEW_LINE);
                sb.append(TAB).append("Location: ").append(x).append(" , ").append(y).append(" , ").append(z).append(" Rotation: ").append(rot).append(NEW_LINE);
      sb.append("+------------------------------------+").append(NEW_LINE);

      sb.append(TAB).append("Spells: ").append(NEW_LINE);
      if (skills != null)
         for (Map.Entry<String, Skill> entry : skills.entrySet())
            sb.append(TAB).append(TAB).append(entry.getValue().getId()).append(" -- Modifies: ").append(entry.getValue().getModifies()).append(", Level: ").append(entry.getValue().getLevel()).append(NEW_LINE);
               
      sb.append(TAB).append("Main Compose: ").append(NEW_LINE);

      if (MainCompose != null)
         for (Map.Entry<String, PetProperty> entry : MainCompose.entrySet())
            sb.append(TAB).append(TAB).append(entry.getKey()).append(" -- ").append(entry.getValue()).append(NEW_LINE);
               
                sb.append(TAB).append("Sub Compose: ").append(NEW_LINE);

      if (SubCompose != null)
         for (Map.Entry<String, PetProperty> entry : SubCompose.entrySet())
            sb.append(TAB).append(TAB).append(entry.getKey()).append(" -- ").append(entry.getValue()).append(NEW_LINE);
               
               
                sb.append(TAB).append("Buffs: ").append(NEW_LINE);
               
                if (buff != null)
         for (Map.Entry<String, Buffs> entry : buff.entrySet())
            sb.append(TAB).append(TAB).append(entry.getKey()).append(" Name:  ").append(entry.getValue().getName()).append(" Type:").append(entry.getValue().getType()).append(" Duration:").append(entry.getValue().getDuration()).append(" Level:").append(entry.getValue().getLevel()).append(" Owner:").append(entry.getValue().getOwner()).append(" Percent:").append(entry.getValue().getPct()).append(" Id:").append(entry.getValue().getId()).append(NEW_LINE);
               
       return sb.toString();
   }
}


Which is built up from the Pet Properties.

Code: Select all

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package altarianonline.model.Pets;

import com.smartfoxserver.v2.protocol.serialization.SerializableSFSType;

public class PetProperty implements SerializableSFSType
{
   private int value;
   private String id;
   
   public PetProperty()
    {
       // TODO Auto-generated constructor stub
    }
   
   public PetProperty(int value, String id)
    {
       this.value = value;
       this.id = id;
    }
   
   public int getValue()
    {
       return value;
    }
   public void setValue(int value)
    {
       this.value = value;
    }
   public String getId()
    {
       return id;
    }
   public void setId(String id)
    {
       this.id = id;
    }
   
   @Override
   public String toString()
   {
      return "value: " + value;
   }
}


At which im trying to use a server handler which is this code:

Code: Select all

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package altarianonline.model.Pets;

import com.smartfoxserver.v2.protocol.serialization.SerializableSFSType;
public class PetProperty implements SerializableSFSType
{
   private int value;
   private String id;
   
   public PetProperty()
    {
       // TODO Auto-generated constructor stub
    }
   
   public PetProperty(int value, String id)
    {
       this.value = value;
       this.id = id;
    }
   
   public int getValue()
    {
       return value;
    }
   public void setValue(int value)
    {
       this.value = value;
    }
   public String getId()
    {
       return id;
    }
   public void setId(String id)
    {
       this.id = id;
    }
   
   @Override
   public String toString()
   {
      return "value: " + value;
   }
}


However is generates
11:21:27,610 INFO [SFSWorker:Ext:2] Extensions - {altExt}: finding pets
11:21:27,615 WARN [SFSWorker:Ext:2] protocol.SFSIoHandler - com.smartfoxserver.v2.exceptions.SFSRuntimeException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: com.smartfoxserver.v2.exceptions.SFSRuntimeException
Message: java.lang.IllegalStateException: Cannot serialize object: {}, type: java.util.HashMap -- It doesn't implement the SerializableSFSType interface
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.pojo2sfs(DefaultSFSDataSerializer.java:1461)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:852)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.arr2bin(DefaultSFSDataSerializer.java:563)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.array2binary(DefaultSFSDataSerializer.java:549)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:843)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:519)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:502)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:848)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:519)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:502)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:848)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:519)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:502)
com.smartfoxserver.v2.entities.data.SFSObject.toBinary(SFSObject.java:234)
com.smartfoxserver.v2.protocol.binary.BinaryIoHandler.handleWrite(BinaryIoHandler.java:84)
com.smartfoxserver.v2.protocol.SFSIoHandler.onDataWrite(SFSIoHandler.java:223)
com.smartfoxserver.v2.protocol.SFSProtocolCodec.onPacketWrite(SFSProtocolCodec.java:165)
com.smartfoxserver.bitswarm.core.BitSwarmEngine.writeToSocket(BitSwarmEngine.java:412)
com.smartfoxserver.bitswarm.core.BitSwarmEngine.write(BitSwarmEngine.java:406)
com.smartfoxserver.bitswarm.io.Response.write(Response.java:71)
com.smartfoxserver.v2.api.response.SFSResponseApi.sendExtResponse(SFSResponseApi.java:91)
com.smartfoxserver.v2.api.SFSApi.sendExtensionResponse(SFSApi.java:1515)
com.smartfoxserver.v2.extensions.BaseSFSExtension.send(BaseSFSExtension.java:487)
com.smartfoxserver.v2.extensions.BaseSFSExtension.send(BaseSFSExtension.java:462)
com.smartfoxserver.v2.extensions.BaseClientRequestHandler.send(BaseClientRequestHandler.java:56)
altarianonline.GetPets.handleClientRequest(GetPets.java:132)
com.smartfoxserver.v2.extensions.SFSExtension.handleClientRequest(SFSExtension.java:208)
com.smartfoxserver.v2.controllers.v290.ExtensionReqController.processRequest(ExtensionReqController.java:174)
com.smartfoxserver.v2.controllers.v290.ExtensionReqController$1.run(ExtensionReqController.java:68)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


Which im assuming its because i am sending back a Hashmap not directly the class as i need to send multiple instances...soo any ideas ? Appreciated as always too!
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Serialization on Class

Postby Lapo » 05 Apr 2018, 15:11

Hi,
yes the top class must be a SFSSerializableType.
If you pass a Map<K,V> as the top object it will cause such error.

Wrapping your the hash map in a SFSSerializableType object will do the trick.
Lapo
--
gotoAndPlay()
...addicted to flash games
Sanity1993
Posts: 26
Joined: 30 Jan 2018, 21:14

Re: Serialization on Class

Postby Sanity1993 » 05 Apr 2018, 15:15

Hello Lapo,
Thank you for clarifying that. Are you aware of another way to send back multiple serializable classes from 1 call ?
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Serialization on Class

Postby Lapo » 05 Apr 2018, 15:22

Wrapping them in one serializable object :)
Lapo

--

gotoAndPlay()

...addicted to flash games
nik0990
Posts: 76
Joined: 23 Dec 2016, 10:48
Location: India

Re: Serialization on Class

Postby nik0990 » 18 Feb 2019, 04:41

Wrapping your the hash map in a SFSSerializableType object will do the trick

How?
nik0990
Posts: 76
Joined: 23 Dec 2016, 10:48
Location: India

Re: Serialization on Class

Postby nik0990 » 18 Feb 2019, 06:08

I wrap My code but still having this exception while converting to JSON

Code: Select all

public class A implements SerializableSFSType{
public B b = new B();
//getter and setters
}



Exception: com.smartfoxserver.v2.exceptions.SFSRuntimeException
Message: java.lang.IllegalStateException: Cannot serialize object: {0=mtt.EntryChipAndStatus@1507737c}, type: java.util.HashMap -- It doesn't implement the SerializableSFSType interface
Description: Error during websocket packet write
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.pojo2sfs(DefaultSFSDataSerializer.java:1461)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:852)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:519)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:502)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:848)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:519)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:502)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.encodeObject(DefaultSFSDataSerializer.java:848)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.obj2bin(DefaultSFSDataSerializer.java:519)
com.smartfoxserver.v2.protocol.serialization.DefaultSFSDataSerializer.object2binary(DefaultSFSDataSerializer.java:502)
com.smartfoxserver.v2.entities.data.SFSObject.toBinary(SFSObject.java:234)
com.smartfoxserver.bitswarm.websocket.jetty.WebSocketBinaryProtocolCodec.onPacketWrite(WebSocketBinaryProtocolCodec.java:99)
com.smartfoxserver.bitswarm.core.BitSwarmEngine.writeToWebSocket(BitSwarmEngine.java:417)
com.smartfoxserver.bitswarm.core.BitSwarmEngine.write(BitSwarmEngine.java:398)
com.smartfoxserver.bitswarm.io.Response.write(Response.java:71)
com.smartfoxserver.v2.api.response.SFSResponseApi.sendExtResponse(SFSResponseApi.java:91)
com.smartfoxserver.v2.api.SFSApi.sendExtensionResponse(SFSApi.java:1531)
com.smartfoxserver.v2.extensions.BaseSFSExtension.send(BaseSFSExtension.java:487)
com.smartfoxserver.v2.extensions.BaseSFSExtension.send(BaseSFSExtension.java:462)
extensions.games.main.GameExtension.sendCommand(GameExtension.java:1377)
extensions.games.main.PokerExtension.sendReEntryData(PokerExtension.java:1351)
poker.GameController.processReentryPopUp(GameController.java:16579)
poker.GameController.processRoundEnd(GameController.java:7392)
poker.GameController.runStateMachine(GameController.java:6484)
extensions.games.main.GameExtension$TaskRunner.run(GameExtension.java:2087)
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


Could you suggest something?
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Serialization on Class

Postby Lapo » 19 Feb 2019, 16:35

Hi,
it looks like you are doing the same thing you were doing earlier, when you posted the first issue. In other words you're sending an HashMap via putClass which is not possible because it doesn't implement the SerializableSFSType.

If this is not the case please show me the code you're using for sending this message.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 65 guests