JSON NET自定义反序列化器根本不起作用 [英] JSON NET Custom deserializer not work at all

查看:82
本文介绍了JSON NET自定义反序列化器根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用自定义的Json解串器,我已经完成了下一个:

I need to use custom Json deserialiser I've done the next:

JsonCreationConverter

JsonCreationConverter

public abstract class AbstractJsonCreationConverter<T> : JsonConverter
{
    protected abstract T Create(Type objectType, JObject jsonObject);

    public override bool CanConvert(Type objectType)
    {
        return typeof(T).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType,
      object existingValue, JsonSerializer serializer)
    {
        var jsonObject = JObject.Load(reader);
        var target = Create(objectType, jsonObject);
        serializer.Populate(jsonObject.CreateReader(), target);
        return target;
    }

    public override void WriteJson(JsonWriter writer, object value,
   JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

JsonBuildBlockConverter

JsonBuildBlockConverter

protected override AbstractBuildBlock Create(Type objectType, JObject jsonObject)
    {
        var type = jsonObject["contentType"].ToString();
        switch(type)
        {
            case "text":
                return new TextBlock();
            default:
                return null;
        }
    }

活页夹

public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
    {
        // use Json.NET to deserialize the incoming Position
        controllerContext.HttpContext.Request.InputStream.Position = 0; // see: http://stackoverflow.com/a/3468653/331281
        Stream stream = controllerContext.RequestContext.HttpContext.Request.InputStream;
        var readStream = new StreamReader(stream, Encoding.UTF8);
        string json = readStream.ReadToEnd();
        return JsonConvert.DeserializeObject<Site>(json, new JsonBuildBlockConverter());
    }

mvc操作

public string Update(Site site)
    {
        //the goal to see in debugger block not null after next line
        TextBlock block = site.Pages[0].Rows[0].BuildBlocks[0] as TextBlock;
        //siteRepository.Add(site);
        return "Success";
    }

我已经在SiteModelBinder和JsonBuildBlockConverter中设置了断点.我进入SiteModelBinder,但不进入JsonBuildBlockConverter.并且在mvc操作站点中,所有字段均为null.为什么会发生?

I have set up break points in SiteModelBinder and JsonBuildBlockConverter. I go into SiteModelBinder but don't go to JsonBuildBlockConverter. And in mvc action site has all fields null. Why it happens?

推荐答案

问题出在我发送数据的方式上.因此,当您需要发送默认模型资料夹的数据时,请使用以下方法:

The problem was in the way I send the data. So, when you need to send data for default model binder use this:

$.ajax({
    ...
    data:{variableName: jsonValue}
}

默认的资料夹足够聪明,可以正常工作,但是现在我的SiteModelBinder读取了整个输入流,因此我用以下内容替换了数据:

Default binder will correctly work this he is clever enough, but now my SiteModelBinder as it reads the whole input stream I replace data with this:

$.ajax({
    data: jsonValue
}

都可以正常工作,所以问题在于variableName也是我尝试解析并导致错误的json的一部分.

and all become working, so the problem was that variableName was also a part of the json that I try to parse and cause the error.

这篇关于JSON NET自定义反序列化器根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆