-Added NullValueHandling and DefaultValueHandling support to serializing dynamic properties

This commit is contained in:
James Newton-King
2013-05-01 20:26:05 +12:00
parent 2b9941fa74
commit da491c3a0f
2 changed files with 87 additions and 0 deletions
@@ -757,6 +757,9 @@ To fix this error either change the environment to be fully trusted, change the
{
JsonContract valueContract = GetContractSafe(memberValue);
if (!ShouldWriteDynamicProperty(memberValue))
continue;
if (CheckForCircularReference(writer, memberValue, null, valueContract, contract, member))
{
string resolvedPropertyName = (contract.PropertyNameResolver != null)
@@ -784,6 +787,18 @@ To fix this error either change the environment to be fully trusted, change the
}
#endif
private bool ShouldWriteDynamicProperty(object memberValue)
{
if (Serializer._nullValueHandling == NullValueHandling.Ignore && memberValue == null)
return false;
if (HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Ignore) &&
(memberValue == null || MiscellaneousUtils.ValueEquals(memberValue, ReflectionUtils.GetDefaultValue(memberValue.GetType()))))
return false;
return true;
}
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
TypeNameHandling resolvedTypeNameHandling =