0

I have this line of code:

Message m = new Message(ciphertext, cipherKey, DateTime.Now, currentUser, serverUser);

currentUser and serverUser have been initialized as follows

serverUser = new User("Server", "svr", "Server", serverPublicKey);
currentUser = new User("Charlie", "c", "Charlie", publicKey);

m is then Serialized

string jsonMsg = JsonConvert.SerializeObject(m);

but the JSON string only shows the following

"{\"data\":\"ylPSml/wesmAqxP4DpZc7A==\",\"encryptedKey\":\"hlEX5u9eWeQb626ea4dq8D37jKyNkGGUMtdtrpKeaZxo5LfzRbCWvB9ZY5i3aL3RsG/XTf3vhLxPHztto/UJDVqGc+tQeGqUaVbgLJhCwmppmrEvciRrv3PRd/E8pCmmD69HVSN0/Vb0546AhPaI6OJqzhUpWRC+lcE7EEKNJT4=\",\"dateTime\":\"2016-01-07T19:18:08.315557+08:00\",\"dest\":null,\"src\":null}"

The fields for dest and src are both null. Am I doing this wrong or the SerializeObject function supposed to work this way?.

Edit

User and Message class:

public class User
{
    public string displayName { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public string publicKey { get; set; }

    public User(string displayName, string id, string name, string publicKey)
    {
        this.displayName = displayName;
        this.id = id;
        this.name = name;
        this.publicKey = publicKey;
    }
}


public class Message
{
    public string data { get; set; }
    public byte[] encryptedKey { get; set; }
    public DateTime dateTime { get; set; }
    public User dest { get; set; }
    public User src { get; set; }

    public Message(string data, byte[] encryptedKey, DateTime dateTime, User dest, User src)
    {
        this.dateTime = dateTime;
        this.data = data;
        this.encryptedKey = encryptedKey;
    }
}
5
  • 4
    Could you post your User and Message class? Commented Jan 7, 2016 at 11:22
  • Ideally, post a short but complete program demonstrating the problem. Commented Jan 7, 2016 at 11:25
  • Yes, you are doing something wrong but you haven't given us enough information to say what. Commented Jan 7, 2016 at 11:25
  • 4
    Your Message constructor doesn't set dest or src. What should've been step 1 in debugging: if serialising m shows that m.dest is null, also check whether m.dest is null prior to the serialisation. Commented Jan 7, 2016 at 11:27
  • That was stupid of me. Thank you Commented Jan 7, 2016 at 11:32

1 Answer 1

3

The Message constructor is missing the initalisation of the the two User based properties and should look like the following:

public Message(string data, byte[] encryptedKey, DateTime dateTime, User dest, User src)
{
    this.dateTime = dateTime;
    this.data = data;
    this.encryptedKey = encryptedKey;

    // Note these two lines:
    this.dest = dest;
    this.src = src;
}
Sign up to request clarification or add additional context in comments.

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.