Cannot serialize List<string>

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

ERR0
Posts: 4
Joined: 28 Jun 2012, 04:17

Cannot serialize List<string>

Postby ERR0 » 09 Jul 2012, 18:18

Hi I'm trying to send a List<string> to the SFS from my client API and I get this error:
[SFS DEBUG] Cannot serialize field of object: com.insightng.platform.model.Group, field: nodeIds, type: MonoField -- unsupported type!


The list is part of an object called Group and I'm putting the group into an SFSObject and sending it to the server with an ExtensionRequest.
All the other variables in the object serialize just fine.

Here is the object I'm trying to send:

Code: Select all

public class Group : SerializableSFSType
   {
      private string id;
   
      private string label;
   
      private List<string> nodeIds;
   
      private string color;
   
      public string getLabel() {
         return label;
      }
   
      public void setLabel(string label) {
         this.label = label;
      }
   
      public List<string> getNodeIds() {
         return nodeIds;
      }
   
      public void setNodeIds(List<string> nodeIds) {
         this.nodeIds = nodeIds;
      }
   
      public string getColor() {
         return color;
      }
   
      public void setColor(string color) {
         this.color = color;
      }
   
      public string getID() {
         return id;
      }
   
      public void setID(string id) {
         this.id = id;
      }
   
   }


The object on the server is identical to this one.

I call this function to send the Group to the server:

Code: Select all

   // Send the passed group to the server
   public void SendGroup(ContextGroup newGrp)
   {
      Group grp = new Group();
      grp.setLabel(newGrp.name);
      
      Debug.LogWarning("Group name: " + grp.getLabel());
      Debug.LogWarning(newGrp.knowledgeUnits.Count + " nodes in the remote group");
      
      // If there are knowledge units in the ContextGroup, add them to the remote Group
      if (newGrp.knowledgeUnits.Count != 0)
      {
         List<string> nodeIDs = new List<string>();
         
         for(int i=0; i < newGrp.knowledgeUnits.Count; i++)
         {
            nodeIDs.Add(currentCanvas.getNodes()[newGrp.knowledgeUnits[i].canvasID].getID());
            Debug.LogWarning("Node ID " + i + ": " + currentCanvas.getNodes()[newGrp.knowledgeUnits[i].canvasID].getID());
         }
         
         Debug.LogWarning(nodeIDs.Count + " nodes in the remote group");
         
         grp.setNodeIds((List<string>)nodeIDs);
      }
      
      Debug.LogError(newGrp.color);
      
      // Convert the color of the group to a string
      if (newGrp.color == Color.cyan)
         grp.setColor("cyan");
      else if (newGrp.color == Color.blue)
         grp.setColor("blue");
      else if (newGrp.color == Color.green)
         grp.setColor("green");
      else if (newGrp.color == Color.yellow)
         grp.setColor("yellow");
      else if (newGrp.color == Color.magenta)
         grp.setColor("magenta");
      else if (newGrp.color == Color.red)
         grp.setColor("red");
      else if (newGrp.color == Color.white)
         grp.setColor("white");
      else if (newGrp.color == Color.black)
         grp.setColor("black");
      else if (newGrp.color == Color.gray)
         grp.setColor("gray");

      SFSObject sfso = new SFSObject();
      sfso.PutClass("group", grp);
            
      Debug.LogWarning("Sending group ...");
      sfs.Send(new ExtensionRequest("group.add", sfso));
      
      groupQueue.Enqueue(grp);
   }


Any help or insight on this would be much appreciated.
Thanks.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Re: Cannot serialize List<string>

Postby ThomasLund » 19 Jul 2012, 07:53

Yeah - generics are not supported. So you should try to send a string[] instead.

/T
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Cannot serialize List<string>

Postby Lapo » 19 Jul 2012, 07:58

A little extra note from me...
Generics are not supported because the type definition is lost at runtime (at least in Java).
Also typed arrays are not recommended, because they pose similar issues (type conversion over serialization)

You can either use a "wildcard" list (List<?> in Java) or user SFSObjects which allows to define types and are more network efficient
Lapo
--
gotoAndPlay()
...addicted to flash games
ERR0
Posts: 4
Joined: 28 Jun 2012, 04:17

Re: Cannot serialize List<string>

Postby ERR0 » 20 Jul 2012, 14:47

Thanks Lapo!
I'll send it as a string[] and attach it to my object using PutUtfStringArray.
I tested it and it didn't give me a serialization error.
User avatar
jeen
Posts: 19
Joined: 05 Apr 2012, 23:03

Re: Cannot serialize List<string>

Postby jeen » 22 Jul 2012, 23:54

Unfortunately, this doesn't help, because we are using class serialization as described here.

We could probably switch to using SFSArray directly instead, but this requires a lot of rewriting on the server side of things, because it does not seem possible to use SFSObject/SFSArray as property datatypes inside a pojo (at least, I get runtimeexceptions at the serverside when I try this).

I just need some datatype that I can use in my Java POJO to represent a collection of strings, and which can be (de)serialized on the C# end as well.

What would be the combination of datatypes to get a working mapping? For example, if I made the property type List<?> in Java as you suggest, what should the equivalent type in C# be?
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Cannot serialize List<string>

Postby Lapo » 23 Jul 2012, 08:23

A non typed List in Java (or List<?>) correspond to the equivalent in C#. The only problem is that you will have to cast the objects from that list to the type you need (String in this case).

The pojo serialization has its limits due to the fact that we need to find a common denominator across multiple client languages. In particular generic types in Java are lost at runtime, due to type erasure. So in actuality the compiled code uses an untyped List and items are type-casted on each call to the get() method.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
jeen
Posts: 19
Joined: 05 Apr 2012, 23:03

Re: Cannot serialize List<string>

Postby jeen » 23 Jul 2012, 20:13

Lapo wrote:A non typed List in Java (or List<?>) correspond to the equivalent in C#.


The problem is that we don't know what the equivalent is :)
C# does not have a non-typed List class, and the wildcard approach does not work in C# either due to the stricter nature of its generics mechanism.

The only problem is that you will have to cast the objects from that list to the type you need (String in this case).

The pojo serialization has its limits due to the fact that we need to find a common denominator across multiple client languages. In particular generic types in Java are lost at runtime, due to type erasure. So in actuality the compiled code uses an untyped List and items are type-casted on each call to the get() method.


Fair enough, I appreciate the issues in making multiple languages talk to each other. I have already started on a workaround for our problem where we just use SFSObject/SFSArray instead of class serialization. Nevertheless out of curiosity I'd still be keen to learn if there is some way to send a list/collection/array as part of a pojo to a C# client, and what the equivalent datatype(s) should be to make this work...
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Cannot serialize List<string>

Postby Lapo » 23 Jul 2012, 20:41

Sure, I am not the expert here :) I will ask Thomas Lund to give you the details.
As far as I recall collections like the ArrayList didn't require generic types, right? I presume it's something along those lines. But let Thomas reveal the mystery :)

Cheers.
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 69 guests