public void FriendListSerialization() { HashSet <int> f_contactList = new HashSet <int>(); f_contactList.Add(1); f_contactList.Add(2); f_contactList.Add(3); FriendList f = new FriendList(0, f_contactList); DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(FriendList)); MemoryStream m = new MemoryStream(); js.WriteObject(m, f); m.Position = 0; StreamReader s_object = new StreamReader(m); StreamReader s_file = new StreamReader("FriendListSerializationCorrectFormat.json", Encoding.UTF8); DataContractJsonSerializer jsonFriendList = new DataContractJsonSerializer(typeof(FriendList)); MemoryStream mFriendList = new MemoryStream(Encoding.ASCII.GetBytes(s_file.ReadToEnd())); MemoryStream mFriendListObject = new MemoryStream(Encoding.ASCII.GetBytes(s_object.ReadToEnd())); FriendList friendListFile = jsonFriendList.ReadObject(mFriendList) as FriendList; FriendList friendListObject = jsonFriendList.ReadObject(mFriendListObject) as FriendList; Assert.IsNotNull(friendListFile); Assert.IsTrue(friendListFile.Equals(friendListObject)); }
public void FriendListDeserializationCorrectFormat_1() { StreamReader s = new StreamReader("FriendListCorrectFormat_1.json", Encoding.UTF8); var user = JsonValue.Load(s) as JsonObject; Assert.IsNotNull(user); Assert.IsTrue((int)user["message_type"] == (int)MessageType.FRIEND_LIST); s = new StreamReader("FriendListCorrectFormat_1.json", Encoding.UTF8); DataContractJsonSerializer jsonFriendList = new DataContractJsonSerializer(typeof(FriendList)); MemoryStream m = new MemoryStream(Encoding.ASCII.GetBytes(s.ReadToEnd())); FriendList friendListClass = jsonFriendList.ReadObject(m) as FriendList; HashSet <int> f_contactList = new HashSet <int>(); f_contactList.Add(0); f_contactList.Add(2); f_contactList.Add(3); FriendList friendListObject = new FriendList(1, f_contactList); Assert.IsTrue(friendListObject.Equals(friendListClass)); }