简体   繁体   English

ServiceStack.Text:将DataSet序列化为json

[英]ServiceStack.Text: serializing DataSet to json

I am having a trouble serializing a dataset to json using ServiceStack.Text (from Nuget.org). 我在使用ServiceStack.Text(来自Nuget.org)将数据集序列化为json时遇到麻烦。 I am using the latest stable build 4.0.50 and VS 2015 . 我正在使用最新的稳定版本4.0.50VS 2015 I keep getting 我不断

Process is terminated due to StackOverflowException 进程由于StackOverflowException而终止

My code: 我的代码:

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);
        }
    }
}

Suggestions anybody? 建议有人吗?

None of ServiceStack's text serializers have explicit support for DataSet's which are a horrible type for serialization. ServiceStack的文本序列化程序都没有对DataSet的显式支持,而DataSet是可怕的序列化类型。

Micro ORM's like OrmLite ends up being much cleaner and easier to use which maps to clean POCO's that are ideal for serialization , eg the equivalent code for your above query is: OrmLite之类的Micro ORM最终变得更加整洁和易于使用,这些映射可以清理非常适合序列化的POCO ,例如,上述查询的等效代码是:

var customers = Db.Select<Customer>(q => q.Take(1));

var json = customers.ToJson();
var dto = json.FromJson<Customer>();

Or if you don't want to create a Customer type you can use OrmLite's dynamic API's to map to a lose typed List of objects: 或者,如果您不想创建客户类型,则可以使用OrmLite的动态API映射到丢失类型的对象列表:

var list = Db.Select<List<object>>(
   "SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");

Or a Dictionary: 或字典:

var dict = Db.Select<Dictionary<string,object>>(
   "SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");

Checkout the northwind.servicestack.net project for code examples of using OrmLite and AutoQuery to query the Northwind dataset. 检出northwind.servicestack.net项目,以获取使用OrmLite和AutoQuery查询Northwind数据集的代码示例。

In summary, you'll have a lot less issues if you use clean POCOs instead of DataSets which are supported in every serializer, are much smaller and faster than DataSet's, you can also serialize POCO's or loose-typed Dictionary's to CSV with ServiceStack.Text: 总而言之,如果您使用干净的POCO代替每个序列化程序支持的数据集,并且数据集的大小和速度比DataSet的小得多和快得多,那么您将遇到的问题会少很多,还可以使用ServiceStack.Text将POCO或松散类型的Dictionary序列化为CSV。 :

var csv = customers.ToCsv();
var csv = dict.ToCsv();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM