public void PopulateStore() { Store s = new Store(); s.Color = StoreColor.Red; s.product = new List<Product> { new Product { ExpiryDate = new DateTime(2000, 12, 3, 0, 0, 0, DateTimeKind.Utc), Name = "ProductName!", Price = 9.9m } }; s.Width = 99.99d; s.Mottos = new List<string> { "Can do!", "We deliver!" }; string json = @"{ ""Color"": 2, ""Establised"": ""\/Date(1264122061000+0000)\/"", ""Width"": 99.99, ""Employees"": 999, ""RoomsPerFloor"": [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], ""Open"": false, ""Symbol"": ""@"", ""Mottos"": [ ""Fail whale"" ], ""Cost"": 100980.1, ""Escape"": ""\r\n\t\f\b?{\\r\\n\""'"", ""product"": [ { ""Name"": ""ProductName!"", ""ExpiryDate"": ""\/Date(975801600000)\/"", ""Price"": 9.9, ""Sizes"": null } ] }"; JsonConvert.PopulateObject(json, s, new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace }); Assert.AreEqual(1, s.Mottos.Count); Assert.AreEqual("Fail whale", s.Mottos[0]); Assert.AreEqual(1, s.product.Count); //Assert.AreEqual("James", p.Name); }
public void NullValueHandlingSerialization() { Store s1 = new Store(); JsonSerializer jsonSerializer = new JsonSerializer(); jsonSerializer.NullValueHandling = NullValueHandling.Ignore; StringWriter sw = new StringWriter(); jsonSerializer.Serialize(sw, s1); //JsonConvert.ConvertDateTimeToJavaScriptTicks(s1.Establised.DateTime) Assert.AreEqual(@"{""Color"":4,""Establised"":""\/Date(1264122061000)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949532490000)\/"",""Price"":0.0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946684800000)\/"",""Price"":0.0}]}", sw.GetStringBuilder().ToString()); Store s2 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader("{}")), typeof(Store)); Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s2.Escape); Store s3 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(@"{""Escape"":null}")), typeof(Store)); Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s3.Escape); Store s4 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(@"{""Color"":2,""Establised"":""\/Date(1264071600000+1300)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949485690000+1300)\/"",""Price"":0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946638000000)\/"",""Price"":0.0}]}")), typeof(Store)); Assert.AreEqual(s1.Establised, s3.Establised); }
public void WriteSerializedStore() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Store s1 = new Store(); s1.Color = StoreColor.White; s1.Cost = 999.59m; s1.Employees = int.MaxValue - 1; s1.Open = true; s1.product.Add(new Product { ExpiryDate = new DateTime(2000, 9, 28, 3, 59, 58, DateTimeKind.Local), Name = "BSON!", Price = -0.1m, Sizes = new [] { "First", "Second" } }); s1.Establised = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local); JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, s1); ms.Seek(0, SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); Store s2 = (Store)serializer.Deserialize(reader, typeof (Store)); Assert.AreNotEqual(s1, s2); Assert.AreEqual(s1.Color, s2.Color); Assert.AreEqual(s1.Cost, s2.Cost); Assert.AreEqual(s1.Employees, s2.Employees); Assert.AreEqual(s1.Escape, s2.Escape); Assert.AreEqual(s1.Establised, s2.Establised); Assert.AreEqual(s1.Mottos.Count, s2.Mottos.Count); Assert.AreEqual(s1.Mottos.First(), s2.Mottos.First()); Assert.AreEqual(s1.Mottos.Last(), s2.Mottos.Last()); Assert.AreEqual(s1.Open, s2.Open); Assert.AreEqual(s1.product.Count, s2.product.Count); Assert.AreEqual(s1.RoomsPerFloor.Length, s2.RoomsPerFloor.Length); Assert.AreEqual(s1.Symbol, s2.Symbol); Assert.AreEqual(s1.Width, s2.Width); MemoryStream ms1 = new MemoryStream(); BsonWriter writer1 = new BsonWriter(ms1); serializer.Serialize(writer1, s1); Assert.AreEqual(ms.ToArray(), ms1.ToArray()); string s = JsonConvert.SerializeObject(s1); byte[] textData = Encoding.UTF8.GetBytes(s); int l1 = textData.Length; int l2 = ms.ToArray().Length; Console.WriteLine(l1); Console.WriteLine(l2); }