Text:将数据集序列化为json
我在使用ServiceStack.Text(来自Nuget.org)将数据集序列化为json时遇到问题。我正在使用最新的稳定版本4.0.50和VS 2015。我一直在 进程因StackOverflowException而终止 我的代码:Text:将数据集序列化为json,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我在使用ServiceStack.Text(来自Nuget.org)将数据集序列化为json时遇到问题。我正在使用最新的稳定版本4.0.50和VS 2015。我一直在 进程因StackOverflowException而终止 我的代码: using System; using System.Data; using System.Data.SqlClient; using ServiceStack.Text; namespace TestServiceStackText { class
using System;
using System.Data;
using System.Data.SqlClient;
using ServiceStack.Text;
namespace TestServiceStackText
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = @"Server=MyServer; Database=NORTHWND; User Id=SomeUser; Password=SomePassword;";
string SqlQuery = @"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]";
// Create new dataset instance
DataSet dataset = new DataSet();
// Fill it with a little data: 1 table, 1 row
using (var conn = new SqlConnection())
{
using (var da = new SqlDataAdapter())
{
using (da.SelectCommand = conn.CreateCommand())
{
da.SelectCommand.CommandText = SqlQuery;
da.SelectCommand.Connection.ConnectionString = ConnectionString;
da.Fill(dataset);
}
}
}
// Serialize to json: exception occurs here
string json = TypeSerializer.SerializeToString<DataSet>(dataset);
Console.WriteLine("Dataset serialized to json:\n {0}", json);
// Deserialize to DataSet
DataSet ds = TypeSerializer.DeserializeFromString<DataSet>(json);
Console.WriteLine("Name: {0}, Nr. of Tables: {1}", ds.DataSetName, ds.Tables.Count);
}
}
}
使用系统;
使用系统数据;
使用System.Data.SqlClient;
使用ServiceStack.Text;
命名空间TestServiceStackText
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串连接字符串=@“Server=MyServer;Database=NORTHWND;User Id=SomeUser;Password=SomePassword;”;
字符串SqlQuery=@“从[NORTHWND].[dbo].[Customers]中选择前1位*”;
//创建新的数据集实例
数据集=新数据集();
//用少量数据填充它:1个表,1行
使用(var conn=new SqlConnection())
{
使用(var da=new SqlDataAdapter())
{
使用(da.SelectCommand=conn.CreateCommand())
{
da.SelectCommand.CommandText=SqlQuery;
da.SelectCommand.Connection.ConnectionString=ConnectionString;
da.Fill(数据集);
}
}
}
//序列化为json:此处发生异常
string json=TypeSerializer.SerializeToString(数据集);
WriteLine(“数据集序列化为json:\n{0}”,json);
//反序列化到数据集
DataSet ds=TypeSerializer.DeserializeFromString(json);
WriteLine(“名称:{0},表编号:{1}”,ds.DataSetName,ds.Tables.Count);
}
}
}
任何人的建议?ServiceStack的文本序列化程序都不明确支持DataSet,因为DataSet是一种可怕的序列化类型 Micro ORM的类似功能最终会更干净、更易于使用,例如,上述查询的等效代码为:
var customers = Db.Select<Customer>(q => q.Take(1));
var json = customers.ToJson();
var dto = json.FromJson<Customer>();
我怀疑它们是
数据集中的循环依赖项,servicestack将继续尝试序列化所有属性,即使它以前已经序列化了这些属性。以上所有内容都很有意义,但我选择DataSet作为DTO,因为我需要它完全通用。在我的项目中,查询每次都会发生变化,因此DTO必须处理该查询,并且在需要反序列化它的WCF客户端的接收端是已知类型。然而,我注意到序列化/反序列化数据集是一项昂贵的操作。我目前正在使用Newtonsoft.Json,我正在寻找一个比目前更好的DTO和/或更快的序列化程序组件。@ErikB这也是动态API允许您做的。注意:数据集已被弃用,没有将其添加到CoreCLR的计划。它们是一个沉重、复杂的数据结构,对于序列化来说是一个糟糕的选择。我建议转移到clean POCO或通用集合,这两个集合都更具互操作性,没有DEP,并且在将来会给您带来更少的问题。
var list = Db.Select<List<object>>(
"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");
var dict = Db.Select<Dictionary<string,object>>(
"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");
var csv = customers.ToCsv();
var csv = dict.ToCsv();