예제 #1
0
        private static JsonProperty GetComplexTypedPropertyChild(Type complexType,
                                                                 JsonProperty complexTypedJsonProperty, PropertyInfo complexTypedPropertyInfo, JsonProperty child)
        {
            var newChild = GetJsonPropertyDuplicate(child);

            if (complexTypedJsonProperty.Ignored)
            {
                newChild.Ignored =
                    true; //this ensures that when a complex field is ignored, all its children are ignored too.
            }
            //set complex name
            newChild.SimplePropertyName = child.PropertyName;
            newChild.PropertyName       =
                $"{complexTypedJsonProperty.PropertyName}{Defaults.ComplexTypeNameSeparator}{child.PropertyName}";
            newChild.ComplexUnderlyingName =
                $"{complexTypedJsonProperty.GetComplexOrSimpleUnderlyingName()}{Defaults.ComplexTypeNameSeparator}{child.UnderlyingName}";

            //set new value provider
            newChild.ValueProvider = new ComplexTypedPropertyValueProvider
                                         (complexTypedJsonProperty.UnderlyingName, complexTypedJsonProperty.PropertyType,
                                         complexTypedJsonProperty.DeclaringType, complexTypedJsonProperty.ValueProvider,
                                         child.PropertyType, child.ValueProvider);

            //do this to avoid enclosing the jsonproperties in the closures.
            var parentActualName = complexTypedJsonProperty.UnderlyingName;
            var parentType       = (child.ValueProvider as ComplexTypedPropertyValueProvider)?.DeclaringType ??
                                   child.DeclaringType;
            var parentReflectedType = complexType;

            var childShouldSerialize = child.ShouldSerialize;

            //var childShouldDeserialize = child.ShouldDeserialize;

            //set a shouldserialize and shoulddeserialize
            newChild.ShouldSerialize = entity =>
            {
                var propertyInfo = entity.GetType().GetProperty(parentActualName);
                var parentValue  = propertyInfo.GetValue(entity);

                Utils.Utilities.CheckIfComplexTypeInstanceIsNull(parentValue, parentActualName,
                                                                 propertyInfo.DeclaringType);

                var parentValueType = parentValue.GetType();

                var isAssignable = parentType.IsGenericAssignableFrom(parentValueType);
                isAssignable = isAssignable || parentReflectedType == parentValueType;

                return(isAssignable && (childShouldSerialize == null || childShouldSerialize(parentValue)));
            };

            return(newChild);
        }