Esempio n. 1
0
        private int GetRecCount(string teableName)
        {
            string         responce;
            HttpWebRequest req;

            req        = (HttpWebRequest)WebRequest.Create($"{sourceConnectionStringBuilder.EndPoint}/{teableName}/_count");
            req.Method = "GET";
            foreach (var header in sourceConnectionStringBuilder.Headers)
            {
                req.Headers.Add(header.Key, header.Value);
            }

            using (var response = req.GetResponse() as HttpWebResponse)
            {
                var encoding = Encoding.GetEncoding(response.CharacterSet);

                using (var responseStream = response.GetResponseStream())
                    using (var reader = new System.IO.StreamReader(responseStream, encoding))
                        responce = reader.ReadToEnd();
            }

            int countRec = 0;

            int.TryParse(JsonBase.FromString(responce)["count"].ToString(), out countRec);
            return(countRec);
        }
Esempio n. 2
0
        public override string[] GetTableNames()
        {
            string         respoce;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create($"{sourceConnectionStringBuilder.EndPoint}/_alias");

            req.Method = "GET";
            foreach (var header in sourceConnectionStringBuilder.Headers)
            {
                req.Headers.Add(header.Key, header.Value);
            }

            using (var response = req.GetResponse() as HttpWebResponse)
            {
                var encoding = Encoding.GetEncoding(response.CharacterSet);

                using (var responseStream = response.GetResponseStream())
                    using (var reader = new System.IO.StreamReader(responseStream, encoding))
                        respoce = reader.ReadToEnd();
            }
            List <string> Names = new List <string>();
            object        s     = JsonBase.FromString(respoce).Keys;

            foreach (string name in JsonBase.FromString(respoce).Keys)
            {
                Names.Add(name);
            }
            return(Names.ToArray());
        }
Esempio n. 3
0
        public void ReadKeyTest(string jsonText, string key)
        {
            JsonBase obj = JsonBase.FromString(jsonText);

            Assert.IsType <JsonObject>(obj);
            Assert.Single(obj as JsonObject);
            Assert.Contains(key, (obj as JsonObject).Keys);
        }
Esempio n. 4
0
        public void ReadNumberTest(string jsonText, double number)
        {
            JsonBase obj = JsonBase.FromString(jsonText);

            Assert.IsType <JsonArray>(obj);
            Assert.Single(obj as JsonArray);
            Assert.IsType <double>((obj as JsonArray)[0]);
            Assert.Equal(number, (double)(obj as JsonArray)[0], 1);
        }
Esempio n. 5
0
        public void TestComplex()
        {
            string jsonText = @"{
    ""glossary"": {
        ""title"": ""example glossary"",
		""GlossDiv"": {
            ""title"": ""S"",
			""GlossList"": {
                ""GlossEntry"": {
                    ""ID"": ""SGML"",
					""SortAs"": ""SGML"",
					""GlossTerm"": ""Standard Generalized Markup Language"",
					""Acronym"": ""SGML"",
					""Abbrev"": ""ISO 8879:1986"",
					""GlossDef"": {
                        ""para"": ""A meta-markup language, used to create markup languages such as DocBook."",
						""GlossSeeAlso"": [""GML"", ""XML""]
                    },
					""GlossSee"": ""markup""
                }
            }
        }
    }
}";

            JsonBase jsonBase = JsonBase.FromString(jsonText);

            Assert.IsType <JsonObject>(jsonBase);

            JsonObject obj = jsonBase as JsonObject;

            Assert.Single(obj);

            Assert.IsType <JsonObject>(obj["glossary"]);

            obj = obj["glossary"] as JsonObject;

            Assert.Equal(2, obj.Count);

            Assert.IsType <string>(obj["title"]);

            Assert.Equal("example glossary", obj["title"] as string);

            Assert.IsType <JsonObject>(obj["GlossDiv"]);
            obj = obj["GlossDiv"] as JsonObject;

            Assert.IsType <JsonObject>(obj["GlossList"]);
            obj = obj["GlossList"] as JsonObject;

            Assert.IsType <JsonObject>(obj["GlossEntry"]);
            obj = obj["GlossEntry"] as JsonObject;

            Assert.IsType <JsonObject>(obj["GlossDef"]);
            obj = obj["GlossDef"] as JsonObject;

            Assert.IsType <JsonArray>(obj["GlossSeeAlso"]);
        }
Esempio n. 6
0
        public void ReadBool(string jsonText, string key, bool value)
        {
            JsonBase obj = JsonBase.FromString(jsonText);

            Assert.IsType <JsonObject>(obj);
            Assert.Single(obj as JsonObject);
            Assert.Contains(key, (obj as JsonObject).Keys);
            Assert.IsType <bool>((obj as JsonObject)[key]);
            Assert.Equal(value, (bool)(obj as JsonObject)[key]);
        }
        private void InitConnection(bool rebuildSchema)
        {
            JsonDataSourceConnectionStringBuilder builder = new JsonDataSourceConnectionStringBuilder(ConnectionString);
            JsonBase obj      = null;
            string   jsonText = builder.Json.Trim();

            if (jsonText.Length > 0)
            {
                if (!(jsonText[0] == '{' || jsonText[0] == '['))
                {
                    using (WebClient client = new WebClient())
                    {
                        try
                        {
                            client.Encoding = Encoding.GetEncoding(builder.Encoding);
                        }
                        catch
                        {
                            client.Encoding = Encoding.UTF8;
                        }
                        jsonText = client.DownloadString(jsonText);
                    }
                }
                obj = JsonBase.FromString(jsonText) as JsonBase;
            }

            string schema = builder.JsonSchema;

            // have to update schema
            if (schema != jsonSchemaString || jsonSchema == null || String.IsNullOrEmpty(jsonSchemaString))
            {
                JsonSchema schemaObj = null;
                if (String.IsNullOrEmpty(schema) || rebuildSchema)
                {
                    if (obj != null)
                    {
                        schemaObj = JsonSchema.FromJson(obj);
                        JsonObject child = new JsonObject();
                        schemaObj.Save(child);
                        jsonSchemaString = child.ToString();
                    }
                }
                else
                {
                    schemaObj        = JsonSchema.Load(JsonBase.FromString(schema) as JsonObject);
                    jsonSchemaString = schema;
                }

                if (schemaObj == null)
                {
                    schemaObj      = new JsonSchema();
                    schemaObj.Type = "array";
                }

                if (schemaObj.Type != "array")
                {
                    JsonSchema parentSchema = new JsonSchema();
                    parentSchema.Items = schemaObj;
                    parentSchema.Type  = "array";
                    schemaObj          = parentSchema;
                }

                jsonSchema = schemaObj;
            }

            if (obj is JsonArray)
            {
                jsonInternal = obj as JsonArray;
            }
            else
            {
                JsonArray result = new JsonArray();
                if (obj != null)
                {
                    result.Add(obj);
                }
                jsonInternal = result;
            }
        }
        private void InitConnection(bool rebuildSchema)
        {
            JsonDataSourceConnectionStringBuilder builder = new JsonDataSourceConnectionStringBuilder(ConnectionString);
            JsonBase obj      = null;
            string   jsonText = builder.Json.Trim();

            if (jsonText.Length > 0)
            {
                if (!(jsonText[0] == '{' || jsonText[0] == '['))
                {
                    //using (WebClient client = new WebClient())
                    //{
                    //    try
                    //    {
                    //        client.Encoding = Encoding.GetEncoding(builder.Encoding);
                    //    }
                    //    catch
                    //    {
                    //        client.Encoding = Encoding.UTF8;
                    //    }
                    //    jsonText = client.DownloadString(jsonText);
                    //}

                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(jsonText);

                    foreach (var header in builder.Headers)
                    {
                        req.Headers.Add(header.Key, header.Value);
                    }

                    using (var response = req.GetResponse() as HttpWebResponse)
                    {
                        var encoding = Encoding.GetEncoding(response.CharacterSet);

                        using (var responseStream = response.GetResponseStream())
                            using (var reader = new System.IO.StreamReader(responseStream, encoding))
                                jsonText = reader.ReadToEnd();
                    }
                }
                obj = JsonBase.FromString(jsonText) as JsonBase;
            }

            string schema = builder.JsonSchema;

            // have to update schema
            if (schema != jsonSchemaString || jsonSchema == null || String.IsNullOrEmpty(jsonSchemaString))
            {
                JsonSchema schemaObj = null;
                if (String.IsNullOrEmpty(schema) || rebuildSchema)
                {
                    if (obj != null)
                    {
                        schemaObj = JsonSchema.FromJson(obj);
                        JsonObject child = new JsonObject();
                        schemaObj.Save(child);
                        jsonSchemaString = child.ToString();
                    }
                }
                else
                {
                    schemaObj        = JsonSchema.Load(JsonBase.FromString(schema) as JsonObject);
                    jsonSchemaString = schema;
                }

                if (schemaObj == null)
                {
                    schemaObj      = new JsonSchema();
                    schemaObj.Type = "array";
                }

                if (schemaObj.Type != "array")
                {
                    JsonSchema parentSchema = new JsonSchema();
                    parentSchema.Items = schemaObj;
                    parentSchema.Type  = "array";
                    schemaObj          = parentSchema;
                }

                jsonSchema = schemaObj;
            }

            if (obj is JsonArray)
            {
                jsonInternal = obj as JsonArray;
            }
            else
            {
                JsonArray result = new JsonArray();
                if (obj != null)
                {
                    result.Add(obj);
                }
                jsonInternal = result;
            }
        }
Esempio n. 9
0
 public void EmptyArrayTest(string jsonText)
 {
     Assert.IsType <JsonArray>(JsonBase.FromString(jsonText));
 }
Esempio n. 10
0
 public void EmptyObjectTest(string jsonText)
 {
     Assert.IsType <JsonObject>(JsonBase.FromString(jsonText));
 }
Esempio n. 11
0
 public void EmptyTextTest()
 {
     Assert.Throws <FormatException>(() => JsonBase.FromString(""));
 }