Esempio n. 1
0
        public static JsonV3 Convert(JsonV2 jsonV2)
        {
            var res = new JsonV3();

            foreach (var product in jsonV2.products)
            {
                res.products.Add(new Product2()
                {
                    id    = int.Parse(product.Key),
                    count = product.Value.count,
                    name  = product.Value.name,
                    price = GetPrice(product.Value.price, jsonV2.constants)
                });
            }
            return(res);
        }
Esempio n. 2
0
        public void ConvertWithConstantsTest()
        {
            var dictionary = new Dictionary <string, Product>
            {
                { "1", new Product()
                  {
                      count = 1, price = "2", name = "fr"
                  } },
                { "2", new Product()
                  {
                      count = 2, price = "a+b", name = "frr"
                  } }
            };
            var dict = new Dictionary <string, double>();

            dict.Add("a", 2.2);
            dict.Add("b", 2.5);
            var jsonV2 = new JsonV2()
            {
                products  = dictionary,
                constants = dict
            };
            var actual = Convertor.Convert(jsonV2);
            var jsonV3 = new JsonV3()
            {
                products =
                    new List <Product2>()
                {
                    new Product2()
                    {
                        id = 1, count = 1, price = 2, name = "fr"
                    },
                    new Product2()
                    {
                        id = 2, count = 2, price = 4.7, name = "frr"
                    }
                }
            };

            Assert.AreEqual(JsonConvert.SerializeObject(jsonV3), JsonConvert.SerializeObject(actual));
        }
Esempio n. 3
0
        public static string ConvertJson(string json)
        {
            JObject v2 = JObject.Parse(json);

            ProductV3[] newProducts;
            var         version  = v2["version"];
            var         products = v2["products"];

            try
            {
                var constants = JsonConvert.DeserializeObject <IDictionary <string, double> >(v2["constants"].ToString());
                newProducts = GetV3Array(products, constants);
            }
            catch (NullReferenceException e)
            {
                newProducts = GetV3Array(products);
            }
            var result = new  JsonV3(newProducts);

            return(JsonConvert.SerializeObject(result, Formatting.None));
        }
Esempio n. 4
0
        public void ConvertTest()
        {
            var dictionary = new Dictionary <string, Product>
            {
                { "1", new Product()
                  {
                      count = 1, price = "2", name = "fr"
                  } },
                { "2", new Product()
                  {
                      count = 2, price = "22", name = "frr"
                  } }
            };
            var jsonV2 = new JsonV2()
            {
                products = dictionary
            };
            var actual = Convertor.Convert(jsonV2);
            var jsonV3 = new JsonV3()
            {
                products =
                    new List <Product2>()
                {
                    new Product2()
                    {
                        id = 1, count = 1, price = 2, name = "fr"
                    },
                    new Product2()
                    {
                        id = 2, count = 2, price = 22, name = "frr"
                    }
                }
            };

            Assert.AreEqual(JsonConvert.SerializeObject(jsonV3), JsonConvert.SerializeObject(actual));
        }