/// <summary> /// Visits the children of the given payload element and replaces it with a copy if any child changes /// </summary> /// <param name="payloadElement">The payload element to potentially replace</param> /// <returns>The original element or a copy to replace it with</returns> public virtual ODataPayloadElement Visit(PrimitiveProperty payloadElement) { ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement"); var replacedValue = this.Recurse(payloadElement.Value) as PrimitiveValue; ExceptionUtilities.CheckObjectNotNull(replacedValue, "Replaced value was null or wrong type"); if (!this.ShouldReplace(payloadElement.Value, replacedValue)) { return(payloadElement); } return(payloadElement.ReplaceWith(new PrimitiveProperty(payloadElement.Name, replacedValue))); }
/// <summary> /// Normalizes primitive properties, potentially replacing them with collections if the metadata indicates the payload is from a service operation /// </summary> /// <param name="payloadElement">The payload element to potentially replace</param> /// <returns>The original element or a copy to replace it with</returns> public override ODataPayloadElement Visit(PrimitiveProperty payloadElement) { var replaced = base.Visit(payloadElement); if (replaced.ElementType == ODataPayloadElementType.PrimitiveProperty) { payloadElement = (PrimitiveProperty)replaced; // if the payload looks like // <Foo /> // then it will be deserialized as a primitive property, when it could be an empty collection // if (this.ShouldReplaceWithCollection(payloadElement, payloadElement.Value.IsNull, payloadElement.Value.FullTypeName)) { // if the value is an empty string var stringValue = payloadElement.Value.ClrValue as string; if (stringValue != null && stringValue.Length == 0) { // get the element data type. Note that this must succeed based on the checks performed earlier var dataType = ((CollectionDataType)payloadElement.Annotations.OfType <DataTypeAnnotation>().Single().DataType).ElementDataType; // determine whether to return a complex or primitive collection based on the data type ODataPayloadElementCollection collection; if (dataType is PrimitiveDataType) { collection = new PrimitiveCollection(); } else { ExceptionUtilities.Assert(dataType is ComplexDataType, "Data type was neither primitive nor complex"); collection = new ComplexInstanceCollection(); } // return the replacement return(payloadElement .ReplaceWith(collection) .WithAnnotations(new CollectionNameAnnotation() { Name = payloadElement.Name })); } } } return(replaced); }
/// <summary> /// Normalizes primitive properties, potentially replacing them with collections if the metadata indicates the payload is from a service operation /// </summary> /// <param name="payloadElement">The payload element to potentially replace</param> /// <returns>The original element or a copy to replace it with</returns> public override ODataPayloadElement Visit(PrimitiveProperty payloadElement) { var replaced = base.Visit(payloadElement); if (replaced.ElementType == ODataPayloadElementType.PrimitiveProperty) { payloadElement = (PrimitiveProperty)replaced; // if the payload looks like // <Foo /> // then it will be deserialized as a primitive property, when it could be an empty collection // if (this.ShouldReplaceWithCollection(payloadElement, payloadElement.Value.IsNull, payloadElement.Value.FullTypeName)) { // if the value is an empty string var stringValue = payloadElement.Value.ClrValue as string; if (stringValue != null && stringValue.Length == 0) { // get the element data type. Note that this must succeed based on the checks performed earlier var dataType = ((CollectionDataType)payloadElement.Annotations.OfType<DataTypeAnnotation>().Single().DataType).ElementDataType; // determine whether to return a complex or primitive collection based on the data type ODataPayloadElementCollection collection; if (dataType is PrimitiveDataType) { collection = new PrimitiveCollection(); } else { ExceptionUtilities.Assert(dataType is ComplexDataType, "Data type was neither primitive nor complex"); collection = new ComplexInstanceCollection(); } // return the replacement return payloadElement .ReplaceWith(collection) .WithAnnotations(new CollectionNameAnnotation() { Name = payloadElement.Name }); } } } return replaced; }