0

I have this class:

[Serializable]
public class ServerResponse
{
    public string[] Response { get; set; }
    public object Packet { get; set; }

    public ServerResponse(string[] response, object packet){
        this.Response = response;
        this.Packet = packet;
    }

    public string[] getResponse() { return this.Response; }
    public object getPacket() { return this.Packet; }
}

And use the following to convert to JSON:

    static void writeToClient(TcpClient client, object message)
    {
        string json = JsonConvert.SerializeObject(message);
        NetworkStream clientStream = client.GetStream();
        byte[] buffer = NetworkHelper.GetMessageAsBytes(json);

        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush();
    }

However, the Packet is never serialized. Can anyone see what I'm doing wrong? Sample output:

{"Response":["message","Authenticated"],"Packet":{}}

And this is the actual class I'm attempting to serialize:

[Serializable]
public class ActiveGameSession
{
    private LoginServerSettings serverSettings;
    private List<UserCharacter> sessionCharacters = new List<UserCharacter>();
    private UserAccount sessionAccount;

    public ActiveGameSession()
    {
        this.sessionCharacters = new List<UserCharacter>();
        this.sessionAccount = null;
        this.serverSettings = null;
    }

    public ActiveGameSession(List<UserCharacter> chars, UserAccount session)
    {
        this.sessionCharacters = chars;
        this.sessionAccount = session;
    }

    #region setters
    public void setCharacters(List<UserCharacter> chars)
    {
        this.sessionCharacters = chars;
    }

    public void setSession(UserAccount session)
    {
        this.sessionAccount = session;
    }

    public void setServerSettings(LoginServerSettings settings)
    {
        this.serverSettings = settings;
    }
    #endregion

    #region getters
    public List<UserCharacter> getCharacters()
    {
        return this.sessionCharacters;
    }

    public UserAccount getAccount()
    {
        return this.sessionAccount;
    }

    public LoginServerSettings getSettings()
    {
        return this.serverSettings;
    }
    #endregion
}

2 Answers 2

3

JSON.NET may not support serialization of an object of type Object. Try using a more concrete type. If you want to have multiple packet types you can try using generics or derive from a common base class.

Edit: Also there are no public properties of ActiveGameSession to be deserialized, please note that the getXXX, setXXX methods is not recognized as properties.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the best answer. Thanks for your help! Once I cast the object and exposed the members it was all groovy!
-1

You need to cast the object representing the Packet to a type that JSON.NET can serialize. Try casting to a string and try again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.