给定JsonSerializerSettings对象,Type和System.Reflection.PropertyInfo对象,我该如何确定:
1)使用给定设置转换为JSON.Net时,该属性的JSON属性名称是什么?给定属性或类本身可能存在或不存在的各种规则和属性,以及可能在JsonSerializerSettings中设置的各种设置?
2)如何知道给定的" JSON"使用JSON.Net进行反序列化时,属性名称是否会映射到c#属性?如果是这样,哪个属性?
class Person
{
[JsonProperty("person_id")]
Guid PersonId { get; set; } // given property info for this, want to recieve "person_id"
string FirstName { get; set; } // will this serialize to "firstName"? "FirstName"? depending on the settings?
string LastName { get; set; } //given the settings, would { "LASTNAME": "Johnson" } be serialized into this property?
[JsonIgnore]
string SSN { get; set; } // determine will this property be mapped?
}
注意我希望找到一个解决方案,而不是自己查看属性属性(考虑到所有可能性和合同解析器容易出错),我想根据JsonSerializerSettings确定属性映射是什么?< / p>
答案 0 :(得分:1)
您需要的信息可从Json.NET的contract resolver获得。访问它的步骤顺序如下:
JsonSerializer
。Person
类型的合同并将其转换为JsonObjectContract
。JsonProperty
列表中找到您的媒体资源Properties
。有关该特定属性的所有序列化信息都在那里。因此,例如您可以创建以下扩展方法:
public static partial class JsonExtensions
{
static JsonProperty GetProperty(this JsonSerializerSettings settings, Type type, string underlyingName)
{
// Use JsonSerializer.Create(settings) instead if your framework ignores the global JsonConvert.DefaultSettings
var resolver = JsonSerializer.CreateDefault(settings).ContractResolver;
var contract = resolver.ResolveContract(type) as JsonObjectContract;
if (contract == null)
throw new ArgumentException(string.Format("{0} is not a JSON object", type));
return contract.Properties.Where(p => p.UnderlyingName == underlyingName).SingleOrDefault();
}
public static string GetPropertyName(this JsonSerializerSettings settings, Type type, string underlyingName)
{
var property = settings.GetProperty(type, underlyingName);
// The property might be null if it is nonpublic and not marked with [JsonProperty]
return property == null ? null : property.PropertyName;
}
public static bool GetIsIgnored(this JsonSerializerSettings settings, Type type, string underlyingName)
{
var property = settings.GetProperty(type, underlyingName);
// The property might be null if it is nonpublic and not marked with [JsonProperty]
return property == null ? true : property.Ignored;
}
}
然后,如果您使用以下方法:
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
};
foreach (var property in typeof(Person).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
Console.WriteLine("Property {0}: Json name = \"{1}\", IsIgnored = {2}", property, settings.GetPropertyName(typeof(Person), property.Name), settings.GetIsIgnored(typeof(Person), property.Name));
}
输出
Property System.Guid PersonId: Json name = "person_id", IsIgnored = False
Property System.String FirstName: Json name = "firstName", IsIgnored = False
Property System.String LastName: Json name = "lastName", IsIgnored = False
Property System.String SSN: Json name = "ssn", IsIgnored = True
注意:
Json.NET维护可通过JsonConvert.DefaultSettings访问的全局默认JsonSerializerSettings
。这个属性
获取或设置一个创建默认
JsonSerializerSettings
的函数。默认设置由JsonConvert
上的序列化方法以及ToObject<T> ()
上的FromObject(Object)
和JToken
自动使用。要在不使用任何默认设置的情况下进行序列化,请使用JsonSerializer
创建Create()
。
如果您的框架忽略了全局默认设置,则应在上述方法中将JsonSerializer.CreateDefault(settings)
替换为JsonSerializer.Create(settings)
。
为了提高性能,您可以制作一次序列化程序,并将扩展方法添加到JsonSerializer
而不是JsonSerializerSettings
。如果您需要为给定属性提供多个属性,则只需获取JsonProperty
并直接访问它。
Json.NET可以序列化字段和属性,因此要求将PropertyInfo
传递给扩展方法可能会限制对某些必要的序列化信息的访问。
示例工作.Net fiddle。