Most Windows 8 Store apps spend a significant time in the saving and retrieving of local data. The local file system is often used as main storage. But even the apps that come with server side storage, often need to use local storage: to host a cache for when there’s no network available or whilst the server data is still downloading. If you don’t use a third-party local database (like SQLite) then you have to manage the persistence (i.e. the serialization and deserialization of your objects) yourself. This article introduces you to the 4 main serialization engines that are available to Windows 8 Store apps: the 3 native ones (XmlSerializer, DataContractSerializer, and JsonSerializer), together with one popular serializer that’s not in the framework: the Json.NET serializer.
I built a little benchmarking app to compare these serializers. It measures the duration and validates the result of a workflow that consists of serializing, locally saving, and deserializing a list of business objects. The app lets you select the number of objects to be processed, the location where the file should be saved (Local Folder or Roaming Folder), and whether or not to compare the deserialized objects with the original ones.
Here’s how the app looks like:
The Item class represents the business object. It has properties of different types: strings, datetimes, an enumeration, a link to another business object (called SubItem), and a calculated property that we don’t want to serialize. My intention is to use this app for testing real app models, by replacing this Item class with the appropriate model class(es). An extension class contains methods to generate test data for the instances, and to compare the content of two instances (overriding Equals might be an alternative).
We’re testing 4 candidate serialization engines here, but maybe I’m missing some. So to be able to extend the list of engines in the benchmark, I hooked them into a framework. The app talks to the serializers through an abstract base class:
Abstract Serialization Base
- /// <summary>
- /// Abstract base class for serializers.
- /// </summary>
- /// <typeparam name="T">The type of the instance.</typeparam>
- public abstract class AbstractSerializationBase<T> where T : class
- {
- /// <summary>
- /// Serializes the specified instance.
- /// </summary>
- /// <param name="instance">The instance.</param>
- /// <returns>The size of the serialized instance, in KB.</returns>
- public abstract Task<int> Serialize(T instance);
-
- /// <summary>
- /// Deserializes the instance.
- /// </summary>
- /// <returns>The instance.</returns>
- public abstract Task<T> Deserialize();
-
- /// <summary>
- /// Gets or sets the name of the file.
- /// </summary>
- public string FileName { get; set; }
-
- /// <summary>
- /// Gets or sets the folder.
- /// </summary>
- public StorageFolder Folder { get; set; }
- }
Each engine gets a concrete subclass. Here’s a short overview of the technologies:
XmlSerializer
Here’s how to serialize and deserialize with the XmlSerializer:
XmlSerializer
- public override async Task<int> Serialize(T instance)
- {
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- StringWriter stringWriter = new StringWriter();
- serializer.Serialize(stringWriter, instance);
- string content = stringWriter.ToString();
- StorageFile file = await this.Folder.CreateFileAsync(this.FileName, CreationCollisionOption.ReplaceExisting);
- await FileIO.WriteTextAsync(file, content);
-
- return content.Length / 1024;
- }
-
- public override async Task<T> Deserialize()
- {
- StorageFile file = await this.Folder.GetFileAsync(this.FileName);
- string content = await FileIO.ReadTextAsync(file);
- XmlSerializer serializer = new XmlSerializer(typeof(T));
-
- return (T)serializer.Deserialize(new StringReader(content));
- }
The XmlSerializer saves all public properties in the object graph, except the ones that are decorated with XmlIgnore:
XmlIgnore Attribute
- // Calculated - should not be serialized
- [XmlIgnore]
- public TimeSpan Duration
- {
- get
- {
- return this.End - this.Start;
- }
- }
The XML that is produced by this serializer, can be defined through attributes, but that is outside the scope of this article. The XmlSerializer is the only one that does NOT fire the methods that are flagged with OnSerializing, OnSerialized, OnDeserializing and/or OnDeserialized attributes. That may be a showstopper in some scenarios. Also notice that the XmlSerializer by default removes the insignificant white space, even in element content. If you don’t like that, you can change the setting. It’s not a configuration of the serializer itself: you have to add an extra field in each class to be serialized. He’re a snippet from the Item and SubItem classes:
Setting 'xml:space' value
- // Tell the XmlSerializer to preserve white space.
- [XmlAttribute("xml:space")]
- public string Space = "preserve";
The XmlSerializer is not the fastest, nor does it generate the smallest files. But in every scenario that I went through, it has beaten all the other engines hands down when it came to deserialization. On average, the XmlSerializer deserializes twice as fast as the competition. So when your app needs to read a large amount of data from local storage at startup, then you should choose this one.
DataContractSerializer
The DataContractSerializer is the one used by WCF. Here’s how to serialize and deserialize with it:
DataContractSerializer
- public override async Task<int> Serialize(T instance)
- {
- DataContractSerializer serializer = new DataContractSerializer(typeof(T));
- string content = string.Empty;
- using (var stream = new MemoryStream())
- {
- serializer.WriteObject(stream, instance);
- stream.Position = 0;
- content = new StreamReader(stream).ReadToEnd();
- }
-
- StorageFile file = await this.Folder.CreateFileAsync(this.FileName, CreationCollisionOption.ReplaceExisting);
- await FileIO.WriteTextAsync(file, content);
-
- return content.Length / 1024;
- }
-
- public override async Task<T> Deserialize()
- {
- StorageFile file = await this.Folder.GetFileAsync(this.FileName);
- var inputStream = await file.OpenReadAsync();
- DataContractSerializer serializer = new DataContractSerializer(typeof(T));
-
- return (T)serializer.ReadObject(inputStream.AsStreamForRead());
- }
It will serialize all public properties that are decorated with the DataMember attribute:
DataMember Attribute
- [DataMember]
- public string Name { get; set; }
During the process, it will fire the OnSerializing, OnSerialized, OnDeserializing and OnDeserialized methods. It serializes the fastest. The files are smaller than the ones produced by the XmlSerializer, but not significantly.
JsonSerializer
Here’s how to serialize and deserialize with the JsonSerializer:
Native JsonSerializer
- public override async Task<int> Serialize(T instance)
- {
- var serializer = new DataContractJsonSerializer(instance.GetType());
- string content = string.Empty;
- using (MemoryStream stream = new MemoryStream())
- {
- serializer.WriteObject(stream, instance);
- stream.Position = 0;
- content = new StreamReader(stream).ReadToEnd();
- }
-
- StorageFile file = await this.Folder.CreateFileAsync(this.FileName, CreationCollisionOption.ReplaceExisting);
- await FileIO.WriteTextAsync(file, content);
-
- return content.Length / 1024;
- }
-
- public override async Task<T> Deserialize()
- {
- StorageFile file = await this.Folder.GetFileAsync(this.FileName);
- string content = await FileIO.ReadTextAsync(file);
- var bytes = Encoding.Unicode.GetBytes(content);
- var serializer = new DataContractJsonSerializer(typeof(T));
-
- return (T)serializer.ReadObject(new MemoryStream(bytes));
- }
It uses the same DataMember attribute as the DataContractSerializer to flag the properties to be serialized. During the process, it will fire the OnSerializing, OnSerialized, OnDeserializing and OnDeserialized methods. It serializes a bit faster than the XmlSerializer, and the saved files are undoubtedly smaller (although not always significantly). If you save in the Roaming Folder –with its limited storage- you should consider using Json (but keep on reading: there’s another Json serializer in the benchmark). Unfortunately the JsonSerializer is by far the slowest when it comes to deserialization, and it crashes on uninitialized DateTime values. The.NET default of DateTime.MinValue is beyond its range:
So one way or another you have to make sure that your DateTime values remain in the Json range. This is what I did on the constructor of the business class:
Json DateTime Range
- public Item()
- {
- // To support Json serialization
- this.Start = DateTime.MinValue.ToUniversalTime();
- this.End = DateTime.MinValue.ToUniversalTime();
- }
I’m definitely not feeling comfortable with this.
Json.NET Serializer
Before you can use this serializer, you have to add a reference to the Json.NET assembly (e.g. through NuGet). Here’s how to serialize and deserialize with it:
NewtonSoft Json.NET Serializer
- public override async Task<int> Serialize(T instance)
- {
- string content = string.Empty;
- var serializer = new JsonSerializer();
- // Lots of possible configurations:
- // serializer.PreserveReferencesHandling = PreserveReferencesHandling.All;
- // Nice for debugging:
- // content = JsonConvert.SerializeObject(instance, Formatting.Indented);
- content = JsonConvert.SerializeObject(instance);
- StorageFile file = await this.Folder.CreateFileAsync(this.FileName, CreationCollisionOption.ReplaceExisting);
- await FileIO.WriteTextAsync(file, content);
-
- return content.Length / 1024;
- }
-
- public override async Task<T> Deserialize()
- {
- StorageFile file = await this.Folder.GetFileAsync(this.FileName);
- string content = await FileIO.ReadTextAsync(file);
-
- return JsonConvert.DeserializeObject<T>(content);
- }
When defining the properties to be serialized, you have the choice between opting in (serialized properties must be tagged with JsonProperty OR DataMember) or opting out (all public properties are serialized, except the ones flagged with JsonIgnore). Here’s an extract from the Item class again:
Json Attributes
- [JsonObject(MemberSerialization.OptIn)]
- public class Item
- {
- [JsonProperty]
- public DateTime Start { get; set; }
-
- // Calculated - should not be serialized
- [JsonIgnore]
- public TimeSpan Duration
- {
- get
- {
- return this.End - this.Start;
- }
- }
-
- // ...
- }
During the process, the Json.NET serializer will fire the OnSerializing, OnSerialized, OnDeserializing and OnDeserialized methods. It’s fast and it generates the smallest files. It’s also the most configurable: it comes with a lot of extra attributes and configuration settings. All of that comes with a price of course: the Newtonsoft.Json.dll adds more than 400KB to you app package. If you ask me, that’s a small price…
Conclusions
Of course you have to test these serialization engines against your own data and workload. But here are at least some general observations:
- For small amounts of data, it doesn’t matter which technology you use. But adding Json.NET would just make your package larger.
- Try to stick to one technology in your app. If you’re already using Json to fetch your server data, then use the same serializer to save locally.
- If you’re dealing with large amounts of data, prepare to handle OutOfMemory exceptions. Unsurprisingly these are thrown when you run out of memory:
- But OutOfMemory exception is also thrown when you run out of storage. I didn’t find any documentation of Local Storage limitations, but I do get exceptions when trying to allocate more than 100MB:
- If on startup you need to deserialize large amount of data, prefer the XmlSerializer.
- If you need one or more of the On[De]Serializ[ing][ed] methods, then don’t use the XmlSerializer.
- If you need to store and retrieve local data, but none of the serialization engines covers your requirements, then normalization and indexing is what you need. Well, it’s time for a real database then.
Code
Here’s the full code for the sample app. It was written in Visual Studio 2013, for Windows 8.1: U2UConsult.WinRT.SerializationSample.zip (3MB)
Enjoy,
Diederik