public void Can_deserialize_empty_type() { var ssModel = JsonSerializer.DeserializeFromString<Movie>("{}"); var ssDynamicModel = JsonSerializer.DeserializeFromString("{}", typeof(Movie)); var bclModel = JsonDataContractDeserializer.Instance.DeserializeFromString<Movie>("{}"); var defaultModel = new Movie(); Assert.That(ssModel, Is.EqualTo(defaultModel)); Assert.That(ssModel, Is.EqualTo(ssDynamicModel)); //It's equal except that the BCL resets Lists/Arrays to null which is stupid bclModel.Genres = new List<string>(); Assert.That(ssModel, Is.EqualTo(bclModel)); }
public void Respects_EmitDefaultValue() { using (var x = JsConfig.BeginScope()) { x.IncludeNullValues = true; var jsonModel = new Movie { Genres = null }; var ssJson = JsonSerializer.SerializeToString(jsonModel); var wcfJson = JsonDataContractSerializer.Instance.SerializeToString(jsonModel); Assert.That(ssJson, Is.EqualTo(wcfJson)); } }
public void Does_serialize_To_CamelCase() { var dto = new Movie { Id = 1, ImdbId = "tt0111161", Title = "The Shawshank Redemption", Rating = 9.2m, Director = "Frank Darabont", ReleaseDate = new DateTime(1995, 2, 17, 0, 0, 0, DateTimeKind.Utc), TagLine = "Fear can hold you prisoner. Hope can set you free.", Genres = new List<string> { "Crime", "Drama" }, }; var json = dto.ToJson(); Assert.That(json, Is.EqualTo( "{\"id\":1,\"title\":\"The Shawshank Redemption\",\"imdbId\":\"tt0111161\",\"rating\":9.2,\"director\":\"Frank Darabont\",\"releaseDate\":\"\\/Date(792979200000)\\/\",\"tagLine\":\"Fear can hold you prisoner. Hope can set you free.\",\"genres\":[\"Crime\",\"Drama\"]}")); Serialize(dto); }
/// <summary> /// Determines whether the specified <see cref="T:System.Object" /> is equal to the current /// <see cref="T:System.Object" />. /// </summary> /// <param name="other">The movie to compare to this object.</param> /// <returns> /// true if the specified <see cref="T:System.Object" /> is equal to the current /// <see cref="T:System.Object" />; otherwise, false. /// </returns> public bool Equals(Movie other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.ImdbId, ImdbId) && Equals(other.Title, Title) && other.Rating == Rating && Equals(other.Director, Director) && other.ReleaseDate.Equals(ReleaseDate) && Equals(other.TagLine, TagLine) && Genres.EquivalentTo(other.Genres); }