SimpleSerializer - Serialization / Deserialization of almost anything.

Hello Everyone,

Here is something I have had sitting in my back pocket I thought other people might find useful.

SimpleSerializer is a XML based Serializer/Deserializer. Its purpose was to be able to quickly and easily serialize objects into a human readable format without the need to markup a class with all sorts of attribute tags, which sometimes is not possible if you are working with a compiled dll or pre-existing data types, and then deserialize it back from an xml based string in to its original object types.

Its intended purpose was as a way to quickly save and load data classes for either storage locally or transport over a network.

By using a set of simple and straight forward configuration options SimpleSerialize is typically able to achieve the desired output of a class without the need to use any attributes on classes, fields, or properties.

SimpleSerializer does allow the use of specifically designed attributes, but they are intended to enhance the basic functionality instead of be a requirement.

The project is on-going but seems stable enough right now in what it does to open it up to others to help break it and find things I may have missed. Right now SimpleSerializer is currently being used for transport of objects over a network inside a Socket Server I am working and handles everything I throw at it with no issues.

I do have several more plans for improvements, including fully handling generic types (they are just a bit of a pain) and some features i would like to work in to the attribute tags.

Features

  • Open Source (Bitbucket)
  • Serializes to XML for human readability
  • Serialize almost anything without the need to mark classes, fields, and properties as serializable
  • Ability to deserialize back in to original object types.
  • Maintains References for arrays, lists, dictionary, and other class data types upon deserialization.
  • Easy configuration

Supported Data Types

  • Primitives (sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, boolean, decimal)
  • Strings
  • Enums
  • Arrays - Single and Multi-Dimensional
  • List<>
  • Dictionary<,>
  • Custom Classes - Generics are not currently supported

Performance
Tests were done using the USER_LIST_INFO class shown below in the spoiler. Speeds for a single class, a Both List<> and an Array of USER_LIST_INFO with 1000 unique instances, and also both List<> and Arrays with 1000 duplicate entries, and finally Serializing the same USER_LIST_INFO 1000 times in a loop.

For deserialization times keep in mind these are NOT times for serialization in to an XML object, but back in to the data original object types.

USER_LIST_INFO Class

public class USER_LIST_INFO
        {
            public int id;
            public string username;

            public USER_LIST_INFO()
            {

            }
            public USER_LIST_INFO(int id, string username)
            {
                this.id = id;
                this.username = username;
            }
        }

Processor: Intel Core2 Quad q6700 2.67ghz (so nothing remotly new or fancy)
Results

  • Single Class: Serialization - 0ms Deserialization - 0ms
  • List<> 1000 duplicate entries: Serialization - 0ms Deserialization - 8ms
  • USER_LIST_INFO[ ] 1000 duplicate entries: Serialization - 0ms Deserialization - 15ms
  • List<> 1000 unique entries: Serialization - 33ms Deserialization - 16ms
  • USER_LIST_INFO[ ] 1000 unique entries: Serialization - 34ms Deserialization - 24ms
  • USER_LIST_INFO loop 1000 times: Serialization - 14ms Deserialization - 19ms

As always any feedback is more then welcome.

Source Code BitBucket link - Bitbucket

1 Like

Hey,

Repository has been Updated

The SSField, and SSProperty attributes have been updated to support calling an alternate function during serialization to acquire the fields/properties value used in the serialization process.

The SSField and SSProperty attributes also now support calling of an alternate function on deserialization of a field or property in order to set its data.

This is useful in a situation where the data held in the field is either not all needed, or needed in a different format than it is held in. For instance perhaps you have a list that holds all the classes for users. You may not need to serialize each user class, but instead serialize a list of UserID’s based on the classes in the list. Using the redirect options you can easily accomplish this by telling it to call a different function when serializing the property instead of directly saving the properties value, and then again on de-serialization you can have it redirect the de-serialized object to a function of your choice instead of being put directly back in to the field or property.

Hope my description makes sense.

Cheers