public void DeserializeFromJson_WithNestedObject_ShouldWork() { var json = (JsDictionary<string, object>) Json.Parse( @" { ""Title"": ""Test Title"", ""Content"": ""Test Text"", ""Author"": { ""Email"": ""*****@*****.**"", ""Id"": 2 } } "); var post = new PostViewModel(); post.SetFromJSON(json, null); Assert.AreEqual(post.Title, "Test Title"); Assert.AreEqual(post.Content, "Test Text"); Assert.AreEqual(post.Author.Id, 2); Assert.AreEqual(post.Author.Email, "*****@*****.**"); }
public void DeserializeFromJson_WithNestedObject_WithExistingNestedObjectReference_ShouldPreserveObjectIdentity() { var json = (JsDictionary<string, object>) Json.Parse( @" { ""Title"": ""Test Title"", ""Content"": ""Test Text"", ""Author"": { ""Email"": ""*****@*****.**"", ""Id"": 2 } } "); var post = new PostViewModel(); var originalAuthor = new UserViewModel {Id = 3, Email = "*****@*****.**"}; var anotherAuthor = new UserViewModel {Id = 2, Email = "*****@*****.**"}; post.Author = originalAuthor; post.SetFromJSON(json, null); Assert.AreEqual(post.Title, "Test Title"); Assert.AreEqual(post.Content, "Test Text"); Assert.AreEqual(post.Author.Id, 2); Assert.AreEqual(post.Author.Email, "*****@*****.**"); Assert.IsTrue(post.Author == originalAuthor, "identity equality check - ensure any existing nested object's identity is preserved by FromJson, otherwise existing event bindings to be lost."); Assert.IsTrue(post.Author != anotherAuthor, "identity equality check - verify that 2 instances which are known to have different identities are marked as not equal"); }