public class GDALInfoResult
{
public GDALInfoResult()
{
MetaData = new Dictionary<string, string>();
}
public Dictionary<string, string> MetaData { get; set; }
}
class Program
{
static void Main(string[] args)
{
var result = new GDALInfoResult()
{
};
result.MetaData["EXIF_TestSomthing"] = "Hello";
Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }));
}
}
上面的代码将打印出来:
{
"metaData": {
"exiF_TestSomthing": "Hello"
}
}
有什么方法可以覆盖元数据属性,例如它不使用控制台写入行中指定的CamelCasePropertyNamesContractResolver
?
我不希望它更改我的 MetaData 属性中的名称,但不能更改序列化对象中使用的序列化设置。(因为我可以在这个演示中,但这只是为了显示我的问题)。
下面就可以解决问题,但需要很多额外的代码来做到这一点。有效,如果没有其他人提出更好的解决方案。
public class IgnoreSettingsConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
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)
{
var settings = new JsonSerializerSettings();
var s = JsonSerializer.Create(settings);
s.Serialize(writer, value);
}
}
public class GDALInfoResult
{
public GDALInfoResult()
{
MetaData = new Dictionary<string, string>();
}
[JsonConverter(typeof(IgnoreSettingsConverter))]
public Dictionary<string, string> MetaData { get; set; }
}