Exemplo n.º 1
0
        public Store()
        {
            Mottos.Add("Hello World");
            Mottos.Add("öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~");
            Mottos.Add(null);
            Mottos.Add(" ");

            Product rocket = new Product();
            rocket.Name = "Rocket";
            rocket.ExpiryDate = new DateTime(2000, 2, 2, 23, 1, 30, DateTimeKind.Utc);
            Product alien = new Product();
            alien.Name = "Alien";

            product.Add(rocket);
            product.Add(alien);
        }
        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.Equal("Apple", deserializedProductShort.Name);
            Assert.Equal(new DateTime(2008, 12, 28), deserializedProductShort.ExpiryDate);
            Assert.Equal("Small", deserializedProductShort.Sizes[0]);
            Assert.Equal("Medium", deserializedProductShort.Sizes[1]);
            Assert.Equal("Large", deserializedProductShort.Sizes[2]);
        }
        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"
            //  ]
            //}

			AssertException.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 11.");
        }
Exemplo n.º 4
0
        public void SerializeEmptyAndNullStrings()
        {
            Product p = new Product();
            p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
            p.Name = null;
            p.Price = 9.95m;
            p.Sizes = new[] { "Small", "", null };

            MemoryStream ms = new MemoryStream();
            JsonSerializer serializer = new JsonSerializer();

            BsonWriter writer = new BsonWriter(ms);
            serializer.Serialize(writer, p);

            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            Product deserializedProduct = serializer.Deserialize<Product>(reader);

            Console.WriteLine(deserializedProduct.Name);

            Assert.Equal(null, deserializedProduct.Name);
            Assert.Equal(9.95m, deserializedProduct.Price);
            Assert.Equal(3, deserializedProduct.Sizes.Length);
            Assert.Equal("Small", deserializedProduct.Sizes[0]);
            Assert.Equal("", deserializedProduct.Sizes[1]);
            Assert.Equal(null, deserializedProduct.Sizes[2]);
        }
Exemplo n.º 5
0
        public void Example()
        {
            Product p = new Product();
            p.ExpiryDate = DateTime.Parse("2009-04-05T14:45:00Z");
            p.Name = "Carlos' Spicy Wieners";
            p.Price = 9.95m;
            p.Sizes = new[] { "Small", "Medium", "Large" };

            MemoryStream ms = new MemoryStream();
            JsonSerializer serializer = new JsonSerializer();

            // serialize product to BSON
            BsonWriter writer = new BsonWriter(ms);
            serializer.Serialize(writer, p);

            Console.WriteLine(BitConverter.ToString(ms.ToArray()));
            // 7C-00-00-00-02-4E-61-6D-65-00-16-00-00-00-43-61-72-6C-
            // 6F-73-27-20-53-70-69-63-79-20-57-69-65-6E-65-72-73-00-
            // 09-45-78-70-69-72-79-44-61-74-65-00-E0-51-BD-76-20-01-
            // 00-00-01-50-72-69-63-65-00-66-66-66-66-66-E6-23-40-04-
            // 53-69-7A-65-73-00-2D-00-00-00-02-30-00-06-00-00-00-53-
            // 6D-61-6C-6C-00-02-31-00-07-00-00-00-4D-65-64-69-75-6D-
            // 00-02-32-00-06-00-00-00-4C-61-72-67-65-00-00-00


            ms.Seek(0, SeekOrigin.Begin);

            // deserialize product from BSON
            BsonReader reader = new BsonReader(ms);
            Product deserializedProduct = serializer.Deserialize<Product>(reader);

            Console.WriteLine(deserializedProduct.Name);
            // Carlos' Spicy Wieners


            Assert.Equal("Carlos' Spicy Wieners", deserializedProduct.Name);
            Assert.Equal(9.95m, deserializedProduct.Price);
            Assert.Equal(3, deserializedProduct.Sizes.Length);
        }
        public void BlogPostExample()
        {
            Product product = new Product
            {
                ExpiryDate = new DateTime(2010, 12, 20, 18, 1, 0, DateTimeKind.Utc),
                Name = "Widget",
                Price = 9.99m,
                Sizes = new[] { "Small", "Medium", "Large" }
            };

            string json =
                JsonConvert.SerializeObject(
                    product,
                    Formatting.Indented,
                    new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }
                    );

            //{
            //  "name": "Widget",
            //  "expiryDate": "\/Date(1292868060000)\/",
            //  "price": 9.99,
            //  "sizes": [
            //    "Small",
            //    "Medium",
            //    "Large"
            //  ]
            //}

            StringAssert.Equal(@"{
  ""name"": ""Widget"",
  ""expiryDate"": ""2010-12-20T18:01:00Z"",
  ""price"": 9.99,
  ""sizes"": [
    ""Small"",
    ""Medium"",
    ""Large""
  ]
}", json);
        }
        public void SingleItemArrayPropertySerialization()
        {
            Product product = new Product();

            product.Name = "Apple";
            product.ExpiryDate = new DateTime(2008, 12, 28, 0, 0, 0, DateTimeKind.Utc);
            product.Price = 3.99M;
            product.Sizes = new string[] { "Small" };

            string output = JsonConvert.SerializeObject(product, new IsoDateTimeConverter());

            XmlDocument xmlProduct = JsonConvert.DeserializeXmlNode(output, "product", true);

            StringAssert.Equal(@"<product>
  <Name>Apple</Name>
  <ExpiryDate>2008-12-28T00:00:00Z</ExpiryDate>
  <Price>3.99</Price>
  <Sizes json:Array=""true"" xmlns:json=""http://james.newtonking.com/projects/json"">Small</Sizes>
</product>", IndentXml(xmlProduct.InnerXml));

            string output2 = JsonConvert.SerializeXmlNode(xmlProduct.DocumentElement, Formatting.Indented);

            StringAssert.Equal(@"{
  ""product"": {
    ""Name"": ""Apple"",
    ""ExpiryDate"": ""2008-12-28T00:00:00Z"",
    ""Price"": ""3.99"",
    ""Sizes"": [
      ""Small""
    ]
  }
}", output2);
        }