|
| 1 | +The fastest and most compact text-based serializer for .NET - |
| 2 | +more info on its JSV Format is on the [introductory blog post](http://www.servicestack.net/mythz_blog/?p=176). |
| 3 | + |
| 4 | + |
| 5 | +# Fast new text-serialization format optimized for serializing C# POCO types |
| 6 | + |
| 7 | +Included in the Service Stack libraries is *Type Serializer*, a fast, light-weight compact Text Serializer which can be used to serialize any .NET data type including your own custom POCO's and DataContract's. |
| 8 | + |
| 9 | +Out of the box .NET provides a fairly quick but verbose Xml DataContractSerializer or a slightly more compact but slower JsonDataContractSerializer. |
| 10 | +Both of these options are fragile and likely to break with any significant schema changes. |
| 11 | +TypeSerializer addresses these shortcomings by being both smaller and significantly faster than the most popular options. |
| 12 | +It's also more resilient, e.g. a strongly-typed POCO object can be deserialized back into a loosely-typed string Dictionary and vice-versa. |
| 13 | + |
| 14 | +With that in mind, TypeSerializer's main features are: |
| 15 | + * Fastest and most compact text-serializer for .NET |
| 16 | + * Human readable and writeable, self-describing text format |
| 17 | + * Non-invasive and configuration-free |
| 18 | + * Resilient to schema changes |
| 19 | + * Serializes / De-serializes any .NET data type (by convention) |
| 20 | + * Supports custom, compact serialization of structs by overriding `ToString()` and `static T Parse(string)` methods |
| 21 | + * Can serialize inherited, interface or 'late-bound objects' data types |
| 22 | + * Respects opt-in DataMember custom serialization for DataContract dto types. |
| 23 | + |
| 24 | +These characteristics make it ideal for use anywhere you need to store or transport .NET data-types, e.g. for text blobs in a ORM, data in and out of a key-value store or as the text-protocol in .NET to .NET web services. |
| 25 | + |
| 26 | +As such, it's utilized within ServiceStack's other components: |
| 27 | + * OrmLite - to store complex types on table models as text blobs in a database field and |
| 28 | + * [ServiceStack.Redis](https://github.com/mythz/ServiceStack.Redis) - to store rich POCO data types into the very fast [redis](http://code.google.com/p/redis) instances. |
| 29 | + |
| 30 | +# Download |
| 31 | +`TypeSerializer` is included with [ServiceStack.zip](https://github.com/downloads/mythz/ServiceStack/ServiceStack.zip) |
| 32 | +or available to download separately in a standalone [ServiceStack.Text.zip](https://github.com/downloads/mythz/ServiceStack.Text/ServiceStack.Text.zip). |
| 33 | + |
| 34 | +# Simple API |
| 35 | + |
| 36 | +Like most of the interfaces in Service Stack, the API is simple and descriptive. In most cases these are the only methods that you would commonly use: |
| 37 | + |
| 38 | + string TypeSerializer.SerializeToString<T>(T value); |
| 39 | + void TypeSerializer.SerializeToWriter<T>(T value, TextWriter writer); |
| 40 | + |
| 41 | + T TypeSerializer.DeserializeFromString<T>(string value); |
| 42 | + T TypeSerializer.DeserializeFromReader<T>(TextReader reader); |
| 43 | + |
| 44 | +Where *T* can be any .NET POCO type. That’s all there is - the API was intentionally left simple :) |
| 45 | + |
| 46 | +You may also be interested in the very useful [T.Dump() extension method](http://www.servicestack.net/mythz_blog/?p=202) for recursively viewing the contents of any C# POCO Type. |
| 47 | + |
| 48 | +---- |
| 49 | + |
| 50 | +# Performance |
| 51 | +Type Serializer is actually the fastest and most compact *text serializer* available for .NET. |
| 52 | +Out of all the serializers benchmarked, it is the only one to remain competitive with [http://code.google.com/p/protobuf-net/ protobuf-net]'s very fast implementation of [http://code.google.com/apis/protocolbuffers/ Protocol Buffers] - google's high-speed binary protocol. |
| 53 | + |
| 54 | +Below is a series of benchmarks serialize the different tables in the [http://code.google.com/p/servicestack/source/browse/trunk/Common/Northwind.Benchmarks/Northwind.Common/DataModel/NorthwindData.cs Northwind database] (3202 records) with the most popular serializers available for .NET: |
| 55 | + |
| 56 | +### Combined results for serializing / deserialzing a single row of each table in the Northwind database 1,000,000 times |
| 57 | +_[view the detailed benchmarks](http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.1000000-times.2010-02-06.html)_ |
| 58 | + |
| 59 | +|| Serializer || Size || Peformance || |
| 60 | +|| Microsoft DataContractSerializer || 4.68x || 6.72x || |
| 61 | +|| Microsoft JsonDataContractSerializer|| 2.24x || 10.18x || |
| 62 | +|| Microsoft BinaryFormatter || 5.62x || 9.06x || |
| 63 | +|| NewtonSoft.Json || 2.30x || 8.15x || |
| 64 | +|| ProtoBuf.net || 1x || 1x || |
| 65 | +|| ServiceStack TypeSerializer || 1.78x || 1.92x || |
| 66 | + |
| 67 | +_number of times larger in size and slower in performance than the best - lower is better_ |
| 68 | + |
| 69 | +Microsoft's JavaScriptSerializer was also benchmarked but excluded as it was up to 280x times slower - basically don't use it, ever. |
| 70 | + |
| 71 | + |
| 72 | +# JSV Text Format (JSON + CSV) |
| 73 | + |
| 74 | +Type Serializer uses a hybrid CSV-style escaping + JavaScript-like text-based format that is optimized for both size and speed. I'm naming this JSV-format (i.e. JSON + CSV) |
| 75 | + |
| 76 | +In many ways it is similar to JavaScript, e.g. any List, Array, Collection of ints, longs, etc are stored in exactly the same way, i.e: |
| 77 | + [1,2,3,4,5] |
| 78 | + |
| 79 | +Any IDictionary is serialized like JavaScript, i.e: |
| 80 | + {A:1,B:2,C:3,D:4} |
| 81 | + |
| 82 | +Which also happens to be the same as C# POCO class with the values |
| 83 | + |
| 84 | +`new MyClass { A=1, B=2, C=3, D=4 }` |
| 85 | + |
| 86 | + {A:1,B:2,C:3,D:4} |
| 87 | + |
| 88 | +JSV is *white-space significant*, which means normal string values can be serialized without quotes, e.g: |
| 89 | + |
| 90 | +`new MyClass { Foo="Bar", Greet="Hello World!"}` is serialized as: |
| 91 | + |
| 92 | + {Foo:Bar,Greet:Hello World!} |
| 93 | + |
| 94 | + |
| 95 | +### CSV escaping |
| 96 | + |
| 97 | +Any string with any of the following characters: `[]{},"` |
| 98 | +is escaped using CSV-style escaping where the value is wrapped in double quotes, e.g: |
| 99 | + |
| 100 | +`new MyClass { Name = "Me, Junior" }` is serialized as: |
| 101 | + |
| 102 | + {Name:"Me, Junior"} |
| 103 | + |
| 104 | +A value with a double-quote is escaped with another double quote e.g: |
| 105 | + |
| 106 | +`new MyClass { Size = "2\" x 1\"" }` is serialized as: |
| 107 | + |
| 108 | + {Size:"2"" x 1"""} |
| 109 | + |
| 110 | + |
| 111 | +## Rich support for resilience and schema versioning |
| 112 | +To better illustrate the resilience of `TypeSerializer` and the JSV Format check out a real world example of it when it's used to [Painlessly migrate between old and new types in Redis](http://code.google.com/p/servicestack/wiki/MigrationsUsingSchemalessNoSql). |
| 113 | + |
| 114 | +Support for dynamic payloads and late-bound objects is explained in the post [Versatility of JSV Late-bound objects](http://www.servicestack.net/mythz_blog/?p=314). |
0 commit comments