Beispiel #1
0
        internal Category(string key, JsonValue data)
        {
            this.Key = key;
            Name = (string) data["name"];
            Description = (string) data["description"];
            @default = (bool) data["default"];
            KeepIf = (string) data.ValueOrDefault("keep_if");
            Benefit = (string) data.ValueOrDefault("benefit");

            Selected = Convert.ToBoolean(Registry.GetValue(@"HKEY_CURRENT_USER\Software\zr40.nl\Steam Disk Saver\Categories", key, Convert.ToInt32(@default)));
        }
        public void CreateFromComplexTest()
        {
            JsonValue target = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson);

            Assert.Equal(AnyInstance.AnyPerson.Name, (string)target["Name"]);
            Assert.Equal(AnyInstance.AnyPerson.Age, (int)target["Age"]);
            Assert.Equal(AnyInstance.AnyPerson.Address.City, (string)target.ValueOrDefault("Address", "City"));
        }
        public void LinqToJsonUndefinedTest()
        {
            JsonValue primitive = 12;

            var match = from m in primitive.ValueOrDefault("idontexist")
                        select m;

            Assert.True(match.Count() == 0);
        }
        /// <summary>
        /// Tests for <see cref="JsonValue"/> instances without dynamic references.
        /// </summary>
        public void NonDynamicTest()
        {
            int seed = GetRandomSeed();

            Log.Info("Seed: {0}", seed);
            Random rndGen = new Random(seed);

            AssertIsTrue(Assembly.GetExecutingAssembly().IsFullyTrusted == false, "Executing assembly not expected to be fully trusted!");

            Person person  = new Person(rndGen);
            Person person2 = new Person(rndGen);

            person.AddFriends(3, rndGen);
            person2.AddFriends(3, rndGen);

            JsonValue jo  = JsonValueExtensions.CreateFrom(person);
            JsonValue jo2 = JsonValueExtensions.CreateFrom(person2);

            AssertAreEqual(person.Address.City, jo["Address"]["City"].ReadAs <string>(), "Address.City");
            AssertAreEqual(person.Friends[1].Age, jo["Friends"][1]["Age"].ReadAs <int>(), "Friends[1].Age");

            string newCityName = "Bellevue";

            jo["Address"]["City"] = newCityName;
            AssertAreEqual(newCityName, (string)jo["Address"]["City"], "Address.City2");

            jo["Friends"][1] = jo2;
            AssertAreEqual(person2.Age, (int)jo["Friends"][1]["Age"], "Friends[1].Age2");

            AssertAreEqual(person2.Address.City, jo.ValueOrDefault("Friends").ValueOrDefault(1).ValueOrDefault("Address").ValueOrDefault("City").ReadAs <string>(), "Address.City3");
            AssertAreEqual(person2.Age, (int)jo.ValueOrDefault("Friends").ValueOrDefault(1).ValueOrDefault("Age"), "Friends[1].Age3");

            AssertAreEqual(person2.Address.City, jo.ValueOrDefault("Friends", 1, "Address", "City").ReadAs <string>(), "Address.City3");
            AssertAreEqual(person2.Age, (int)jo.ValueOrDefault("Friends", 1, "Age"), "Friends[1].Age3");

            int       newAge   = 42;
            JsonValue ageValue = jo["Friends"][1]["Age"] = newAge;

            AssertAreEqual(newAge, (int)ageValue, "Friends[1].Age4");
        }
Beispiel #5
0
        public void ToJsonObjectFromArrayTest()
        {
            string json     = "{\"SearchResponse\":{\"Phonebook\":{\"Results\":[{\"name\":1,\"rating\":1}, {\"name\":2,\"rating\":2}, {\"name\":3,\"rating\":3}]}}}";
            string expected = "{\"1\":{\"name\":2,\"rating\":2},\"2\":{\"name\":3,\"rating\":3}}";

            JsonValue jv         = JsonValue.Parse(json);
            double    rating     = 1;
            var       jsonResult = from n in jv.ValueOrDefault("SearchResponse", "Phonebook", "Results")
                                   where n.Value.ValueOrDefault("rating").ReadAs <double>(0.0) > rating
                                   select n;
            var jo = jsonResult.ToJsonObject();

            Assert.Equal(expected, jo.ToString());
        }
        public void DefaultConcatTest()
        {
            JsonValue jv     = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson);
            dynamic   target = JsonValueExtensions.CreateFrom(AnyInstance.AnyPerson);
            Person    person = AnyInstance.AnyPerson;

            Assert.Equal(person.Address.City, target.Address.City.ReadAs <string>());
            Assert.Equal(person.Friends[0].Age, target.Friends[0].Age.ReadAs <int>());

            Assert.Equal(target.ValueOrDefault("Address").ValueOrDefault("City"), target.Address.City);
            Assert.Equal(target.ValueOrDefault("Address", "City"), target.Address.City);

            Assert.Equal(target.ValueOrDefault("Friends").ValueOrDefault(0).ValueOrDefault("Age"), target.Friends[0].Age);
            Assert.Equal(target.ValueOrDefault("Friends", 0, "Age"), target.Friends[0].Age);

            Assert.Equal(JsonType.Default, AnyInstance.AnyJsonValue1.ValueOrDefault((object[])null).JsonType);
            Assert.Equal(JsonType.Default, jv.ValueOrDefault("Friends", null).JsonType);
            Assert.Equal(JsonType.Default, AnyInstance.AnyJsonValue1.ValueOrDefault((string)null).JsonType);
            Assert.Equal(JsonType.Default, AnyInstance.AnyJsonPrimitive.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyShort).JsonType);
            Assert.Equal(JsonType.Default, AnyInstance.AnyJsonArray.ValueOrDefault((string)null).JsonType);
            Assert.Equal(JsonType.Default, AnyInstance.AnyJsonObject.ValueOrDefault(AnyInstance.AnyString, null).JsonType);
            Assert.Equal(JsonType.Default, AnyInstance.AnyJsonArray.ValueOrDefault(-1).JsonType);

            Assert.Same(AnyInstance.AnyJsonValue1, AnyInstance.AnyJsonValue1.ValueOrDefault());

            Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((short)0));
            Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((ushort)0));
            Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((byte)0));
            Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((sbyte)0));
            Assert.Same(AnyInstance.AnyJsonArray.ValueOrDefault(0), AnyInstance.AnyJsonArray.ValueOrDefault((char)0));

            jv = new JsonObject();
            jv[AnyInstance.AnyString] = AnyInstance.AnyJsonArray;

            Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (short)0));
            Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (ushort)0));
            Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (byte)0));
            Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (sbyte)0));
            Assert.Same(jv.ValueOrDefault(AnyInstance.AnyString, 0), jv.ValueOrDefault(AnyInstance.AnyString, (char)0));

            jv = AnyInstance.AnyJsonObject;

            ExceptionHelper.Throws <ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyLong); }, String.Format(InvalidIndexType, typeof(long)));
            ExceptionHelper.Throws <ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyUInt); }, String.Format(InvalidIndexType, typeof(uint)));
            ExceptionHelper.Throws <ArgumentException>(delegate { var c = jv.ValueOrDefault(AnyInstance.AnyString, AnyInstance.AnyBool); }, String.Format(InvalidIndexType, typeof(bool)));
        }
Beispiel #7
0
        public void refreshOpenweathermap()
        {
            string town = string.Empty;
            string temp = string.Empty;

            string osadki   = string.Empty;
            string pressure = string.Empty;

            string cloudness = string.Empty;

            if (chechboxischecked == "false")
            {
                if (city == "sankt-peterburg")
                {
                    string lat = "59.93";
                    string lon = "30.29";

                    // json
                    var Gentral = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/find?lat=" + lat + "&lon=" + lon + "&APPID=c5ba134efa510143f5193f7d8e1f5bc7&units=metric");


                    // Город

                    //var GentralTestCity = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/weather?lat=35&lon=139&APPID=c5ba134efa510143f5193f7d8e1f5bc7");
                    //System.Json.JsonValue ParsGentraltest = JsonValue.Parse(GentralTestCity);

                    System.Json.JsonValue ParsGentral = JsonValue.Parse(Gentral);

                    var CityName = ParsGentral.ValueOrDefault("city");

                    System.Json.JsonValue cityPars = JsonValue.Parse(CityName.ToString());

                    var NameValue = CityName["name"];


                    if (NameValue.ToString() == "\"Novaya Gollandiya\"")

                    {
                        town = "Погода в Санкт-Петербурге";
                    }

                    // Температура


                    System.Json.JsonValue ParsGentralTemp = JsonValue.Parse(Gentral);

                    var List0 = ParsGentralTemp.ValueOrDefault("list");
                    var List1 = List0[0];

                    System.Json.JsonValue tempPars = JsonValue.Parse(List1.ToString());

                    var TempValue1 = tempPars["main"];

                    System.Json.JsonValue tempPars2 = JsonValue.Parse(TempValue1.ToString());

                    var TempValue2 = tempPars2["temp"];

                    temp = "+" + TempValue2.ToString();


                    // Оссадки

                    System.Json.JsonValue ParsGentralOsadki = JsonValue.Parse(Gentral);

                    var ListOsadki0 = ParsGentralOsadki.ValueOrDefault("list");
                    var ListOsadki1 = ListOsadki0[0];

                    System.Json.JsonValue OsadkiPars = JsonValue.Parse(ListOsadki1.ToString());

                    string OsadkiValue1 = OsadkiPars["weather"].ToString();

                    string osadkiTemp2 = new Regex(@"""description"":""(?<osadkiTemp2>[^<]+)"",").Match(OsadkiValue1).Groups["osadkiTemp2"].Value; // на будущее наклонную черту в регексе \ менять на "

                    osadki = osadkiTemp2;


                    // Давление

                    System.Json.JsonValue ParsGentralPressure = JsonValue.Parse(Gentral);

                    var ListPressure0 = ParsGentralPressure.ValueOrDefault("list");
                    var ListPressure1 = ListPressure0[0];

                    System.Json.JsonValue PressurePars = JsonValue.Parse(ListPressure1.ToString());

                    var PressureValue1 = PressurePars["main"];

                    System.Json.JsonValue PressurePars2 = JsonValue.Parse(PressureValue1.ToString());

                    var PressureValue2 = PressurePars2["pressure"];

                    pressure = Math.Round(Double.Parse(Convert.ToString(PressureValue2).Replace('.', ',')) * 0.75006375541921, 0).ToString() + " мм. рт. ст.";

                    // Облачность


                    System.Json.JsonValue ParsGentralOsadky = JsonValue.Parse(Gentral);

                    var List00 = ParsGentralOsadky.ValueOrDefault("list");
                    var List11 = List00[0];

                    System.Json.JsonValue cloudnessPars = JsonValue.Parse(List11.ToString());

                    var CloudnessValue1 = cloudnessPars["clouds"];

                    System.Json.JsonValue cloudnessPars2 = JsonValue.Parse(CloudnessValue1.ToString());

                    var cloudnessPars3 = cloudnessPars2["all"];


                    cloudness = cloudnessPars3.ToString();
                }

                else

                {
                    if (city == "moskva")
                    {
                        string lat = "55.75";
                        string lon = "37.62";

                        // json
                        var Gentral = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/find?lat=" + lat + "&lon=" + lon + "&APPID=c5ba134efa510143f5193f7d8e1f5bc7&units=metric");


                        // Город

                        //var GentralTestCity = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/weather?lat=35&lon=139&APPID=c5ba134efa510143f5193f7d8e1f5bc7");
                        //System.Json.JsonValue ParsGentraltest = JsonValue.Parse(GentralTestCity);

                        System.Json.JsonValue ParsGentral = JsonValue.Parse(Gentral);

                        var CityName = ParsGentral.ValueOrDefault("city");

                        System.Json.JsonValue cityPars = JsonValue.Parse(CityName.ToString());

                        var NameValue = CityName["name"];


                        if (NameValue.ToString() == "\"Moscow\"")



                        {
                            town = "Погода в Москве";
                        }

                        // Температура


                        System.Json.JsonValue ParsGentralTemp = JsonValue.Parse(Gentral);

                        var List0 = ParsGentralTemp.ValueOrDefault("list");
                        var List1 = List0[0];

                        System.Json.JsonValue tempPars = JsonValue.Parse(List1.ToString());

                        var TempValue1 = tempPars["main"];

                        System.Json.JsonValue tempPars2 = JsonValue.Parse(TempValue1.ToString());

                        var TempValue2 = tempPars2["temp"];

                        temp = "+" + TempValue2.ToString();


                        // Оссадки

                        System.Json.JsonValue ParsGentralOsadki = JsonValue.Parse(Gentral);

                        var ListOsadki0 = ParsGentralOsadki.ValueOrDefault("list");
                        var ListOsadki1 = ListOsadki0[0];

                        System.Json.JsonValue OsadkiPars = JsonValue.Parse(ListOsadki1.ToString());

                        string OsadkiValue1 = OsadkiPars["weather"].ToString();

                        string osadkiTemp2 = new Regex(@"""description"":""(?<osadkiTemp2>[^<]+)"",").Match(OsadkiValue1).Groups["osadkiTemp2"].Value; // на будущее наклонную черту в регексе \ менять на "

                        osadki = osadkiTemp2;


                        // Давление

                        System.Json.JsonValue ParsGentralPressure = JsonValue.Parse(Gentral);

                        var ListPressure0 = ParsGentralPressure.ValueOrDefault("list");
                        var ListPressure1 = ListPressure0[0];

                        System.Json.JsonValue PressurePars = JsonValue.Parse(ListPressure1.ToString());

                        var PressureValue1 = PressurePars["main"];

                        System.Json.JsonValue PressurePars2 = JsonValue.Parse(PressureValue1.ToString());

                        var PressureValue2 = PressurePars2["pressure"];

                        pressure = Math.Round(Double.Parse(Convert.ToString(PressureValue2).Replace('.', ',')) * 0.75006375541921, 0).ToString() + " мм. рт. ст.";

                        // Облачность


                        System.Json.JsonValue ParsGentralOsadky = JsonValue.Parse(Gentral);

                        var List00 = ParsGentralOsadky.ValueOrDefault("list");
                        var List11 = List00[0];

                        System.Json.JsonValue cloudnessPars = JsonValue.Parse(List11.ToString());

                        var CloudnessValue1 = cloudnessPars["clouds"];

                        System.Json.JsonValue cloudnessPars2 = JsonValue.Parse(CloudnessValue1.ToString());

                        var cloudnessPars3 = cloudnessPars2["all"];


                        cloudness = cloudnessPars3.ToString();
                    }

                    else

                    {
                        if (city == "kiev")
                        {
                            string lat = "50.45";
                            string lon = "30.5";

                            // json
                            var Gentral = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/find?lat=" + lat + "&lon=" + lon + "&APPID=c5ba134efa510143f5193f7d8e1f5bc7&units=metric");


                            // Город

                            //var GentralTestCity = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/weather?lat=35&lon=139&APPID=c5ba134efa510143f5193f7d8e1f5bc7");
                            //System.Json.JsonValue ParsGentraltest = JsonValue.Parse(GentralTestCity);

                            System.Json.JsonValue ParsGentral = JsonValue.Parse(Gentral);

                            var CityName = ParsGentral.ValueOrDefault("city");

                            System.Json.JsonValue cityPars = JsonValue.Parse(CityName.ToString());

                            var NameValue = CityName["name"];

                            string sdf = NameValue.ToString();

                            if (NameValue.ToString() == "\"Pushcha-Voditsa\"")

                            {
                                town = "Погода в Киеве";
                            }

                            // Температура


                            System.Json.JsonValue ParsGentralTemp = JsonValue.Parse(Gentral);

                            var List0 = ParsGentralTemp.ValueOrDefault("list");
                            var List1 = List0[0];

                            System.Json.JsonValue tempPars = JsonValue.Parse(List1.ToString());

                            var TempValue1 = tempPars["main"];

                            System.Json.JsonValue tempPars2 = JsonValue.Parse(TempValue1.ToString());

                            var TempValue2 = tempPars2["temp"];

                            temp = "+" + TempValue2.ToString();


                            // Оссадки

                            System.Json.JsonValue ParsGentralOsadki = JsonValue.Parse(Gentral);

                            var ListOsadki0 = ParsGentralOsadki.ValueOrDefault("list");
                            var ListOsadki1 = ListOsadki0[0];

                            System.Json.JsonValue OsadkiPars = JsonValue.Parse(ListOsadki1.ToString());

                            string OsadkiValue1 = OsadkiPars["weather"].ToString();

                            string osadkiTemp2 = new Regex(@"""description"":""(?<osadkiTemp2>[^<]+)"",").Match(OsadkiValue1).Groups["osadkiTemp2"].Value; // на будущее наклонную черту в регексе \ менять на "

                            osadki = osadkiTemp2;


                            // Давление

                            System.Json.JsonValue ParsGentralPressure = JsonValue.Parse(Gentral);

                            var ListPressure0 = ParsGentralPressure.ValueOrDefault("list");
                            var ListPressure1 = ListPressure0[0];

                            System.Json.JsonValue PressurePars = JsonValue.Parse(ListPressure1.ToString());

                            var PressureValue1 = PressurePars["main"];

                            System.Json.JsonValue PressurePars2 = JsonValue.Parse(PressureValue1.ToString());

                            var PressureValue2 = PressurePars2["pressure"];

                            pressure = Math.Round(Double.Parse(Convert.ToString(PressureValue2).Replace('.', ',')) * 0.75006375541921, 0).ToString() + " мм. рт. ст.";

                            // Облачность


                            System.Json.JsonValue ParsGentralOsadky = JsonValue.Parse(Gentral);

                            var List00 = ParsGentralOsadky.ValueOrDefault("list");
                            var List11 = List00[0];

                            System.Json.JsonValue cloudnessPars = JsonValue.Parse(List11.ToString());

                            var CloudnessValue1 = cloudnessPars["clouds"];

                            System.Json.JsonValue cloudnessPars2 = JsonValue.Parse(CloudnessValue1.ToString());

                            var cloudnessPars3 = cloudnessPars2["all"];


                            cloudness = cloudnessPars3.ToString();
                        }
                    }
                }
            }

            else
            {
                if (chechboxischecked == "true")

                {
                    string lat = latGeneral;
                    string lon = lonGeneral;

                    // json
                    var Gentral = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/find?lat=" + lat + "&lon=" + lon + "&APPID=c5ba134efa510143f5193f7d8e1f5bc7&units=metric");


                    // Город

                    //var GentralTestCity = new System.Net.WebClient().DownloadString("http://api.openweathermap.org/data/2.5/forecast/weather?lat=35&lon=139&APPID=c5ba134efa510143f5193f7d8e1f5bc7");
                    //System.Json.JsonValue ParsGentraltest = JsonValue.Parse(GentralTestCity);

                    System.Json.JsonValue ParsGentral = JsonValue.Parse(Gentral);

                    var CityName = ParsGentral.ValueOrDefault("city");

                    System.Json.JsonValue cityPars = JsonValue.Parse(CityName.ToString());

                    var NameValue = CityName["name"];



                    town = "Погода в " + NameValue.ToString();


                    // Температура


                    System.Json.JsonValue ParsGentralTemp = JsonValue.Parse(Gentral);

                    var List0 = ParsGentralTemp.ValueOrDefault("list");
                    var List1 = List0[0];

                    System.Json.JsonValue tempPars = JsonValue.Parse(List1.ToString());

                    var TempValue1 = tempPars["main"];

                    System.Json.JsonValue tempPars2 = JsonValue.Parse(TempValue1.ToString());

                    var TempValue2 = tempPars2["temp"];

                    temp = "+" + TempValue2.ToString();


                    // Оссадки

                    System.Json.JsonValue ParsGentralOsadki = JsonValue.Parse(Gentral);

                    var ListOsadki0 = ParsGentralOsadki.ValueOrDefault("list");
                    var ListOsadki1 = ListOsadki0[0];

                    System.Json.JsonValue OsadkiPars = JsonValue.Parse(ListOsadki1.ToString());

                    string OsadkiValue1 = OsadkiPars["weather"].ToString();

                    string osadkiTemp2 = new Regex(@"""description"":""(?<osadkiTemp2>[^<]+)"",").Match(OsadkiValue1).Groups["osadkiTemp2"].Value; // на будущее наклонную черту в регексе \ менять на "

                    osadki = osadkiTemp2;


                    // Давление

                    System.Json.JsonValue ParsGentralPressure = JsonValue.Parse(Gentral);

                    var ListPressure0 = ParsGentralPressure.ValueOrDefault("list");
                    var ListPressure1 = ListPressure0[0];

                    System.Json.JsonValue PressurePars = JsonValue.Parse(ListPressure1.ToString());

                    var PressureValue1 = PressurePars["main"];

                    System.Json.JsonValue PressurePars2 = JsonValue.Parse(PressureValue1.ToString());

                    var PressureValue2 = PressurePars2["pressure"];

                    pressure = Math.Round(Double.Parse(Convert.ToString(PressureValue2).Replace('.', ',')) * 0.75006375541921, 0).ToString() + " мм. рт. ст.";

                    // Облачность


                    System.Json.JsonValue ParsGentralOsadky = JsonValue.Parse(Gentral);

                    var List00 = ParsGentralOsadky.ValueOrDefault("list");
                    var List11 = List00[0];

                    System.Json.JsonValue cloudnessPars = JsonValue.Parse(List11.ToString());

                    var CloudnessValue1 = cloudnessPars["clouds"];

                    System.Json.JsonValue cloudnessPars2 = JsonValue.Parse(CloudnessValue1.ToString());

                    var cloudnessPars3 = cloudnessPars2["all"];


                    cloudness = cloudnessPars3.ToString();
                }
            }



            TBCityResult.Text      = town;
            TBtempResult.Text      = temp;
            TBCloudnessResult.Text = cloudness;
            TBCloudsResult.Text    = osadki;
            TBPressureResult.Text  = pressure;

            InterFase();
        }