Deserialize 10-20 times slower than Serialize in ver 4

Jan 6, 2011 at 8:13 PM

I determined that for my objects the culprit was: "assembly = Assembly.LoadWithPartialName(assemblyName);" in the class DefaultSerializationBinder.

I added a TypeCache and attempt Load before LoadWithPartientNames. Deserialization time is now 1/2 of serialize.

 --- Ted

 ...in DefaultSerializationBinder...

        private Dictionary<string, Type> _typeCache = new Dictionary<string, Type>();

        protected Dictionary<string, Type> TypeCache
        {
            get { return _typeCache; }
            set { _typeCache = value; }
        }

       public override Type BindToType(string assemblyName, string typeName)
        {
            if (assemblyName != null)
            {
                Assembly assembly;

                String key = assemblyName + "::" + typeName;

                Type hit = null;
                if (this.TypeCache.TryGetValue(key, out hit))
                {
                    return hit;
                }              
                try
                {
                    assembly = Assembly.Load(assemblyName);
                }
                catch
                {
                    #pragma warning disable 618,612
                    assembly = Assembly.LoadWithPartialName(assemblyName);
                    #pragma warning restore 618,612
                }

                if (assembly == null)
                    throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, assemblyName));

                Type type = assembly.GetType(typeName);
                if (type == null)
                    throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));

                this.TypeCache[key] = type;

                return type;
            }
            else
            {
                return Type.GetType(typeName);
            }
        }
    }

Coordinator
Jan 8, 2011 at 3:41 AM

Thanks for taking the time to investigate. Your issue is fixed in the latest source code

http://json.codeplex.com/SourceControl/list/changesets