1

I want to send some part of class to server.

sending Info

[Serializable]
public class SendingInfos
{
    public string tempID;
    public string playerLettersInHand;
    public string playerLettersMiddle;
    public int playerBet;
    public string playerLetterSent;

}

currently temID and playerBet has value while rest of them null.In this case, if i use

        string jsondata = JsonUtility.ToJson(sendingInfos);

this and send it. Do i send the part of class which isnt null or all of it? Is there any other option to send part of it? like

        string jsondata = JsonUtility.ToJson({ sendingInfos.tempID, sendingInfos.playerBet  });

Edit: the reason i want to send somepart of class to server is,to keep server network traffic at low as much as possible. Also, i might do this by dividing the class to 2 class.however if there is , easier way , i want to do that.

Jsennin
  • 153
  • 2
  • 12
  • You can create another class like `SendingInfosEntity` with only part of the properties of the original class. In this case, you have to copy all the properties of the `SendingInfosEntity` class back & forth manually or with some kind of [automapper](https://automapper.org/). – Serg Apr 08 '20 at 21:57

1 Answers1

0

Normal operation in this case is just to serialize the whole object. That way you make sure it can be deserialized without additional settings. NULL should be handled automatically AFAIK, so when serializing again you should get the same object back (with all values set to NULL or an actual value)

You would not normally send only half of an object, because you maintain a contract between the sender and the receiver for the full object, and not half of it or one third. That seems a bit odd and i cannot figure out why you would want this.

sommmen
  • 6,570
  • 2
  • 30
  • 51
  • because i want to send less data to server, so that there will be less network traffic on server. That is why , i dont want to send variables which is null – Jsennin Apr 08 '20 at 14:38
  • Really? how are you sending your data? did you actually measure that you actually need this optimazation? if you really do and you have measured your performance against your requirements, you instead of serializing an deserializing an object, you could also serialize a dictiionary (key/value) instead, and ommit keys if they're null. That will require more coding though. – sommmen Apr 09 '20 at 08:46
  • keep in mind this 'optimization' you're proposing right now is so incredibly low in gain in most situations, unless you're creating something like a low power network or someting (in which case json is not the right tool to send any data at all). If you want to send less data over the line, take a look at messagepack: https://msgpack.org/ – sommmen Apr 09 '20 at 08:48