本文介绍了如何设置Json.NET ContractSerializer一定特定类型的,而不是全局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要订​​下契约序列化只是对某些类型的在我的ASP.NET Web API应用。我可以设置全局设置在App_Start / FormatterConfig.cs是这样的:

I want to set a contract serializer just for certain types in my ASP.NET Web API application. I can set the settings globally in the App_Start/FormatterConfig.cs like this:

public static void RegisterGlobalFormatters(MediaTypeFormatterCollection formatters)
{
    jsonSerializerSettings.ContractResolver = new CriteriaContractResolver(new List<string>(new string[]{"mdData", "name", "label"})); 

...

但我怎么能只适用于这一个或多个特定类类型?

but how can I just apply this to one or more specific class types?

我想这样做的原因是因为我需要可以设置哪些领域应该在运行时根据配置或参数类似这些例子中,Web服务被序列化:

The reason I want to do this is because I need to be able to set what fields should be serialized at runtime based on configuration or parameters to the web service similar to these examples:

<一个href=\"http://stackoverflow.com/questions/5872855/using-json-net-how-do-i-$p$pvent-serializing-properties-of-a-derived-class-when\">Using JSON.net,我怎么prevent序列化派生类,在基类环境中使用时的属性?

<一个href=\"http://json.$c$cplex.com/discussions/347610\">http://json.$c$cplex.com/discussions/347610

推荐答案

我结束了使用JsonConverter只写道,在属性列表中指定的参数。它更低水平比ContractResolver或格式化,但我不认为这是可能的特定类型配置任何一个。

I ended up using a JsonConverter that only writes the parameters that are specified in the "properties" list. It's more low level than a ContractResolver or a formatter, but I don't think it's possible to configure either one for a specific type.

public class ResourceConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Resource));
    }

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

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        List<string> properties = new List<string>(new string[] { "Name", "Label" });

        writer.WriteStartObject();
        foreach (MemberInfo mi in value.GetType().GetMembers(BindingFlags.GetField | BindingFlags.Instance | BindingFlags.Public) )
        {
            PropertyInfo p = mi as PropertyInfo;

            if (p != null && p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Length == 0 && properties.Contains(p.Name))
            {
                writer.WritePropertyName(p.Name);
                serializer.Serialize(writer, p.GetValue(value, new object[] { }));
            }
        }
        writer.WriteEndObject();
    }
}

此可以使用属性被应用到类:

This can be applied to a class using the attribute:

[JsonConverter(typeof(ResourceConverter))]

这似乎是一个黑客虽然,我想我应该用合同解析器来获得属性列表序列化,而不是直接使用反射,但我不知道怎么样。

This seems like a hack though, I think I should use the contract resolver to get the list of properties to serialize instead of using reflection directly, but I'm not sure how.

这篇关于如何设置Json.NET ContractSerializer一定特定类型的,而不是全局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:06