public void MissingMemberDeserialize() { Product product = new Product(); product.Name = "Apple"; product.ExpiryDate = new DateTime(2008, 12, 28); product.Price = 3.99M; product.Sizes = new string[] { "Small", "Medium", "Large" }; string output = JsonConvert.SerializeObject(product, Formatting.Indented); //{ // "Name": "Apple", // "ExpiryDate": new Date(1230422400000), // "Price": 3.99, // "Sizes": [ // "Small", // "Medium", // "Large" // ] //} ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); }
public void MissingMemberDeserialize() { Product product = new Product(); product.Name = "Apple"; product.ExpiryDate = new DateTime(2008, 12, 28); product.Price = 3.99M; product.Sizes = new string[] { "Small", "Medium", "Large" }; string output = JsonConvert.SerializeObject(product, Formatting.Indented); //{ // "Name": "Apple", // "ExpiryDate": new Date(1230422400000), // "Price": 3.99, // "Sizes": [ // "Small", // "Medium", // "Large" // ] //} ExceptionAssert.Throws <JsonSerializationException>(() => { ProductShort deserializedProductShort = (ProductShort)JsonConvert.DeserializeObject(output, typeof(ProductShort), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error }); }, @"Could not find member 'Price' on object of type 'ProductShort'. Path 'Price', line 4, position 10."); }
public void Json_DateParseHandling_None_Test() { var product = new ProductShort { Name = "apple", ExpiryDate = DateTime.Now, Sizes = null }; var json = JsonConvertX.SerializeObject(product, JsonConvertX.DefaultSettings); var newproduct = JsonConvertX.DeserializeObject <ProductShort>(json, JsonConvertX.DefaultSettings); Assert.Equal(product.ExpiryDate, newproduct.ExpiryDate); var settings = JsonConvertX.CreateSerializerSettings(Formatting.None); settings.DateParseHandling = DateParseHandling.None; newproduct = JsonConvertX.DeserializeObject <ProductShort>(json, JsonConvertX.DefaultSettings); Assert.Equal(product.ExpiryDate, newproduct.ExpiryDate); }
public void MissingMemberDeserializeOkay() { Product product = new Product(); product.Name = "Apple"; product.ExpiryDate = new DateTime(2008, 12, 28); product.Price = 3.99M; product.Sizes = new string[] { "Small", "Medium", "Large" }; string output = JsonConvert.SerializeObject(product); //{ // "Name": "Apple", // "ExpiryDate": new Date(1230422400000), // "Price": 3.99, // "Sizes": [ // "Small", // "Medium", // "Large" // ] //} JsonSerializer jsonSerializer = new JsonSerializer(); jsonSerializer.MissingMemberHandling = MissingMemberHandling.Ignore; object deserializedValue; using (JsonReader jsonReader = new JsonTextReader(new StringReader(output))) { deserializedValue = jsonSerializer.Deserialize(jsonReader, typeof(ProductShort)); } ProductShort deserializedProductShort = (ProductShort)deserializedValue; Assert.AreEqual("Apple", deserializedProductShort.Name); Assert.AreEqual(new DateTime(2008, 12, 28), deserializedProductShort.ExpiryDate); Assert.AreEqual("Small", deserializedProductShort.Sizes[0]); Assert.AreEqual("Medium", deserializedProductShort.Sizes[1]); Assert.AreEqual("Large", deserializedProductShort.Sizes[2]); }