主页:
https://github.com/ServiceStack/ServiceStack.Text
The Home of .NET's fastest JSON, JSV and CSV Text Serializers.
ServiceStack.Text is an independent, dependency-free serialization library that contains all of ServiceStack's text processing functionality, including:
- JsonSerializer
- TypeSerializer (JSV-Format)
- CsvSerializer
- T.Dump extension method
- StringExtensions - Xml/Json/Csv/Url encoding, BaseConvert, Rot13, Hex escape, etc.
- Stream, Reflection, List, DateTime, etc extensions and utils
Supports custom builds for: .NET 3.5+, Mono, MonoTouch/MonoDroid, Silverlight 4/5, XBOX, Windows Phone 7
All in a single 144kb dependency-free ServiceStack.Text.dll
实验代码:
//filename: User.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleTestApp.ServiceStack.Redis.domain { public class User { public int Num { get; set; } public string Name { get; set; } public decimal Weight { get; set; } public DateTime Birthday { get; set; } public override string ToString() { return string.Format("HashCode:{0},Num:{1},Name:{2},Weight:{3},Birthday:{4}", this.GetHashCode(), Num, Name, Weight, Birthday); } } } //filename: Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using ConsoleTestApp.ServiceStack.Redis.domain; using ServiceStack.Text; namespace ConsoleTestApp.ServiceStack.Redis { class Program { static void Main(string[] args) { User u = new User(); u.Num = 110; u.Name = "张三"; u.Weight = 58.3m; u.Birthday = DateTime.Parse("1977-01-29 02:03:04.567"); Console.WriteLine("############ JSON ##################"); string uJson = u.ToJson(); Console.WriteLine("u.ToJson():" + uJson); Console.WriteLine("JsonSerializer:" + JsonSerializer.SerializeToString(u)); //与扩展方法一样 User uDeJson = uJson.FromJson<User>(); Console.WriteLine("JSON反序列化:" + uDeJson.ToString()); Console.WriteLine("############## XML ################"); string uXml = u.ToXml(); Console.WriteLine("u.ToXml():" + uXml); Console.WriteLine("XmlSerializer:" + XmlSerializer.SerializeToString(u)); //与扩展方法一样 User uDeXml = uXml.FromXml<User>(); Console.WriteLine("XML反序列化:" + uDeXml.ToString()); /* * JSV Format (i.e. JSON-like Separated Values) is a JSON inspired format that * uses CSV-style escaping for the least overhead and optimal performance. * * Fastest and most compact text-serializer for .NET (5.3x quicker than JSON, 2.6x smaller than XML) * from:http://www.servicestack.net/mythz_blog/?p=176 */ Console.WriteLine("############## JSV ################"); string uJsv = u.ToJsv(); Console.WriteLine("u.ToJsv():" + uJsv); Console.WriteLine("TypeSerializer:" + TypeSerializer.SerializeToString<User>(u)); User uDeJsv = uJsv.FromJsv<User>(); Console.WriteLine("Jsv反序列化:" + uDeJsv.ToString()); Console.WriteLine("############## CSV ################"); string uCsv = u.ToCsv(); Console.WriteLine("u.ToCsv():" + uCsv); //User uDeCsvl = uCsv.(); //Console.WriteLine("CSV反序列化:" + uDeCsvl.ToString()); Console.WriteLine("\n\n############ TypeSerializer ##################"); string uTypeString = TypeSerializer.SerializeToString<User>(u); Console.WriteLine("TypeSerializer.SerializeToString():" + uTypeString); User uDe = TypeSerializer.DeserializeFromString<User>(uTypeString); Console.WriteLine("原对象:" + u.ToString()); Console.WriteLine("反序列化对象:" + uDe.ToString()); Console.WriteLine("是否相等:" + u.Equals(uDe)); Console.ReadKey(); } } }
packages.config
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="ServiceStack.Common" version="3.9.43" targetFramework="net40-Client" /> <package id="ServiceStack.Redis" version="3.9.43" targetFramework="net40-Client" /> <package id="ServiceStack.Text" version="3.9.43" targetFramework="net40-Client" /> </packages>
PS:另外一个比较好的库:
http://json.codeplex.com/