問題描述
我正在嘗試實現一個自定義 JSON.net IContractResolver,它將用指定的字符串替換所有空屬性值.我知道此功能可通過序列化類型成員的屬性獲得;這是我們正在考慮的替代路線.
I am attempting to implement a custom JSON.net IContractResolver that will replace all null property values with a specified string. I'm aware that this functionality is available via attributes on member of types that get serialized; this is an alternative route that we're considering.
到目前為止,我的解析器實現如下.StringValueProvider 是 IValueProvider 的一個簡單實現,不會影響問題,因為我不知道如何獲取 property
的值,因為我不知道實例的這種方法提供了 member
所以我不能將它作為參數傳遞給 GetValue()
(在代碼示例中標記為 WHAT-GOES-HERE?).
My resolver implementation so far is as follows. StringValueProvider is a simple implementation of IValueProvider that doesn't affect the problem, which is that I can't figure out how to get the value of property
as I have no knowledge in this method of the instance that supplied member
so I can't pass it in as an argument to GetValue()
(marked as WHAT-GOES-HERE? in the code sample).
有沒有一種方法可以讓我從 member
或 property
獲得我需要的東西?
Is there a way that I can get what I need from member
or from property
?
public class NullSubstitutionPropertyValueResolver : DefaultContractResolver
{
private readonly string _substitutionValue;
public NullSubstitutionPropertyValueResolver(string substitutionValue)
{
_substitutionValue = substitutionValue;
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty result = base.CreateProperty(member, memberSerialization);
PropertyInfo property = member as PropertyInfo;
if (property == null)
{
return result;
}
// What do I use here to get the property value?
bool isNull = property.GetValue(WHAT-GOES-HERE?) == null;
if (isNull)
{
result.ValueProvider = new StringValueProvider(_substitutionValue);
}
return result;
}
}
推薦答案
合約解析器不關心實例,它關心類型.值提供者關注實例.在合約解析器中,您根據屬性類型決定是否應將值提供程序應用于屬性(例如,您可能只想在 string
上使用 StringValueProvider
屬性?)然后,您讓值提供者存儲對該屬性的引用(將其與替換值一起傳遞到構造函數中).在值提供者中,您可以從對象實例中讀取值,檢查它是否為空并進行適當的值替換.
The contract resolver is not concerned with instances, it is concerned with types. The value provider is concerned with instances. In the contract resolver, you decide whether the value provider should be applied to the property based on the property type (for example, maybe you only want to use a StringValueProvider
on string
properties?) Then, you make the value provider store a reference to the property (pass it in the constructor along with the substitution value). In the value provider, you can read the value from the object instance, check if it is null and do the appropriate value substitution.
代碼應如下所示:
public class NullSubstitutionPropertyValueResolver : DefaultContractResolver
{
private readonly string _substitutionValue;
public NullSubstitutionPropertyValueResolver(string substitutionValue)
{
_substitutionValue = substitutionValue;
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty result = base.CreateProperty(member, memberSerialization);
PropertyInfo property = member as PropertyInfo;
if (property.PropertyType == typeof(string))
{
result.ValueProvider = new StringValueProvider(property, _substitutionValue);
}
return result;
}
}
public class StringValueProvider : IValueProvider
{
private PropertyInfo _targetProperty;
private string _substitutionValue;
public StringValueProvider(PropertyInfo targetProperty, string substitutionValue)
{
_targetProperty = targetProperty;
_substitutionValue = substitutionValue;
}
// SetValue gets called by Json.Net during deserialization.
// The value parameter has the original value read from the JSON;
// target is the object on which to set the value.
public void SetValue(object target, object value)
{
_targetProperty.SetValue(target, value);
}
// GetValue is called by Json.Net during serialization.
// The target parameter has the object from which to read the value;
// the return value is what gets written to the JSON
public object GetValue(object target)
{
object value = _targetProperty.GetValue(target);
return value == null ? _substitutionValue : value;
}
}
這是一個工作演示:https://dotnetfiddle.net/PAZULK
這篇關于覆蓋自定義 JSON.net 合同解析器中的屬性值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!