Serialization Json.NET:序列化图表系列只会返回“0”;键入名称";

Serialization Json.NET:序列化图表系列只会返回“0”;键入名称";,serialization,json.net,Serialization,Json.net,我正在尝试使用NewtonSoft.Json序列化System.Windows.Forms.DataVisualization.Charting.Series,得到的只是该系列的名称及其类型。我正在尝试获取系列的所有属性,以便保存系列设置 我对序列化非常陌生,但我在其他对象方面取得了一些成功,所以我不确定这里发生了什么。以下是我所做的(作为示例): 返回: “系列测试名称” 我期待着更像 { "AxisLabel": "", "BackG

我正在尝试使用NewtonSoft.Json序列化System.Windows.Forms.DataVisualization.Charting.Series,得到的只是该系列的名称及其类型。我正在尝试获取系列的所有属性,以便保存系列设置

我对序列化非常陌生,但我在其他对象方面取得了一些成功,所以我不确定这里发生了什么。以下是我所做的(作为示例):

返回:
“系列测试名称”

我期待着更像

{
  "AxisLabel": "",
  "BackGradientStyle": None,
  "BackHatchStyle": None
...
}

感谢@dbc将我指向所讨论的
NoTypeConverterJsonConverter
,我能够解决我的问题

我制作了一个修改版的
notypecoverterjsonconverter
,它采用了一系列类型来应用NoType转换:

public class NoTypeConverterJsonConverter : JsonConverter
{
    private NoTypeConverterContractResolver Resolver = new NoTypeConverterContractResolver();

    /// <summary>
    /// The types where the default typeconverter will not be used
    /// </summary>
    public List<Type> TypesToOverride
    {
        get { return Resolver.DefaultedTypes; }
        set { Resolver.DefaultedTypes = value; }
    }

    private class NoTypeConverterContractResolver : DefaultContractResolver
    {
        /// <summary>
        /// The types where the default typeconverter will not be used
        /// </summary>
        public List<Type> DefaultedTypes = new List<Type>();

        protected override JsonContract CreateContract(Type objectType)
        {
            // if its in the listed types
            if (DefaultedTypes.Any(t => t.IsAssignableFrom(objectType)))
            {
                // create a default contract
                JsonObjectContract contract = base.CreateObjectContract(objectType);
                // Null out the converter to not use the default typeconverter.
                contract.Converter = null; 
                return contract;
            }
            // if it decends from a list
            else if (typeof(IList<>).IsAssignableFrom(objectType))
            {
                // create a contract from the object, avoiding any presets(?)
                JsonObjectContract contract = this.CreateObjectContract(typeof(IList<>));
                //contract.Converter = null; // Also null out the converter to prevent infinite recursion.
                return contract;
            }

            try
            {
                // use default contract creator
                return base.CreateContract(objectType);
            }
            catch (Exception ex)
            {
                // see if it can be treated as a list
                if (typeof(IList<>).IsAssignableFrom(objectType))
                {
                    // create a contract from the object, avoiding any presets(?)
                    JsonObjectContract contract = this.CreateObjectContract(typeof(IList<>));
                    //contract.Converter = null; // Also null out the converter to prevent infinite recursion.
                    return contract;
                }

                throw ex;
            }
                    
        }

        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            JsonProperty property = base.CreateProperty(member, memberSerialization);

            return property;
        }
    }

    /// <summary>
    /// Default Constructor
    /// </summary>
    /// <param name="skippedTypes"></param>
    public NoTypeConverterJsonConverter(List<Type> skippedTypes = null)
    {
        if (skippedTypes != null)
        {
            TypesToOverride = skippedTypes;
        }
    }

    public override bool CanConvert(Type objectType)
    {
        return TypesToOverride.Any(t => t.IsAssignableFrom(objectType));
    }

    public override object ReadJson(
        JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return JsonSerializer.CreateDefault(
            new JsonSerializerSettings { ContractResolver = Resolver }
            ).Deserialize(reader, objectType);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JsonSerializer.CreateDefault(
            new JsonSerializerSettings { ContractResolver = Resolver }
            ).Serialize(writer, value);
    }
}
然后使用代码,如下所示:

// The types to not use the default typeconverter
List<Type> skippedTypes = new List<Type>()
{
    typeof(DataPoint),
    typeof(DataPointCustomProperties),
    typeof(SmartLabelStyle)
};

// create converter
NoTypeConverterJsonConverter converter = new NoTypeConverterJsonConverter(skippedTypes);

// use the converter to serialize a series
string seriesstr = JsonConvert.SerializeObject(series, Formatting.Indented, 
    new JsonSerializerSettings()
    {
        Converters = new List<JsonConverter> { converter }
    });

// deserialise using the same converter
Series series2 = JsonConvert.DeserializeObject<Series>(seriesstr, new JsonSerializerSettings()
{
    Converters = new List<JsonConverter> { converter }
});
//不使用默认类型转换器的类型
List skippedTypes=新列表()
{
类型(数据点),
类型(DataPointCustomProperties),
类型(智能标签样式)
};
//创建转换器
NotTypeConverterJSONConverter converter=新的NotTypeConverterJSONConverter(跳过类型);
//使用转换器序列化系列
string seriesstr=JsonConvert.SerializeObject(序列、格式、缩进、,
新的JsonSerializerSettings()
{
转换器=新列表{converter}
});
//使用相同的转换器反序列化
Series series2=JsonConvert.DeserializeObject(seriesstr,新的JsonSerializerSettings()
{
转换器=新列表{converter}
});

实际上,只要在列表中添加给您带来麻烦的任何类型,它通常会对其进行排序。

可能
Series
应用了
TypeConverter
。尝试将
notypecoverterjsonconverter
添加到,其中``notypecoverterjsonconverter`来自到。
// see if it can be treated as a list
if (typeof(IList<>).IsAssignableFrom(objectType))
{
    // create a contract from the object, avoiding any presets(?)
    JsonObjectContract contract = this.CreateObjectContract(typeof(IList<>));
    //contract.Converter = null; // Also null out the converter to prevent infinite recursion.
    return contract;
}
// The types to not use the default typeconverter
List<Type> skippedTypes = new List<Type>()
{
    typeof(DataPoint),
    typeof(DataPointCustomProperties),
    typeof(SmartLabelStyle)
};

// create converter
NoTypeConverterJsonConverter converter = new NoTypeConverterJsonConverter(skippedTypes);

// use the converter to serialize a series
string seriesstr = JsonConvert.SerializeObject(series, Formatting.Indented, 
    new JsonSerializerSettings()
    {
        Converters = new List<JsonConverter> { converter }
    });

// deserialise using the same converter
Series series2 = JsonConvert.DeserializeObject<Series>(seriesstr, new JsonSerializerSettings()
{
    Converters = new List<JsonConverter> { converter }
});