Ejemplo n.º 1
0
        public void ToStringTest()
        {
            UniValue value = UniValue.ParseXml("<response><item>1</item><item>2</item><item>3</item></response>");

            Assert.Equal("{ \"response\": { \"item\": [\"1\", \"2\", \"3\"] } }", value.ToString());

            value["response"]["item"]["test"] = "123";

            Assert.Equal("{ \"response\": { \"item\": { \"0\": \"1\", \"1\": \"2\", \"2\": \"3\", \"test\": \"123\" } } }", value.ToString());

            value = UniValue.ParseJson("{\"data\": [{\"id\": 1, \"name\": \"test\"}, {\"id\": 2, \"name\": \"test2\"}, {\"id\": 3, \"name\": \"test3\", \"list\": [{\"x\":1,\"y\":\"hi\"}, {\"x\":2, \"y\":\"hello\"}] }] }");

            Assert.Equal("{ \"data\": [{ \"id\": 1, \"name\": \"test\" }, { \"id\": 2, \"name\": \"test2\" }, { \"id\": 3, \"name\": \"test3\", \"list\": [{ \"x\": 1, \"y\": \"hi\" }, { \"x\": 2, \"y\": \"hello\" }] }] }", value.ToString());
            Assert.Equal("[{ \"id\": 1, \"name\": \"test\" }, { \"id\": 2, \"name\": \"test2\" }, { \"id\": 3, \"name\": \"test3\", \"list\": [{ \"x\": 1, \"y\": \"hi\" }, { \"x\": 2, \"y\": \"hello\" }] }]", value["data"].ToString());
            Assert.Equal("test2", value["data"][1]["name"].ToString());

            value = UniValue.ParseXml("<response><item id='1' style='bold' color='white' /><item id='2' style='italic' color='red'>123</item></response>");

            Assert.True(value == "{ \"response\": { \"item\": [{ \"@id\": \"1\", \"@style\": \"bold\", \"@color\": \"white\" }, { \"value\": \"123\", \"@id\": \"2\", \"@style\": \"italic\", \"@color\": \"red\" }] } }");
            Assert.True(value["response"] == "{ \"item\": [{ \"@id\": \"1\", \"@style\": \"bold\", \"@color\": \"white\" }, { \"value\": \"123\", \"@id\": \"2\", \"@style\": \"italic\", \"@color\": \"red\" }] }");
            Assert.True(value["response"]["item"] == "[{ \"@id\": \"1\", \"@style\": \"bold\", \"@color\": \"white\" }, { \"value\": \"123\", \"@id\": \"2\", \"@style\": \"italic\", \"@color\": \"red\" }]");
            Assert.True(String.IsNullOrEmpty(value["response"]["item"][0].ToString()));
            Assert.True(value["response"]["item"][0].ToString() == "");
            Assert.False(UniValue.IsNullOrEmpty(value["response"]["item"][0]));
            Assert.True(value["response"]["item"][1] == "123");
            Assert.True(value["response"]["item"][1] == 123);
            Assert.True(value["response"]["item"][0]["@id"] == 1);
            Assert.True(value["response"]["item"][0]["style"] == "bold");

            value = UniValue.ParseXml("<response><item id='1' style='bold' color='white'><vvalue abc='test' avalue='1234567'></vvalue></item><item id='2' style='italic' color='red'><v>456</v></item></response>");

            Assert.Equal("{ \"response\": { \"item\": [{ \"value\": { \"vvalue\": { \"@abc\": \"test\", \"@avalue\": \"1234567\" } }, \"@id\": \"1\", \"@style\": \"bold\", \"@color\": \"white\" }, { \"value\": { \"v\": \"456\" }, \"@id\": \"2\", \"@style\": \"italic\", \"@color\": \"red\" }] } }", value.ToString());
            Assert.Equal("[{ \"value\": { \"vvalue\": { \"@abc\": \"test\", \"@avalue\": \"1234567\" } }, \"@id\": \"1\", \"@style\": \"bold\", \"@color\": \"white\" }, { \"value\": { \"v\": \"456\" }, \"@id\": \"2\", \"@style\": \"italic\", \"@color\": \"red\" }]", value["response"]["item"].ToString());
            Assert.Equal("{ \"vvalue\": { \"@abc\": \"test\", \"@avalue\": \"1234567\" } }", value["response"]["item"][0].ToString());

            value = UniValue.ParseJson("{data: [{id: 1, name: null}, {id: 2, name: 'test2\r\n123'}, {id: 3, name: 'test3', list: [{x:1,y:'hi'}, {x:2, y:'hello'}] }] }");

            Assert.Equal("{ \"data\": [{ \"id\": 1, \"name\": null }, { \"id\": 2, \"name\": \"test2\\r\\n123\" }, { \"id\": 3, \"name\": \"test3\", \"list\": [{ \"x\": 1, \"y\": \"hi\" }, { \"x\": 2, \"y\": \"hello\" }] }] }", value.ToString());

            value = "test\r\ntest";

            Assert.Equal("test\r\ntest", value.ToString());
        }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;

            if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            object parents = null;

            if (!String.IsNullOrEmpty(this.CurrentFolderId))
            {
                parents = new object[] { new { id = this.CurrentFolderId } };
            }

            UniValue properties = UniValue.Create
                                  (
                new
            {
                title   = Path.GetFileName(openFileDialog1.FileName),
                parents = parents
            }
                                  );

            var file = openFileDialog1.OpenFile();

            this.CurrentFileLength = file.Length;

            var parameters = new HttpParameterCollection();

            parameters.Add("uploadType", "multipart");
            parameters.AddContent("application/json", properties.ToString());
            parameters.AddContent("application/octet-stream", file);

            OAuthUtility.PostAsync
            (
                "https://www.googleapis.com/upload/drive/v2/files",
                parameters,
                authorization: new HttpAuthorization(AuthorizationType.Bearer, Properties.Settings.Default.AccessToken),
                contentType: "multipart/related",
                // handler of result
                callback: Upload_Result,
                // handler of uploading
                streamWriteCallback: Upload_Processing
            );
        }
Ejemplo n.º 3
0
        public async Task ChageFileName(string filename)
        {
            string   uri        = "https://api.onedrive.com/v1.0/drive/items/" + Item.FileID;
            string   method     = "PATCH";
            UniValue properties = UniValue.Create(new { name = filename });

            try
            {
                byte[] buf = Encoding.UTF8.GetBytes(properties.ToString());
                System.Net.HttpWebResponse rp = await HttpHelper.RequstHttp(method, uri, null, Item.driveinfo.token.access_token, null, buf, buf.Length, 0, buf.Length);

                Item.Path     = Item.Path.Replace(Item.FileName, filename);
                Item.FileName = filename;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 4
0
        public async Task ChageFileName(string filename)
        {
            string   uri        = "https://www.googleapis.com/drive/v1/files/" + Item.FileID + "?updateViewedDate=true&updateModifiedDate=true";
            UniValue properties = UniValue.Create(new { title = filename });
            string   method     = "PATCH";

            try
            {
                byte[] buf = Encoding.UTF8.GetBytes(properties.ToString());
                System.Net.HttpWebResponse rp = await HttpHelper.RequstHttp(method, uri, null, Item.driveinfo.token.access_token, null, buf, buf.Length, 0, buf.Length);

                item.Path     = item.Path.Replace(item.FileName, filename);
                Item.FileName = filename;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 5
0
        public async Task <CloudFiles> CreateFolder(string foldername, string parentid)
        {
            string   method     = "POST";
            string   uri        = string.Format("https://api.onedrive.com/v1.0/drive/items/{0}/children", parentid);
            UniValue properties = UniValue.Create(new { name = foldername, folder = new { } });

            try
            {
                byte[] buf = Encoding.UTF8.GetBytes(properties.ToString());
                System.Net.HttpWebResponse rp = await HttpHelper.RequstHttp(method, uri, null, driveinfo.token.access_token, null, buf, buf.Length, 0, buf.Length);

                Dictionary <string, object> dic = HttpHelper.DerealizeJson(rp.GetResponseStream());
                CloudFiles file = AddFiles(dic);
                return(file);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 6
0
        public async Task <string> GetGoogleUploadUrl()
        {
            object parent = null;

            parent = new object[] { new { id = currentfileid } };
            UniValue properties = UniValue.Create(new { title = path, parents = parent, description = "AllCloudeByUpLoad", fileSize = maxsize });

            byte[]          date = Encoding.UTF8.GetBytes(properties.ToString());
            HttpWebResponse respone;

            try
            {
                respone = await HttpHelper.RequstHttp("POST", "https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable", null, accesstoken, null, date, date.Length, 0, date.Length);
            }
            catch (Exception e)
            {
                throw e;
            }
            return(respone.Headers["Location"].ToString());
        }
Ejemplo n.º 7
0
        public async Task CopyFile(FileInfo parentfile)
        {
            string   method   = "POST";
            UniValue properte = UniValue.Create(new { name = Item.FileName, parentReference = new { id = parentfile.FileID } });

            System.Collections.Specialized.NameValueCollection header = new System.Collections.Specialized.NameValueCollection();
            header.Add("Prefer", "respond-async");
            byte[] buf = Encoding.UTF8.GetBytes(properte.ToString());
            string uri = string.Format("https://api.onedrive.com/v1.0/drive/items/{0}/action.copy", Item.FileID);

            try
            {
                System.Net.HttpWebResponse rp = await HttpHelper.RequstHttp(method, uri, null, Item.driveinfo.token.access_token, header, buf, buf.Length, 0, buf.Length);
            }
            catch (System.Net.WebException e)
            {
                Dictionary <string, object> ErrorBlinkStyle = HttpHelper.DerealizeJson(e.Response.GetResponseStream());
                throw e;
            }
            return;
        }
Ejemplo n.º 8
0
        public async Task <CloudFiles> CreateFolder(string foldername, string parentid)
        {
            string method = "POST";
            string uri    = "https://www.googleapis.com/drive/v2/files";
            object parent;

            parent = new object[] { new { id = parentid } };
            UniValue properties = UniValue.Create(new { title = foldername, parents = parent, mimeType = "application/vnd.google-apps.folder" });

            try
            {
                byte[] buf = Encoding.UTF8.GetBytes(properties.ToString());
                System.Net.HttpWebResponse rp = await HttpHelper.RequstHttp(method, uri, null, driveinfo.token.access_token, null, buf, buf.Length, 0, buf.Length);

                Dictionary <string, object> dic = HttpHelper.DerealizeJson(rp.GetResponseStream());
                CloudFiles file = AddFiles(dic);
                return(file);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 9
0
        private void button1_Click(object sender, EventArgs e)
        {
            // help: https://developers.google.com/drive/v2/reference/files/insert

            object parents = null;

            if (!String.IsNullOrEmpty(this.CurrentFolderId))
            {
                parents = new object[] { new { id = this.CurrentFolderId } };
            }

            UniValue content = UniValue.Create
                               (
                new
            {
                mimeType = "application/vnd.google-apps.folder",
                title    = textBox1.Text,
                parents  = parents
            }
                               );

            var parameters = new HttpParameterCollection();

            parameters.Encoding = Encoding.UTF8;
            parameters.Add("uploadType", "multipart");
            parameters.AddContent("application/json", content.ToString());

            OAuthUtility.PostAsync
            (
                "https://www.googleapis.com/upload/drive/v2/files",
                parameters: parameters,
                authorization: new HttpAuthorization(AuthorizationType.Bearer, Properties.Settings.Default.AccessToken),
                contentType: "multipart/related",
                callback: CreateFolder_Result
            );
        }
Ejemplo n.º 10
0
 public Download(UniValue path) : this()
 {
     this.FilePath = path.ToString();
     this.saveFileDialog1.FileName = this.LoadingFileName = Path.GetFileName(this.FilePath);
     this.Text = String.Format("Loading '{0}'", this.LoadingFileName);
 }
Ejemplo n.º 11
0
        public DriveInfo(UniValue source, ApiDataMapping mapping)
        {
            if (mapping == null || !source.HasValue)
            {
                return;
            }
            this.Items = source.ToDictionary();
            var t = typeof(DriveInfo);

            foreach (var p in t.GetProperties())
            {
                var item = mapping.FirstOrDefault(itm => itm.DestinationName.Equals(p.Name, StringComparison.OrdinalIgnoreCase));
                if (item != null && source.ContainsKey(item.SourceName))
                {
                    object   vr = null;
                    UniValue vs = source[item.SourceName];
                    if (item.Parse != null)
                    {
                        vr = item.Parse(vs);
                    }
                    else
                    {
                        if (item.Type == typeof(DateTime))
                        {
                            var f = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name, true);
                            var formatDateTime = "dd.MM.yyyy HH:mm:ss";
                            if (!String.IsNullOrEmpty(item.Format))
                            {
                                formatDateTime = item.Format;
                            }
                            f.DateTimeFormat.FullDateTimePattern = formatDateTime;
                            f.DateTimeFormat.ShortDatePattern    = formatDateTime;
                            DateTime dateValue;
                            if (DateTime.TryParse(vs.ToString(), f, DateTimeStyles.NoCurrentDateDefault, out dateValue))
                            {
                                vr = dateValue;
                            }
                            else
                            {
                                vr = null;
                            }
                        }
                        else if (item.Type == typeof(bool))
                        {
                            vr = Convert.ToBoolean(vs);
                        }
                        else if (item.Type == typeof(Int16))
                        {
                            vr = Convert.ToInt16(vs);
                        }
                        else if (item.Type == typeof(Int32))
                        {
                            vr = Convert.ToInt32(vs);
                        }
                        else if (item.Type == typeof(Int64))
                        {
                            vr = Convert.ToInt64(vs);
                        }
                        else if (item.Type == typeof(UInt16))
                        {
                            vr = Convert.ToUInt16(vs);
                        }
                        else if (item.Type == typeof(UInt32))
                        {
                            vr = Convert.ToUInt32(vs);
                        }
                        else if (item.Type == typeof(UInt64))
                        {
                            vr = Convert.ToUInt64(vs);
                        }
                        else if (item.Type == typeof(double))
                        {
                            vr = Convert.ToDouble(vs);
                        }
                        else if (item.Type == typeof(Single))
                        {
                            vr = Convert.ToSingle(vs);
                        }
                        else if (item.Type == typeof(decimal))
                        {
                            vr = Convert.ToDecimal(vs);
                        }
                        else if (item.Type == typeof(byte))
                        {
                            vr = Convert.ToByte(vs);
                        }
                        else if (item.Type == typeof(char))
                        {
                            vr = Convert.ToChar(vs);
                        }
                        else if (item.Type == typeof(string))
                        {
                            vr = Convert.ToString(vs);
                        }
                        else
                        {
                            vr = Convert.ToString(vs);
                        }
                    }
                    p.SetValue(this, vr, null);
                }
            }
        }
Ejemplo n.º 12
0
        public void UniValue_Common()
        {
            Console.WriteLine("Test 1: Types");
            var test = new Dictionary <string, UniValue>();

            test.Add("a", 123);
            test.Add("b", "test");
            test.Add("c", DateTime.Now);
            test.Add("d", 4.2);
            test.Add("e", new byte[] { 1, 2, 3 });
            test.Add("f", UniValue.Create(new Uri("http://localhost")));
            test.Add("g", UniValue.Create(new { id = 123, name = "tester", date = DateTime.Now, obj = new { a = 1, b = 2, c = 3, d = new int[] { 1, 2, 3 } } }));
            test.Add("h", new int[] { 1, 2, 3 });
            test.Add("i", new string[] { "a", "b", "c" });
            StringBuilder sb = new StringBuilder(); sb.Append("xyz");

            test.Add("j", sb);

            /*ResultValue a = 123;
             * ResultValue b = "test";
             * ResultValue c = DateTime.Now;
             * ResultValue d = 4.2;
             * ResultValue e = new byte[] { 1, 2, 3 };
             * ResultValue f = new Uri("http://localhost");*/

            foreach (var key in test.Keys)
            {
                Console.WriteLine("{0} = {1}", key, test[key]);
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 2: Parse");

            Console.WriteLine("JSON");
            try
            {
                var r = UniValue.ParseJson("test,1,#2,$3");
                Assert.Fail("Invalid parse: ERROR");
            }
            catch
            {
                Console.WriteLine("Invalid parse: OK");
            }
            try
            {
                UniValue r = UniValue.Empty;
                if (!UniValue.TryParseJson("test,1,#2,$3", out r))
                {
                    Console.WriteLine("Try parse #1: OK");
                }
                else
                {
                    Assert.Fail("Try parse #1: ERROR");
                }
                if (UniValue.TryParseJson("[1,2,3]", out r))
                {
                    Console.WriteLine("Try parse #2: OK / {0}", r);
                }
                else
                {
                    Assert.Fail("Try parse #2: ERROR");
                }
            }
            catch
            {
                Assert.Fail("Try parse: ERROR");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("XML");
            try
            {
                var r = UniValue.ParseXml("test,1,#2,$3");
                Assert.Fail("Invalid parse: ERROR");
            }
            catch
            {
                Console.WriteLine("Invalid parse: OK");
            }
            try
            {
                UniValue r = UniValue.Empty;
                if (!UniValue.TryParseXml("test,1,#2,$3", out r))
                {
                    Console.WriteLine("Try parse #1: OK");
                }
                else
                {
                    Assert.Fail("Try parse #1: ERROR");
                }
                if (UniValue.TryParseXml("<items><item>1</item><item>2</item><item>3</item></items>", out r))
                {
                    Console.WriteLine("Try parse #2: OK / {0}", r);
                }
                else
                {
                    Assert.Fail("Try parse #2: ERROR");
                }
            }
            catch
            {
                Assert.Fail("Try parse: ERROR");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("PARAMETERS");
            try
            {
                var r = UniValue.ParseParameters("test,1,#2,$3\r\n");
                Assert.Fail("Invalid parse: ERROR");
            }
            catch
            {
                Console.WriteLine("Invalid parse: OK");
            }
            try
            {
                UniValue r = UniValue.Empty;
                if (!UniValue.TryParseParameters("test,1,#2,$3\r\n", out r))
                {
                    Console.WriteLine("Try parse #1: OK");
                }
                else
                {
                    Assert.Fail("Try parse #1: ERROR");
                }
                if (UniValue.TryParseParameters("a=1&b=2&c=3", out r))
                {
                    Console.WriteLine("Try parse #2: OK / {0}", r);
                }
                else
                {
                    Assert.Fail("Try parse #2: ERROR");
                }
            }
            catch
            {
                Assert.Fail("Try parse: ERROR");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 3");

            Console.WriteLine("ToString");

            UniValue r2  = "world!";
            string   ttt = "Hello, " + r2;

            Console.WriteLine("IsString: {0}", r2.IsString);
            if (!r2.IsString)
            {
                Assert.Fail();
            }
            Console.WriteLine(ttt);
            Console.WriteLine("OK");

            Console.WriteLine("ToInt32");

            r2 = 123;
            int num = (int)r2;

            num = Convert.ToInt32(r2);

            r2  = "ff";
            num = Convert.ToInt32(r2.ToString(), 16);

            r2  = "123";
            num = (int)r2;

            r2  = 123.123;
            num = (int)r2;

            r2  = "123.123";
            num = (int)r2;

            r2  = "123,123";
            num = (int)r2;

            try
            {
                r2  = "abc";
                num = (int)r2;
                Assert.Fail();
            }
            catch
            {
            }
            Console.WriteLine("OK");

            Console.WriteLine("ToDouble");
            r2 = 123;
            double num2 = (double)r2;

            r2   = "123";
            num2 = (double)r2;

            r2   = "123.123";
            num2 = (double)r2;

            r2   = "123,123";
            num2 = (double)r2;

            try
            {
                r2   = "abc";
                num2 = (double)r2;
                Assert.Fail();
            }
            catch
            {
            }
            Console.WriteLine("OK");

            Console.WriteLine("ToDateTime");

            r2 = "Thu Dec 04, 2014";
            Convert.ToDateTime(r2);

            Console.WriteLine("OK");
        }
Ejemplo n.º 13
0
        public void UniValue_ToString()
        {
            Console.WriteLine("Test 1");
            UniValue value = UniValue.ParseXml("<response><item>1</item><item>2</item><item>3</item></response>");

            Console.WriteLine(value);
            Console.WriteLine("-------------------------------------");

            if (value != "{ \"response\": { \"item\": [\"1\", \"2\", \"3\"] } }")
            {
                Assert.Fail();
            }
            else
            {
                Console.WriteLine("OK");
            }

            value["response"]["item"]["test"] = "123";
            Console.WriteLine(value);

            if (value != "{ \"response\": { \"item\": { \"0\": \"1\", \"1\": \"2\", \"2\": \"3\", \"test\": \"123\" } } }")
            {
                Assert.Fail();
            }
            else
            {
                Console.WriteLine("OK");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 2");
            value = UniValue.ParseJson("{\"data\": [{\"id\": 1, \"name\": \"test\"}, {\"id\": 2, \"name\": \"test2\"}, {\"id\": 3, \"name\": \"test3\", \"list\": [{\"x\":1,\"y\":\"hi\"}, {\"x\":2, \"y\":\"hello\"}] }] }");
            Console.WriteLine("-------------------------------------");

            Console.WriteLine("[] = {0}", value);
            if (value != "{ \"data\": [{ \"id\": 1, \"name\": \"test\" }, { \"id\": 2, \"name\": \"test2\" }, { \"id\": 3, \"name\": \"test3\", \"list\": [{ \"x\": 1, \"y\": \"hi\" }, { \"x\": 2, \"y\": \"hello\" }] }] }")
            {
                Assert.Fail();
            }
            else
            {
                Console.WriteLine("OK");
            }

            Console.WriteLine("[data] = {0}", value["data"]);
            if (value["data"] != "[{ \"id\": 1, \"name\": \"test\" }, { \"id\": 2, \"name\": \"test2\" }, { \"id\": 3, \"name\": \"test3\", \"list\": [{ \"x\": 1, \"y\": \"hi\" }, { \"x\": 2, \"y\": \"hello\" }] }]")
            {
                Assert.Fail();
            }
            else
            {
                Console.WriteLine("OK");
            }

            Console.WriteLine("[data][1][name] = {0}", value["data"][1]["name"]);
            if (value["data"][1]["name"] != "test2")
            {
                Assert.Fail();
            }
            else
            {
                Console.WriteLine("OK");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 3");
            value = UniValue.ParseXml("<response><item id='1' style='bold' color='white' /><item id='2' style='italic' color='red'>123</item></response>");

            Console.WriteLine("[]");
            Console.WriteLine(value);
            Console.WriteLine();

            Console.WriteLine("[response]");
            Console.WriteLine(value["response"]);
            Console.WriteLine();

            Console.WriteLine("[response][item]");
            Console.WriteLine(value["response"]["item"]);
            Console.WriteLine();

            Console.WriteLine("[response][item][0]");
            Console.WriteLine(value["response"]["item"][0]);
            Console.WriteLine();

            Console.WriteLine("[response][item][1]");
            Console.WriteLine(value["response"]["item"][1]);
            Console.WriteLine();

            Console.WriteLine("[response][item][0][@id]");
            Console.WriteLine(value["response"]["item"][0]["@id"]);
            Console.WriteLine();

            Console.WriteLine("[response][item][0][@style]");
            Console.WriteLine(value["response"]["item"][0]["style"]);
            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 4");
            value = UniValue.ParseXml("<response><item id='1' style='bold' color='white'><vvalue abc='test' avalue='1234567'></vvalue></item><item id='2' style='italic' color='red'><v>456</v></item></response>");
            Console.WriteLine("[]");
            Console.WriteLine(value);
            Console.WriteLine();

            Console.WriteLine("[response]");
            Console.WriteLine(value["response"]);
            Console.WriteLine();

            Console.WriteLine("[response][item]");
            Console.WriteLine(value["response"]["item"]);

            if (value["response"]["item"] != "[{ \"value\": { \"vvalue\": { \"@abc\": \"test\", \"@avalue\": \"1234567\" } }, \"@id\": \"1\", \"@style\": \"bold\", \"@color\": \"white\" }, { \"value\": { \"v\": \"456\" }, \"@id\": \"2\", \"@style\": \"italic\", \"@color\": \"red\" }]")
            {
                Assert.Fail();
            }
            else
            {
                Console.WriteLine("OK");
            }

            Console.WriteLine();

            Console.WriteLine("[response][item][0]");
            Console.WriteLine(value["response"]["item"][0]);

            if (value["response"]["item"][0] != "{ \"vvalue\": { \"@abc\": \"test\", \"@avalue\": \"1234567\" } }")
            {
                Assert.Fail();
            }
            else
            {
                Console.WriteLine("OK");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 5");
            value = UniValue.ParseJson("{data: [{id: 1, name: null}, {id: 2, name: 'test2\r\n123'}, {id: 3, name: 'test3', list: [{x:1,y:'hi'}, {x:2, y:'hello'}] }] }");
            Console.WriteLine(value);
            Assert.AreEqual(value.ToString(), "{ \"data\": [{ \"id\": 1, \"name\": null }, { \"id\": 2, \"name\": \"test2\\r\\n123\" }, { \"id\": 3, \"name\": \"test3\", \"list\": [{ \"x\": 1, \"y\": \"hi\" }, { \"x\": 2, \"y\": \"hello\" }] }] }");

            value = "test\r\ntest";
            Console.WriteLine(value);
            Assert.AreEqual(value.ToString(), "test\r\ntest");
            Console.WriteLine("-------------------------------------");
        }
Ejemplo n.º 14
0
        public void Common()
        {
            var test = new Dictionary <string, UniValue>();
            var now  = DateTime.Now;

            test.Add("a", 123);
            test.Add("b", "test");
            test.Add("c", now);
            test.Add("d", 4.2);
            test.Add("e", new byte[] { 1, 2, 3 });
            test.Add("f", UniValue.Create(new Uri("http://localhost")));
            test.Add("g", UniValue.Create(new { id = 123, name = "tester", date = now, obj = new { a = 1, b = 2, c = 3, d = new int[] { 1, 2, 3 } } }));
            test.Add("h", new int[] { 1, 2, 3 });
            test.Add("i", new string[] { "a", "b", "c" });
            test.Add("j", new StringBuilder("xyz"));

            Assert.Equal("123", test["a"].ToString());
            Assert.Equal("test", test["b"].ToString());
            Assert.Equal(now, (DateTime)test["c"]);
            Assert.Equal(4.2, (double)test["d"]);
            Assert.Equal("http://localhost", test["f"]["OriginalString"]);
            Assert.Equal(123, test["g"]["id"]);
            Assert.Equal(now, test["g"]["date"]);
            Assert.Equal(1, test["g"]["obj"]["a"]);
            Assert.Equal(2, test["g"]["obj"]["b"]);
            Assert.Equal(3, test["g"]["obj"]["c"]);
            Assert.Equal(1, test["h"][0]);
            Assert.Equal(2, test["h"][1]);
            Assert.Equal(3, test["h"][2]);
            Assert.Equal("a", test["i"][0]);
            Assert.Equal("b", test["i"][1]);
            Assert.Equal("c", test["i"][2]);
            Assert.Equal("xyz", test["j"].ToString());
            Assert.True(test["j"].Equals("xyz"));
            // TODO: Assert.Equal("xyz", test["j"]);

            UniValue r2  = "world!";
            string   ttt = "Hello, " + r2;

            Assert.True(r2.IsString);
            Assert.Equal("Hello, world!", ttt);

            r2 = 123;
            Assert.Equal(123, (int)r2);
            Assert.Equal(123, Convert.ToInt32(r2));

            r2 = "ff";
            Assert.Equal(255, Convert.ToInt32(r2.ToString(), 16));

            r2 = "123";
            Assert.Equal(123, (int)r2);

            r2 = 123.123;
            Assert.Equal(123, (int)r2);

            r2 = "123.123";
            Assert.Equal(123, (int)r2);

            r2 = "123,123";
            Assert.Equal(123, (int)r2);

            r2 = "abc";

#if NET35
            Assert.Throws <FormatException>(() => (int)r2);
#elif NET40
            Assert.Throws <FormatException>(() => (int)r2);
#else
            Assert.ThrowsAny <FormatException>(() => (int)r2);
#endif

            r2 = 123;
            Assert.Equal(123, (double)r2);

            r2 = "123";
            Assert.Equal(123, (double)r2);

            r2 = "123.123";
            Assert.Equal(123.123, (double)r2);

            r2 = "123,123";
            Assert.Equal(123.123, (double)r2);

            r2 = "abc";

#if NET35
            Assert.Throws <FormatException>(() => (double)r2);
#elif NET40
            Assert.Throws <FormatException>(() => (double)r2);
#else
            Assert.ThrowsAny <FormatException>(() => (double)r2);
#endif

            r2 = "Thu Dec 04, 2014";
            Assert.Equal(new DateTime(2014, 12, 4), Convert.ToDateTime(r2));
        }