} // GetAssemblyQualifiedNoVersionName

        private static async System.Threading.Tasks.Task WriteAssociativeColumnsArray(
            Newtonsoft.Json.JsonTextWriter jsonWriter
            , System.Data.Common.DbDataReader dr, RenderType_t renderType)
        {
            //await jsonWriter.WriteStartObjectAsync();
            await jsonWriter.WriteStartObjectAsync();

            for (int i = 0; i <= dr.FieldCount - 1; i++)
            {
                await jsonWriter.WritePropertyNameAsync(dr.GetName(i));

                await jsonWriter.WriteStartObjectAsync();

                await jsonWriter.WritePropertyNameAsync("index");

                await jsonWriter.WriteValueAsync(i);

                if (renderType.HasFlag(RenderType_t.WithDetail))
                {
                    await jsonWriter.WritePropertyNameAsync("fieldType");

                    // await jsonWriter.WriteValueAsync(GetAssemblyQualifiedNoVersionName(dr.GetFieldType(i)));
                    await jsonWriter.WriteValueAsync(GetTypeName(dr.GetFieldType(i), renderType));
                }

                await jsonWriter.WriteEndObjectAsync();
            } // Next i

            await jsonWriter.WriteEndObjectAsync();
        } // WriteAssociativeArray
Exemple #2
0
        } // End Sub TestCompleteness

        public static async System.Threading.Tasks.Task TestJsonGeneration2()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            using (System.IO.StringWriter sw = new System.IO.StringWriter(sb))
            {
                using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(sw))
                {
                    jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;

                    // await jsonWriter.WriteStartObjectAsync();


                    jsonWriter.WriteRaw("{");



                    jsonWriter.WriteRaw(System.Environment.NewLine);

                    await jsonWriter.WritePropertyNameAsync("key");

                    await jsonWriter.WriteValueAsync("value");


                    await jsonWriter.WritePropertyNameAsync("key 2");

                    await jsonWriter.WriteValueAsync("value 2");

                    jsonWriter.WriteRaw(System.Environment.NewLine);
                    jsonWriter.WriteRaw("}");
                    jsonWriter.WriteRaw(System.Environment.NewLine);



                    jsonWriter.WriteRaw(",bar:{");
                    jsonWriter.WriteRaw(System.Environment.NewLine);

                    await jsonWriter.WritePropertyNameAsync("key");

                    await jsonWriter.WriteValueAsync("value");


                    await jsonWriter.WritePropertyNameAsync("key 2");

                    await jsonWriter.WriteValueAsync("value 2");

                    jsonWriter.WriteRaw(System.Environment.NewLine);
                    jsonWriter.WriteRaw("}");
                    jsonWriter.WriteRaw(System.Environment.NewLine);

                    // await jsonWriter.WriteEndObjectAsync();
                } // End Using jsonWriter
            }     // End Using sw

            string txt = sb.ToString();

            System.Console.WriteLine(txt);
        } // End Sub TestJsonGeneration2
        public static async Task WriteResultsToStream(object results, Stream stream, CancellationToken cancel)
        {
            if (results == null)
            {
                return;
            }

            if (results is AdomdDataReader rdr)
            {
                var encoding = new System.Text.UTF8Encoding(false);
                using (var tw = new StreamWriter(stream, encoding, 1024 * 4, true))
                    using (var w = new Newtonsoft.Json.JsonTextWriter(tw))
                    {
                        await w.WriteStartObjectAsync(cancel);

                        var rn = "rows";

                        await w.WritePropertyNameAsync(rn);

                        await w.WriteStartArrayAsync(cancel);

                        while (rdr.Read())
                        {
                            await w.WriteStartObjectAsync(cancel);

                            for (int i = 0; i < rdr.FieldCount; i++)
                            {
                                string name  = rdr.GetName(i);
                                object value = rdr.GetValue(i);

                                await w.WritePropertyNameAsync(name, cancel);

                                await w.WriteValueAsync(value, cancel);
                            }
                            await w.WriteEndObjectAsync(cancel);
                        }

                        await w.WriteEndArrayAsync(cancel);

                        await w.WriteEndObjectAsync(cancel);

                        await w.FlushAsync();

                        await tw.FlushAsync();

                        await stream.FlushAsync();
                    }
            }
            else if (results is CellSet cs)
            {
                throw new NotSupportedException("CellSet results");
            }
            else
            {
                throw new InvalidOperationException("Unexpected result type");
            }
        }
Exemple #4
0
        } // End Sub TestJsonGeneration

        public static async System.Threading.Tasks.Task TestJsonGeneration1()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            using (System.IO.StringWriter sw = new System.IO.StringWriter(sb))
            {
                using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(sw))
                {
                    jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;

                    await jsonWriter.WriteStartObjectAsync();

                    await jsonWriter.WritePropertyNameAsync("tables");

                    await jsonWriter.WriteStartArrayAsync();

                    await jsonWriter.WriteEndArrayAsync();

                    await jsonWriter.WriteEndObjectAsync();
                } // End Using jsonWriter
            }     // End Using sw

            string txt = sb.ToString();

            System.Console.WriteLine(txt);
        } // End Sub TestJsonGeneration1
Exemple #5
0
        static async Task WriteResultsToStream(AdomdDataReader results, Stream stream, CancellationToken cancel, ILogger log)
        {
            if (results == null)
            {
                log.LogInformation("Null results");
                return;
            }

            using var rdr = results;

            //can't call Dispose on these without syncronous IO on the underlying connection
            var tw   = new StreamWriter(stream, encoding, 1024 * 4, true);
            var w    = new Newtonsoft.Json.JsonTextWriter(tw);
            int rows = 0;

            try
            {
                await w.WriteStartArrayAsync(cancel);

                while (rdr.Read())
                {
                    if (cancel.IsCancellationRequested)
                    {
                        throw new TaskCanceledException();
                    }
                    rows++;
                    await w.WriteStartObjectAsync(cancel);

                    for (int i = 0; i < rdr.FieldCount; i++)
                    {
                        string name  = rdr.GetName(i);
                        object value = rdr.GetValue(i);

                        await w.WritePropertyNameAsync(name, cancel);

                        await w.WriteValueAsync(value, cancel);
                    }
                    await w.WriteEndObjectAsync(cancel);

                    if (rows % 50000 == 0)
                    {
                        log.LogInformation($"Wrote {rows} rows to output stream.");
                    }
                }
                log.LogInformation($"Finished Writing {rows} rows to output stream.");

                await w.WriteEndArrayAsync(cancel);

                await w.FlushAsync();

                await tw.FlushAsync();

                await stream.FlushAsync();
            }
            catch (TaskCanceledException ex)
            {
                log.LogWarning($"Writing results canceled after {rows} rows.");
            }
        }
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            System.Data.Common.DbConnection cnn
            , string sql
            , RenderType_t format
            , Microsoft.AspNetCore.Http.HttpContext context
            , System.Text.Encoding encoding
            , object parameters = null
            , System.Data.IDbTransaction transaction = null
            , int?commandTimeout = null
            , System.Data.CommandType?commandType = null
            )
        {
            // DynamicParameters dbArgs = new DynamicParameters();


            using (System.IO.StreamWriter responseWriter = new System.IO.StreamWriter(context.Response.Body, encoding))
            {
                using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                           new Newtonsoft.Json.JsonTextWriter(responseWriter))
                {
                    if (format.HasFlag(RenderType_t.Indented))
                    {
                        jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                    }


                    try
                    {
                        using (System.Data.Common.DbDataReader dr = await cnn.ExecuteDbReaderAsync(sql, parameters, transaction, commandTimeout, commandType))
                        {
                            context.Response.StatusCode  = (int)System.Net.HttpStatusCode.OK;
                            context.Response.ContentType = "application/json; charset=" + encoding.WebName;

                            await jsonWriter.WriteStartObjectAsync();

                            await jsonWriter.WritePropertyNameAsync("tables");

                            await jsonWriter.WriteStartArrayAsync();

                            do
                            {
                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();

                                    await jsonWriter.WritePropertyNameAsync("columns");

                                    if (format.HasFlag(RenderType_t.Columns_Associative))
                                    {
                                        await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                    }
                                    else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                    {
                                        await WriteComplexArray(jsonWriter, dr, format);
                                    }
                                    else // (format.HasFlag(RenderType_t.Array))
                                    {
                                        await WriteArray(jsonWriter, dr);
                                    }
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))


                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WritePropertyNameAsync("rows");
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))

                                await jsonWriter.WriteStartArrayAsync();

                                string[] columns = null;
                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    columns = new string[dr.FieldCount];
                                    for (int i = 0; i < dr.FieldCount; i++)
                                    {
                                        columns[i] = dr.GetName(i);
                                    } // Next i
                                }     // End if (format.HasFlag(RenderType_t.DataTable))

                                while (await dr.ReadAsync())
                                {
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteStartObjectAsync();
                                    }
                                    else
                                    {
                                        await jsonWriter.WriteStartArrayAsync();
                                    }

                                    for (int i = 0; i <= dr.FieldCount - 1; i++)
                                    {
                                        object obj = await dr.GetFieldValueAsync <object>(i);

                                        if (obj == System.DBNull.Value)
                                        {
                                            obj = null;
                                        }

                                        if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WritePropertyNameAsync(columns[i]);
                                        }

                                        await jsonWriter.WriteValueAsync(obj);
                                    } // Next i

                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteEndObjectAsync();
                                    }
                                    else
                                    {
                                        await jsonWriter.WriteEndArrayAsync();
                                    }
                                } // Whend

                                await jsonWriter.WriteEndArrayAsync();

                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))

                                await jsonWriter.FlushAsync();

                                await responseWriter.FlushAsync();
                            } while (await dr.NextResultAsync());

                            await jsonWriter.WriteEndArrayAsync();

                            await jsonWriter.WriteEndObjectAsync();

                            await jsonWriter.FlushAsync();

                            await responseWriter.FlushAsync();
                        } // End Using dr
                    }
                    catch (System.Exception ex)
                    {
                        context.Response.StatusCode  = (int)System.Net.HttpStatusCode.InternalServerError;
                        context.Response.ContentType = "application/json; charset=" + encoding.WebName;

                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("Error");

                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("Message");

                        await jsonWriter.WriteValueAsync(ex.Message);

                        await jsonWriter.WritePropertyNameAsync("StackTrace");

                        await jsonWriter.WriteValueAsync(ex.StackTrace);

                        await jsonWriter.WriteEndObjectAsync();

                        await jsonWriter.WriteEndObjectAsync();
                    }

                    await jsonWriter.FlushAsync();

                    await responseWriter.FlushAsync();

                    await context.Response.CompleteAsync();
                } // End Using jsonWriter
            }     // End Using responseWriter
        }         // End Sub AnyDataReaderToJson
Exemple #7
0
        } // End Task DeserializeJSON

        private static async System.Threading.Tasks.Task Ransack(
            Microsoft.AspNetCore.Http.HttpContext context
            , System.Net.WebSockets.WebSocket webSocket)
        {
            Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
            SearchArguments searchArguments           = await DeserializeJSON <SearchArguments>(webSocket, serializer);


            string[] fieldNames = LinqHelper.GetFieldAndPropertyNames <SearchResult>();
            Getter_t <SearchResult>[] getters = LinqHelper.GetGetters <SearchResult>(fieldNames);


            using (WebSocketTextWriter wtw = new WebSocketTextWriter(webSocket))
            {
#if true
                using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(wtw))
                {
                    System.Threading.Tasks.Task wsa = jsonWriter.WriteStartArrayAsync();

                    // jsonWriter.WriteStartArray();

                    int j = 0;
                    foreach (SearchResult thisSearchResult in FileSearch.SearchContent2(searchArguments))
                    {
                        await wsa;


                        // serializer.Serialize(jsonWriter, thisSearchResult);


                        // jsonWriter.WriteStartObject();
                        await jsonWriter.WriteStartObjectAsync();



                        // jsonWriter.WritePropertyName("CharPos");
                        // jsonWriter.WriteValue(thisSearchResult.CharPos);

                        // jsonWriter.WritePropertyName("File");
                        // jsonWriter.WriteValue(thisSearchResult.File);

                        // jsonWriter.WritePropertyName("Line");
                        // jsonWriter.WriteValue(thisSearchResult.Line);

                        // jsonWriter.WritePropertyName("LineNumber");
                        // jsonWriter.WriteValue(thisSearchResult.LineNumber);

                        // jsonWriter.WritePropertyName("SearchTerm");
                        // jsonWriter.WriteValue(thisSearchResult.SearchTerm);

                        /*
                         *
                         * await jsonWriter.WritePropertyNameAsync("CharPos");
                         * await jsonWriter.WriteValueAsync(thisSearchResult.CharPos);
                         *
                         * await jsonWriter.WritePropertyNameAsync("CharPos");
                         * await jsonWriter.WriteValueAsync(thisSearchResult.CharPos);
                         *
                         * await jsonWriter.WritePropertyNameAsync("File");
                         * await jsonWriter.WriteValueAsync(thisSearchResult.File);
                         *
                         * await jsonWriter.WritePropertyNameAsync("Line");
                         * await jsonWriter.WriteValueAsync(thisSearchResult.Line);
                         *
                         * await jsonWriter.WritePropertyNameAsync("LineNumber");
                         * await jsonWriter.WriteValueAsync(thisSearchResult.LineNumber);
                         *
                         * await jsonWriter.WritePropertyNameAsync("SearchTerm");
                         * await jsonWriter.WriteValueAsync(thisSearchResult.SearchTerm);
                         */



                        for (int i = 0; i < getters.Length; ++i)
                        {
                            System.Threading.Tasks.Task wpnt = jsonWriter.WritePropertyNameAsync(fieldNames[i]);
                            object value = getters[i](thisSearchResult);
                            // if (value == System.DBNull.Value) value = null;

                            await wpnt;
                            await jsonWriter.WriteValueAsync(value);
                        } // Next i



                        // await awso;



                        // jsonWriter.WriteEndObject();
                        System.Threading.Tasks.Task weo = jsonWriter.WriteEndObjectAsync();
                        // await weo;


                        if (j > 0 && j % 200 == 0)
                        {
                            j++;
                            await weo;
                            await jsonWriter.WriteEndArrayAsync();

                            await jsonWriter.FlushAsync();

                            // await wtw.FlushAsync();
                            await wtw.SendAsync(true);

                            await jsonWriter.WriteStartArrayAsync();
                        } // Next j
                        else
                        {
                            j++;
                            await weo;
                        }
                    } // Next thisSearchResult

                    await jsonWriter.WriteEndArrayAsync();

                    //jsonWriter.WriteEndArray();

                    await jsonWriter.FlushAsync();

                    // jsonWriter.Flush();
                } // End Using jsonWriter
#else
                // System.Collections.Generic.List<SearchResult> ls = FileSearch.SearchContent(searchArguments);
                System.Collections.Generic.IEnumerable <SearchResult> ls = FileSearch.SearchContent2(searchArguments);

                using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(wtw))
                {
                    serializer.Serialize(jsonWriter, ls);
                    await jsonWriter.FlushAsync();
                } // End Using jsonWriter
#endif

                await wtw.SendAsync(true);
            } // End Using wtw

            await webSocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "Normal closure; the connection successfully completed whatever purpose for which it was created.", System.Threading.CancellationToken.None);

            serializer = null;
        } // End Task Ransack
Exemple #8
0
        } // End Sub WriteArray 


        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            string sql
            , System.Collections.Generic.Dictionary<string, object> pars
            , Microsoft.AspNetCore.Http.HttpContext context
            , RenderType_t format)
        {
            SqlService service = (SqlService) context.RequestServices.GetService(typeof(SqlService));

            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;
                    // cmd.CommandText = "SELECT  * FROM T_Benutzer; SELECT * FROM T_Benutzergruppen;";

                    service.AddParameterList(pars, cmd);
                    // cmd.ExecuteNonQueryAsync
                    // cmd.ExecuteReaderAsync
                    // cmd.ExecuteScalarAsync

                    using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync(
                        System.Data.CommandBehavior.SequentialAccess
                        | System.Data.CommandBehavior.CloseConnection))
                    {
                        using (System.IO.StreamWriter output = new System.IO.StreamWriter(context.Response.Body))
                        {
                            using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                                new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                            {
                                if (format.HasFlag(RenderType_t.Indented))
                                    jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;


                                context.Response.StatusCode = (int) System.Net.HttpStatusCode.OK;
                                context.Response.ContentType = "application/json";


                                await jsonWriter.WriteStartObjectAsync();

                                await jsonWriter.WritePropertyNameAsync("tables");
                                await jsonWriter.WriteStartArrayAsync();

                                do
                                {
                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteStartObjectAsync();
                                        await jsonWriter.WritePropertyNameAsync("columns");

                                        if (format.HasFlag(RenderType_t.Columns_Associative))
                                        {
                                            await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                        }
                                        else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                        {
                                            await WriteComplexArray(jsonWriter, dr, format);
                                        }
                                        else // (format.HasFlag(RenderType_t.Array))
                                        {
                                            await WriteArray(jsonWriter, dr);
                                        }
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only)) 


                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WritePropertyNameAsync("rows");
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only))
                                    
                                    await jsonWriter.WriteStartArrayAsync();
                                    
                                    string[] columns = null;
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        columns = new string[dr.FieldCount];
                                        for (int i = 0; i < dr.FieldCount; i++)
                                        {
                                            columns[i] = dr.GetName(i);
                                        } // Next i 
                                    } // End if (format.HasFlag(RenderType_t.DataTable)) 

                                    while (await dr.ReadAsync())
                                    {
                                        if (format.HasFlag(RenderType_t.DataTable))
                                            await jsonWriter.WriteStartObjectAsync();
                                        else
                                            await jsonWriter.WriteStartArrayAsync();

                                        for (int i = 0; i <= dr.FieldCount - 1; i++)
                                        {
                                            object obj = await dr.GetFieldValueAsync<object>(i);
                                            if (obj == System.DBNull.Value)
                                                obj = null;

                                            if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                            {
                                                await jsonWriter.WritePropertyNameAsync(columns[i]);
                                            }

                                            await jsonWriter.WriteValueAsync(obj);
                                        } // Next i 

                                        if (format.HasFlag(RenderType_t.DataTable))
                                            await jsonWriter.WriteEndObjectAsync();
                                        else
                                            await jsonWriter.WriteEndArrayAsync();
                                    } // Whend 

                                    await jsonWriter.WriteEndArrayAsync();

                                    if (!format.HasFlag(RenderType_t.Data_Only) &&
                                        !format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WriteEndObjectAsync();
                                    } // End if (!format.HasFlag(RenderType_t.Data_Only)) 
                                } while (await dr.NextResultAsync());

                                await jsonWriter.WriteEndArrayAsync();
                                await jsonWriter.WriteEndObjectAsync();

                                await jsonWriter.FlushAsync();
                                await output.FlushAsync();
                            } // jsonWriter 
                        } // output 
                    } // dr 
                } // End Using cmd 

                if (con.State != System.Data.ConnectionState.Closed)
                    con.Close();
            } // con 
        } // End Sub WriteArray 
Exemple #9
0
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task <System.Exception> AsJSON(
            this IDbConnection cnn
            , System.IO.TextWriter output
            , string sql
            , RenderType_t format
            , object param = null
            , IDbTransaction transaction = null
            , int?commandTimeout         = null
            , CommandType?commandType    = null)
        {
            try
            {
                using (System.Data.Common.DbDataReader dr = cnn.ExecuteDbReader(sql, param, transaction, commandTimeout, commandType))
                {
                    using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                               new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                    {
                        if (format.HasFlag(RenderType_t.Indented))
                        {
                            jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                        }


                        // https://stackoverflow.com/questions/15305203/what-to-do-with-errors-when-streaming-the-body-of-an-http-request
                        // https://tools.ietf.org/html/rfc2616#section-14.40
                        // https://stackoverflow.com/questions/50299300/how-to-write-malformed-http-response-to-guarantee-something-akin-to-http-500
                        // https://stackoverflow.com/questions/50298999/write-http-trailer-headers-manually
                        // How do I write a useful trail header to the response that can be displayed well by the browser?
                        // You don’t. Mainstream Web browsers do not care about trailers.
                        // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                        // context.Response.ContentType = "application/json";


                        await jsonWriter.WriteStartObjectAsync();

                        await jsonWriter.WritePropertyNameAsync("tables");

                        await jsonWriter.WriteStartArrayAsync();

                        do
                        {
                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteStartObjectAsync();

                                await jsonWriter.WritePropertyNameAsync("columns");

                                if (format.HasFlag(RenderType_t.Columns_Associative))
                                {
                                    await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                }
                                else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                {
                                    await WriteComplexArray(jsonWriter, dr, format);
                                }
                                else // (format.HasFlag(RenderType_t.Array))
                                {
                                    await WriteArray(jsonWriter, dr);
                                }
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))


                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WritePropertyNameAsync("rows");
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))

                            await jsonWriter.WriteStartArrayAsync();

                            string[] columns = null;
                            if (format.HasFlag(RenderType_t.DataTable))
                            {
                                columns = new string[dr.FieldCount];
                                for (int i = 0; i < dr.FieldCount; i++)
                                {
                                    columns[i] = dr.GetName(i);
                                } // Next i
                            }     // End if (format.HasFlag(RenderType_t.DataTable))

                            while (await dr.ReadAsync())
                            {
                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteStartArrayAsync();
                                }

                                for (int i = 0; i <= dr.FieldCount - 1; i++)
                                {
                                    object obj = await dr.GetFieldValueAsync <object>(i);

                                    if (obj == System.DBNull.Value)
                                    {
                                        obj = null;
                                    }

                                    if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                    {
                                        await jsonWriter.WritePropertyNameAsync(columns[i]);
                                    }

                                    await jsonWriter.WriteValueAsync(obj);
                                } // Next i

                                if (format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                }
                                else
                                {
                                    await jsonWriter.WriteEndArrayAsync();
                                }
                            } // Whend

                            await jsonWriter.WriteEndArrayAsync();

                            if (!format.HasFlag(RenderType_t.Data_Only) &&
                                !format.HasFlag(RenderType_t.DataTable))
                            {
                                await jsonWriter.WriteEndObjectAsync();
                            } // End if (!format.HasFlag(RenderType_t.Data_Only))
                        } while (await dr.NextResultAsync());

                        await jsonWriter.WriteEndArrayAsync();

                        await jsonWriter.WriteEndObjectAsync();

                        await jsonWriter.FlushAsync();

                        await output.FlushAsync();
                    } // jsonWriter
                }     // End Using dr
            }         // End Try
            catch (System.Exception ex)
            {
                return(ex);
            } // End Catch

            return(null);
        } // End Task AsJSON
        } // End Sub WriteArray

        public static async System.Threading.Tasks.Task AnyDataReaderToJson(
            string sql
            , System.Collections.Generic.Dictionary <string, object> pars
            , SqlService service
            , WebSocketTextWriter output
            , RenderType_t format)
        {
            using (System.Data.Common.DbConnection con = service.Connection)
            {
                using (System.Data.Common.DbCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = sql;

                    service.AddParameterList(pars, cmd);
                    // cmd.ExecuteNonQueryAsync
                    // cmd.ExecuteReaderAsync
                    // cmd.ExecuteScalarAsync

                    using (System.Data.Common.DbDataReader dr = await cmd.ExecuteReaderAsync(
                               System.Data.CommandBehavior.SequentialAccess
                               | System.Data.CommandBehavior.CloseConnection))
                    {
                        using (Newtonsoft.Json.JsonTextWriter jsonWriter =
                                   new Newtonsoft.Json.JsonTextWriter(output)) // context.Response.Output)
                        {
                            if (format.HasFlag(RenderType_t.Indented))
                            {
                                jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
                            }


                            // context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
                            // context.Response.ContentType = "application/json";


                            await jsonWriter.WriteStartObjectAsync();

                            await jsonWriter.WritePropertyNameAsync("tables");

                            await jsonWriter.WriteStartArrayAsync();

                            do
                            {
                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteStartObjectAsync();

                                    await jsonWriter.WritePropertyNameAsync("columns");

                                    if (format.HasFlag(RenderType_t.Columns_Associative))
                                    {
                                        await WriteAssociativeColumnsArray(jsonWriter, dr, format);
                                    }
                                    else if (format.HasFlag(RenderType_t.Columns_ObjectArray))
                                    {
                                        await WriteComplexArray(jsonWriter, dr, format);
                                    }
                                    else // (format.HasFlag(RenderType_t.Array))
                                    {
                                        await WriteArray(jsonWriter, dr);
                                    }
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))



                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WritePropertyNameAsync("rows");
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))


                                await output.TransmitAsync();

                                await jsonWriter.WriteStartArrayAsync();

                                if (dr.HasRows)
                                {
                                    string[] columns = null;
                                    if (format.HasFlag(RenderType_t.DataTable))
                                    {
                                        columns = new string[dr.FieldCount];
                                        for (int i = 0; i < dr.FieldCount; i++)
                                        {
                                            columns[i] = dr.GetName(i);
                                        } // Next i
                                    }     // End if (format.HasFlag(RenderType_t.DataTable))

                                    int rowCount = 0;

                                    while (await dr.ReadAsync())
                                    {
                                        if (format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WriteStartObjectAsync();
                                        }
                                        else
                                        {
                                            await jsonWriter.WriteStartArrayAsync();
                                        }

                                        for (int i = 0; i <= dr.FieldCount - 1; i++)
                                        {
                                            object obj = await dr.GetFieldValueAsync <object>(i);

                                            if (obj == System.DBNull.Value)
                                            {
                                                obj = null;
                                            }

                                            if (columns != null && format.HasFlag(RenderType_t.DataTable))
                                            {
                                                await jsonWriter.WritePropertyNameAsync(columns[i]);
                                            }

                                            await jsonWriter.WriteValueAsync(obj);
                                        } // Next i

                                        if (format.HasFlag(RenderType_t.DataTable))
                                        {
                                            await jsonWriter.WriteEndObjectAsync();
                                        }
                                        else
                                        {
                                            await jsonWriter.WriteEndArrayAsync();
                                        }

                                        rowCount++;

                                        await jsonWriter.FlushAsync();

                                        if (rowCount % 5 == 0)
                                        {
                                            await output.TransmitAsync();
                                        }
                                        else
                                        {
                                            await output.FlushAsync();
                                        }
                                    } // Whend

                                    await jsonWriter.FlushAsync();

                                    await output.TransmitAsync();
                                } // End if (dr.HasRows)

                                await jsonWriter.WriteEndArrayAsync();

                                if (!format.HasFlag(RenderType_t.Data_Only) &&
                                    !format.HasFlag(RenderType_t.DataTable))
                                {
                                    await jsonWriter.WriteEndObjectAsync();
                                } // End if (!format.HasFlag(RenderType_t.Data_Only))
                            } while (await dr.NextResultAsync());

                            await jsonWriter.WriteEndArrayAsync();

                            await jsonWriter.WriteEndObjectAsync();

                            try
                            {
                                await jsonWriter.FlushAsync();

                                await output.TransmitAsync();
                            }
                            catch (System.Exception ex)
                            {
                                System.Console.WriteLine(ex.Message);
                            }
                        } // jsonWriter
                    }     // dr
                }         // End Using cmd

                if (con.State != System.Data.ConnectionState.Closed)
                {
                    con.Close();
                }
            } // con
        }     // End Sub WriteArray