我有这行代码:
Message m = new Message(ciphertext, cipherKey, DateTime.Now, currentUser, serverUser);
currentUser
和serverUser
已初始化如下
serverUser = new User("Server", "svr", "Server", serverPublicKey);
currentUser = new User("Charlie", "c", "Charlie", publicKey);
m
然后序列化
string jsonMsg = JsonConvert.SerializeObject(m);
但JSON字符串仅显示以下内容
"{\"data\":\"ylPSml/wesmAqxP4DpZc7A==\",\"encryptedKey\":\"hlEX5u9eWeQb626ea4dq8D37jKyNkGGUMtdtrpKeaZxo5LfzRbCWvB9ZY5i3aL3RsG/XTf3vhLxPHztto/UJDVqGc+tQeGqUaVbgLJhCwmppmrEvciRrv3PRd/E8pCmmD69HVSN0/Vb0546AhPaI6OJqzhUpWRC+lcE7EEKNJT4=\",\"dateTime\":\"2016-01-07T19:18:08.315557+08:00\",\"dest\":null,\"src\":null}"
dest
和src
的字段均为空。我这样做错了还是SerializeObject
函数应该以这种方式工作?
修改
用户和消息类:
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;
}
}
答案 0 :(得分:3)
Message构造函数缺少两个基于用户的属性的初始化,应该如下所示:
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;
}