【问题标题】:Json.NET custom JsonConverter being ignoredJson.NET 自定义 JsonConverter 被忽略
【发布时间】:2014-10-09 02:24:19
【问题描述】:

我有一个泛型类,我想用它的一个属性的值序列化它的子类。

为此,我编写了一个自定义 JsonConverter 并将其附加到具有 JsonConverter(Type) 属性的基类 - 但是,它似乎从未被调用过。作为参考,如下例所示,我正在使用System.Web.Mvc.Controller.Json()方法序列化对象的List<>

如果有更好的方法来达到相同的结果,我绝对愿意接受建议。

示例

查看功能

public JsonResult SomeView()
{
    List<Foo> foos = GetAListOfFoos();
    return Json(foos);
}

自定义 JsonConverter

class FooConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        System.Diagnostics.Debug.WriteLine("This never seems to be run");
        // This probably won't work - I have been unable to test it due to mentioned issues.
        serializer.Serialize(writer, (value as FooBase<dynamic, dynamic>).attribute);
    }

    public override void ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override bool CanConvert(Type objectType)
    {
        System.Diagnostics.Debug.WriteLine("This never seems to be run either");
        return objectType.IsGenericType
            && objectType.GetGenericTypeDefinition() == typeof(FooBase<,>);
    }
}

Foo 基类

[JsonConverter(typeof(FooConverter))]
public abstract class FooBase<TBar, TBaz>
    where TBar : class
    where TBaz : class
{
    public TBar attribute;
}

Foo 实现

public class Foo : FooBase<Bar, Baz>
{
    // ...
}

电流输出

[
    {"attribute": { ... } },
    {"attribute": { ... } },
    {"attribute": { ... } },
    ...
]

期望的输出

[
    { ... },
    { ... },
    { ... },
    ...
]

【问题讨论】:

    标签: c# asp.net-mvc json json.net


    【解决方案1】:

    首先 System.Web.Mvc.Controller.Json() 不适用于 Json.NET - 它使用的 JavaScriptSerializer 对您的 Json.NET 的东西一无所知。如果您仍想使用 System.Web.Mvc.Controller.Json() 调用,您应该执行this 之类的操作。还将WriteJson 更改为:

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, ((dynamic)value).attribute);
    }
    

    我认为这应该使您的代码正常工作。

    【讨论】:

    • 我的印象是它确实如此,但如果我错了也不会感到惊讶 - 也许是其他东西将 Json.NET 作为依赖项。谢谢你的文章,我会读一读。
    • ASP.NET WebApi 使用 JSON.NET 作为 json 序列化器,但 ASP.NET MVC 没有。我知道这很令人困惑。
    • 啊,原来如此。
    • 可以确认这是根本问题。感谢您的帮助。
    【解决方案2】:

    文档说: 要将 JsonConverter 应用于集合中的项目,请使用 JsonArrayAttribute、JsonDictionaryAttribute 或 JsonPropertyAttribute,并将 ItemConverterType 属性设置为您要使用的转换器类型。

    http://james.newtonking.com/json/help/html/SerializationAttributes.htm

    也许这会有所帮助。

    【讨论】:

    • 我去看看,谢谢。花了很长时间浏览文档,但并不真正知道我在寻找什么。
    【解决方案3】:

    发生在我身上的是,我按照 Visual Studio 的建议自动添加了 using 语句。并且错误地添加了using System.Text.Json.Serialization; 而不是using Newtonsoft.Json;

    所以我在目标类上使用了System.Text.Json.Serialization.JsonConverterAttribute。 Json.Net(正确地)忽略了哪个。

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 2021-10-09
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      相关资源
      最近更新 更多