Handling namespace changes with TypeNameHandling.All

女生的网名这么多〃 提交于 2019-11-30 13:58:20

问题


I managed to get my self into a fix with the JSON.net TypeNameHandling. I am storing a JSON formatted object using RavenDB and set the TypeNameHandling of the JSON.net serializer to true in order to deal with an inheritance structure I have in place.

I needed to change the namespace of the document which I am storing, so now when it is deserialzed it is throws the error "Error resolving type specified in JSON" because the reference to the type in the JSON document no longer exists.

Is it possible to intercept the Json deserialization in order to do some kind of rolling migration?

Thanks,


回答1:


Ok, figured it out. In the end it was pretty straight forward. You need to override the DefaultSerializationBinder which is responsible for creating the .Net type from the document. Since my json document has the old namespace in it, I needed to intercept the creation of that type to return the correct type. I put together a simple implementation which will allow you to configure "migrations" when the JSON serializer is created.

    public class NamespaceMigrationSerializationBinder : DefaultSerializationBinder
    {
        private readonly INamespaceMigration[] _migrations;

        public NamespaceMigrationSerializationBinder(params INamespaceMigration[] migrations)
        {
            _migrations = migrations;
        }

        public override Type BindToType(string assemblyName, string typeName)
        {
            var migration = _migrations.SingleOrDefault(p => p.FromAssembly == assemblyName && p.FromType == typeName);
            if(migration != null)
            {
                return migration.ToType;
            }
            return base.BindToType(assemblyName, typeName);
        }
    }

Where the interface is

public interface INamespaceMigration
{
    string FromAssembly { get; }

    string FromType { get; }

    Type ToType { get; }
}



回答2:


You can use an DocumentConversionListener to do this. Please take a look here: http://ayende.com/blog/66563/ravendb-migrations-rolling-updates



来源:https://stackoverflow.com/questions/9908913/handling-namespace-changes-with-typenamehandling-all

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!