Skip to content

Commit 5444eb2

Browse files
vkuttypCopilot
andcommitted
Add SqlDataTable.ToJson() - serialize rows as JSON array
Each row becomes a JSON object with column names as keys and native typed values (bool, int, decimal, string, null). Arabic/Unicode text rendered directly using UnsafeRelaxedJsonEscaping. Optional indented parameter (default: true). Bump to v1.2.5. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 21a03f0 commit 5444eb2

6 files changed

Lines changed: 44 additions & 5 deletions

File tree

src/SqlDotnetty.Core/SqlDataTable.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Reflection;
22
using System.Text;
3+
using System.Text.Json;
4+
using System.Text.Json.Nodes;
35

46
namespace CosmoSQLClient.Core;
57

@@ -86,6 +88,43 @@ private static void SetProperty(object obj, PropertyInfo prop, SqlValue value)
8688
if (v is not null) prop.SetValue(obj, v);
8789
}
8890

91+
/// <summary>Render as a JSON array of objects, one per row.</summary>
92+
public string ToJson(bool indented = true)
93+
{
94+
var array = new JsonArray();
95+
foreach (var row in Rows)
96+
{
97+
var obj = new JsonObject();
98+
for (int i = 0; i < Columns.Count; i++)
99+
obj[Columns[i].Name] = ToJsonNode(row[i]);
100+
array.Add(obj);
101+
}
102+
var options = new JsonSerializerOptions
103+
{
104+
WriteIndented = indented,
105+
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
106+
};
107+
return array.ToJsonString(options);
108+
}
109+
110+
private static JsonNode? ToJsonNode(SqlValue v) => v switch
111+
{
112+
SqlValue.Null => null,
113+
SqlValue.Bool b => JsonValue.Create(b.Value),
114+
SqlValue.Int8 b => JsonValue.Create(b.Value),
115+
SqlValue.Int16 s => JsonValue.Create(s.Value),
116+
SqlValue.Int32 i => JsonValue.Create(i.Value),
117+
SqlValue.Int64 l => JsonValue.Create(l.Value),
118+
SqlValue.Float f => JsonValue.Create(f.Value),
119+
SqlValue.Double d => JsonValue.Create(d.Value),
120+
SqlValue.Decimal d => JsonValue.Create(d.Value),
121+
SqlValue.Text t => JsonValue.Create(t.Value),
122+
SqlValue.Uuid u => JsonValue.Create(u.Value.ToString()),
123+
SqlValue.Date dt => JsonValue.Create(dt.Value.ToString("O")),
124+
SqlValue.Bytes b => JsonValue.Create(Convert.ToBase64String(b.Value)),
125+
_ => null
126+
};
127+
89128
/// <summary>Render as a Markdown table string.</summary>
90129
public string ToMarkdownTable()
91130
{

src/SqlDotnetty.Core/SqlDotnetty.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Nullable>enable</Nullable>
99

1010
<PackageId>CosmoSQLClient.Core</PackageId>
11-
<Version>1.2.4</Version>
11+
<Version>1.2.5</Version>
1212
<Authors>Veeran Puthumkara</Authors>
1313
<Description>Shared core types and interfaces for CosmoSQLClient — a unified .NET database driver for MSSQL, PostgreSQL, MySQL and SQLite using DotNetty.</Description>
1414
<PackageTags>sql;mssql;postgres;mysql;sqlite;database;driver;dotnetty;nio</PackageTags>

src/SqlDotnetty.MsSql/SqlDotnetty.MsSql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Nullable>enable</Nullable>
2121

2222
<PackageId>CosmoSQLClient.MsSql</PackageId>
23-
<Version>1.2.4</Version>
23+
<Version>1.2.5</Version>
2424
<Authors>Veeran Puthumkara</Authors>
2525
<Description>Microsoft SQL Server driver for CosmoSQLClient — TDS 7.4 wire protocol from scratch using DotNetty. Supports TLS, Windows/SQL auth, stored procedures and connection pooling.</Description>
2626
<PackageTags>sql;mssql;sqlserver;tds;database;driver;dotnetty;nio</PackageTags>

src/SqlDotnetty.MySql/SqlDotnetty.MySql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Nullable>enable</Nullable>
2121

2222
<PackageId>CosmoSQLClient.MySql</PackageId>
23-
<Version>1.2.4</Version>
23+
<Version>1.2.5</Version>
2424
<Authors>Veeran Puthumkara</Authors>
2525
<Description>MySQL driver for CosmoSQLClient — MySQL v10 wire protocol using DotNetty. Supports mysql_native_password, caching_sha2_password auth and connection pooling.</Description>
2626
<PackageTags>sql;mysql;mariadb;database;driver;dotnetty;nio</PackageTags>

src/SqlDotnetty.Postgres/SqlDotnetty.Postgres.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Nullable>enable</Nullable>
2121

2222
<PackageId>CosmoSQLClient.Postgres</PackageId>
23-
<Version>1.2.4</Version>
23+
<Version>1.2.5</Version>
2424
<Authors>Veeran Puthumkara</Authors>
2525
<Description>PostgreSQL driver for CosmoSQLClient — PostgreSQL v3 wire protocol using DotNetty. Supports SCRAM-SHA-256 auth, TLS and connection pooling.</Description>
2626
<PackageTags>sql;postgres;postgresql;database;driver;dotnetty;nio</PackageTags>

src/SqlDotnetty.Sqlite/SqlDotnetty.Sqlite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<Nullable>enable</Nullable>
1717

1818
<PackageId>CosmoSQLClient.Sqlite</PackageId>
19-
<Version>1.2.4</Version>
19+
<Version>1.2.5</Version>
2020
<Authors>Veeran Puthumkara</Authors>
2121
<Description>SQLite driver for CosmoSQLClient — wraps Microsoft.Data.Sqlite behind the unified ISqlDatabase interface with connection pooling support.</Description>
2222
<PackageTags>sql;sqlite;database;driver;dotnetty;nio</PackageTags>

0 commit comments

Comments
 (0)