Exemplo n.º 1
0
        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.Equal(1, s.Mottos.Count);
            Assert.Equal("Fail whale", s.Mottos[0]);
            Assert.Equal(1, s.product.Count);

            //Assert.Equal("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.Equal(@"{""Color"":4,""Establised"":""2010-01-22T01:01:01Z"",""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"":""2000-02-02T23:01:30Z"",""Price"":0.0},{""Name"":""Alien"",""ExpiryDate"":""2000-01-01T00:00:00Z"",""Price"":0.0}]}", sw.GetStringBuilder().ToString());

            Store s2 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader("{}")), typeof(Store));
            Assert.Equal("\r\n\t\f\b?{\\r\\n\"\'", s2.Escape);

            Store s3 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader(@"{""Escape"":null}")), typeof(Store));
            Assert.Equal("\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.Equal(s1.Establised, s3.Establised);
        }
Exemplo n.º 3
0
        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.NotEqual(s1, s2);
            Assert.Equal(s1.Color, s2.Color);
            Assert.Equal(s1.Cost, s2.Cost);
            Assert.Equal(s1.Employees, s2.Employees);
            Assert.Equal(s1.Escape, s2.Escape);
            Assert.Equal(s1.Establised, s2.Establised);
            Assert.Equal(s1.Mottos.Count, s2.Mottos.Count);
            Assert.Equal(s1.Mottos.First(), s2.Mottos.First());
            Assert.Equal(s1.Mottos.Last(), s2.Mottos.Last());
            Assert.Equal(s1.Open, s2.Open);
            Assert.Equal(s1.product.Count, s2.product.Count);
            Assert.Equal(s1.RoomsPerFloor.Length, s2.RoomsPerFloor.Length);
            Assert.Equal(s1.Symbol, s2.Symbol);
            Assert.Equal(s1.Width, s2.Width);

            MemoryStream ms1 = new MemoryStream();
            BsonWriter writer1 = new BsonWriter(ms1);

            serializer.Serialize(writer1, s1);

            Assert.Equal(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);
        }