Top serialization Questions

List of Tags

Eclipse issues warnings when a serialVersionUID is missing.

The serializable class Foo does not declare a static final serialVersionUID field of type long

What is serialVersionUID and why is it important? Please show an example where missing serialVersionUID will cause a problem.

Answered By: Jon Skeet ( 293)

The docs for are probably about as good an explanation as you'll get:

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.


How do I convert all elements of my form to a JS object?

I'd like to have some way of automatically building a JS object from my form, without having to loop over each element. I do not want a string, as returned by $('#formid').serialize();, nor do I want the map returned by $('#formid').serializeArray();

Answered By: Tobias Cohen ( 373)

serializeArray already does exactly that, you just need to massage the data into your required format:

$.fn.serializeObject = function()
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[] !== undefined) {
            if (!o[].push) {
                o[] = [o[]];
            o[].push(this.value || '');
        } else {
            o[] = this.value || '';
    return o;

Example of this in action:

Edit: Updated code to fix bug when submitting multiple values for a single field, at least one of which is an empty string - thanks Daniel.


I need to store a multi-dimensional associative array of data in a flat file for caching purposes. I might occasionally come across the need to convert it to JSON for use in my web app but the vast majority of the time I will be using the array directly in PHP.

Would it be more efficient to store the array as JSON or as a PHP serialized array in this text file? I've looked around and it seems that in the newest versions of PHP (5.3), json_decode is actually faster than unserialize.

I'm currently leaning towards storing the array as JSON as I feel its easier to read by a human if necessary, it can be used in both PHP and JavaScript with very little effort, and from what I've read, it might even be faster to decode (not sure about encoding, though).

Does anyone know of any pitfalls? Anyone have good benchmarks to show the performance benefits of either method?

Thanks in advance for any assistance.

Answered By: Peter Bailey ( 173)

Depends on your priorities.

If performance is you absolute driving characteristic, then by all means use the fastest one. Just make sure you have a full understanding of the differences before you make a choice

  • JSON converts UTF-8 characters to unicode escape sequences. serialize() does not.
  • JSON will have no memory of what the object's original class was (they are always restored as instances of stdClass).
  • You can't leverage __sleep() and __wakeup() with JSON
  • Only public properties are serialized with JSON
  • JSON is more portable

And there's probably a few other differences I can't think of at the moment.


A simple speed test to compare the two


ini_set( 'display_errors', 1 );
error_reporting( E_ALL );

//  Make a bit, honkin test array
//  You may need to adjust this depth to avoid memory limit errors
$testArray = fillArray( 0, 5 );

//  Time json encoding
$start = microtime( true );
json_encode( $testArray );
$jsonTime = microtime( true ) - $start;
echo "JSON encoded in $jsonTime seconds<br>";

//  Time serialization
$start = microtime( true );
serialize( $testArray );
$serializeTime = microtime( true ) - $start;
echo "PHP serialized in $serializeTime seconds<br>";

//  Compare them
if ( $jsonTime < $serializeTime )
    echo "json_encode() was roughly " . number_format( ($serializeTime / $jsonTime - 1 ) * 100, 2 ) . "% faster than serialize()";
else if ( $serializeTime < $jsonTime )
    echo "serialize() was roughly " . number_format( ($jsonTime / $serializeTime - 1 ) * 100, 2 ) . "% faster than json_encode()";
} else {
    echo 'Unpossible!';

function fillArray( $depth, $max )
    static $seed;
    if ( is_null( $seed ) )
        $seed = array( 'a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10 );
    if ( $depth < $max )
        $node = array();
        foreach ( $seed as $key )
            $node[$key] = fillArray( $depth + 1, $max );
        return $node;
    return 'empty';

Is there a way to deserialize JSON content into a C# 4 dynamic type? It would be nice to skip creating a bunch of classes in order to use the DataContractJsonSerializer.

Answered By: Drew Noakes ( 127)

Unfortunately the blog post by Nikhil Kothari doesn't work with .NET 4 RTM.

An alternative deserialisation approach is suggested here. I modified the code slightly to fix a bug and suit my coding style. All you need is this:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

private sealed class DynamicJsonConverter : JavaScriptConverter
    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        if (dictionary == null)
            throw new ArgumentNullException("dictionary");

        return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        throw new NotImplementedException();

    public override IEnumerable<Type> SupportedTypes
        get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }

    #region Nested type: DynamicJsonObject

    private sealed class DynamicJsonObject : DynamicObject
        private readonly IDictionary<string, object> _dictionary;

        public DynamicJsonObject(IDictionary<string, object> dictionary)
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");
            _dictionary = dictionary;

        public override string ToString()
            var sb = new StringBuilder("{");
            return sb.ToString();

        private void ToString(StringBuilder sb)
            var firstInDictionary = true;
            foreach (var pair in _dictionary)
                if (!firstInDictionary)
                firstInDictionary = false;
                var value = pair.Value;
                var name = pair.Key;
                if (value is string)
                    sb.AppendFormat("{0}:\"{1}\"", name, value);
                else if (value is IDictionary<string, object>)
                    new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);
                else if (value is ArrayList)
                    sb.Append(name + ":[");
                    var firstInArray = true;
                    foreach (var arrayValue in (ArrayList)value)
                        if (!firstInArray)
                        firstInArray = false;
                        if (arrayValue is IDictionary<string, object>)
                            new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);
                        else if (arrayValue is string)
                            sb.AppendFormat("\"{0}\"", arrayValue);
                            sb.AppendFormat("{0}", arrayValue);

                    sb.AppendFormat("{0}:{1}", name, value);

        public override bool TryGetMember(GetMemberBinder binder, out object result)
            if (!_dictionary.TryGetValue(binder.Name, out result))
                // return null to avoid exception.  caller can check for null this way...
                result = null;
                return true;

            var dictionary = result as IDictionary<string, object>;
            if (dictionary != null)
                result = new DynamicJsonObject(dictionary);
                return true;

            var arrayList = result as ArrayList;
            if (arrayList != null && arrayList.Count > 0)
                if (arrayList[0] is IDictionary<string, object>)
                    result = new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)));
                    result = new List<object>(arrayList.Cast<object>());

            return true;


You can use it like this:

string json = ...;

var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });

dynamic obj = serializer.Deserialize(json, typeof(object));

So, given a JSON string:

    { "Name":"Apple", "Price":12.3 },
    { "Name":"Grape", "Price":3.21 }

The following code will work at runtime:

var data = serializer.Deserialize(json, typeof(object));

data.Date; // "21/11/2010"
data.Items.Count; // 2
data.Items[0].Name; // "Apple"
data.Items[0].Price; // 12.3 (as a decimal)
data.Items[1].Name; // "Grape"
data.Items[1].Price; // 3.21 (as a decimal)

I'm interested in any discussion about this approach.


I updated the code to fix a small bug (with lists of complex types) and to include a ToString method that outputs the JSON string, which I found useful for debugging. You can drop the two methods out if you don't want them as they aren't required for deserialisation.


If you are happy to have a dependency upon the System.Web.Helpers assembly, then as Vlad points out you can use the built-in methods of the Json class. It is included with the MVC framework as an additional download to the .NET 4 framework. Be sure to give Vlad an upvote if that's helpful! However if you cannot assume the client environment includes this DLL, then the above approach is a good alternative.


I want a true deep copy. In Java, this was easy, but how do you do it in C#?

Answered By: Kilhoffer ( 193)

I've seen a few different approaches to this, but I use a generic utility method as such:

public static T DeepClone<T>(T obj)
 using (var ms = new MemoryStream())
   var formatter = new BinaryFormatter();
   formatter.Serialize(ms, obj);
   ms.Position = 0;

   return (T) formatter.Deserialize(ms);


  • Your class MUST be marked as [Serializable] in order for this to work.
  • Your source file must include the following code:

    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;