是否可以强制Json.net传递构造函数参数的默认值?
我尝试使用覆盖ContractResolver
和CreateConstructorParameters
方法的自定义CreatePropertyFromConstructorParameter
,并将返回的JsonProperty.DefaultValue
属性设置为我想要的值,将DefaultValueHandling
属性设置为DefaultValueHandling.Populate
。
通过覆盖CreateObjectContract
方法,我可以看到匹配的CreatorParameters
和Properties
都包含我的默认值。但是,我仍然得到一个null
值传递给构造函数。
我什至尝试将属性的Converter
设置为仅返回所需默认值但仍然没有运气的转换器。
下面是ConstractResolver代码:
public class DataControllerContractResolver : DefaultContractResolver
{
///The desired default value
private readonly IDataController _dataController;
private readonly Type _dataControllerType;
private readonly JsonConverter<IDataController> _converter;
public DataControllerContractResolver(IDataController dataController)
{
_dataController = dataController;
_dataControllerType = dataController.GetType();
_converter = SimpleJsonConverter<IDataController>.From((reader, controller, arg3) => _dataController, (writer, controller, arg3) => {});
}
protected override IList<JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
{
var props = base.CreateConstructorParameters(constructor, memberProperties);
foreach (var prop in memberProperties.Where(p => p.PropertyType.IsAssignableFrom(_dataControllerType)).ToList())
{
prop.DefaultValue = _dataController;
prop.Converter = _converter;
prop.DefaultValueHandling = DefaultValueHandling.Populate;
prop.Ignored = false;
}
foreach (var prop in props.Where(p => p.PropertyType.IsAssignableFrom(_dataControllerType)).ToList())
{
prop.DefaultValue = _dataController;
prop.Converter = _converter;
prop.DefaultValueHandling = DefaultValueHandling.Populate;
prop.Ignored = false;
}
return props;
}
protected override JsonProperty CreatePropertyFromConstructorParameter(JsonProperty matchingMemberProperty, ParameterInfo parameterInfo)
{
var prop = base.CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
if (prop.PropertyType.IsAssignableFrom(_dataControllerType))
{
prop.DefaultValue = _dataController;
prop.Converter = _converter;
prop.MemberConverter = _converter;
prop.DefaultValueHandling = DefaultValueHandling.Populate;
prop.Ignored = false;
}
return prop;
}
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
var contract = base.CreateObjectContract(objectType);
return contract;
}
}
最佳答案
如果在property.DefaultValue
中覆盖property.DefaultValueHandling
和CreateProperties(Type type, MemberSerialization memberSerialization)
,则可以使此工作有效。我没有您所有的课程(没有IDataController
),所以这是一个简单的示例:
public class DefaultStringValueContractResolver : DefaultContractResolver
{
public string DefaultStringValue { get; set; }
public DefaultStringValueContractResolver(string defaultStringValue)
{
this.DefaultStringValue = defaultStringValue;
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
// Set all string properties to have a default value of "this is a default value"
foreach (var property in properties.Where(p => p.PropertyType == typeof(string)))
{
property.DefaultValue = DefaultStringValue;
property.DefaultValueHandling = DefaultValueHandling.Populate;
}
return properties;
}
}
然后:
public class TestClass
{
const string DefaultStringValue = "This is a default string value";
public string Property1 { get; set; }
public string Property2 { get; set; }
public static void Test()
{
var settings = new JsonSerializerSettings { ContractResolver = new DefaultStringValueContractResolver(DefaultStringValue) };
var test = JsonConvert.DeserializeObject<TestClass>("{}", settings);
Debug.Assert(test.Property1 == DefaultStringValue && test.Property2 == DefaultStringValue); // No assert
Debug.WriteLine(JsonConvert.SerializeObject(test)); // Prints {"Property1":"This is a default string value","Property2":"This is a default string value"}
}
}
需要注意的一件事:如果默认值是引用类型,则包含该属性的类的所有实例都将具有引用同一默认值实例的默认值。