/// <summary> /// Replaces the given element, storing the original in an annotation and copying the other annotations over. /// </summary> /// <typeparam name="TReplace">The type of the replacement element</typeparam> /// <param name="toReplace">The original element</param> /// <param name="replacement">The replacement element</param> /// <returns>The replacement element with the original in an annotation</returns> public static TReplace ReplaceWith <TReplace>(this ODataPayloadElement toReplace, TReplace replacement) where TReplace : ODataPayloadElement { return(replacement.WithAnnotations(toReplace.Annotations.Select(a => a.Clone()).Concat(new ReplacedElementAnnotation() { Original = toReplace }))); }
/// <summary> /// Adds the given element to the collection. Throws an invalid operation exception if the type does not match. /// </summary> /// <param name="element">The element to add</param> public override void Add(ODataPayloadElement element) { Type itemType = typeof(TItem); Type newElementType = element.GetType(); ExceptionUtilities.Assert( newElementType.Equals(itemType) || (itemType.IsAbstract() && itemType.IsAssignableFrom(newElementType)), "Cannot add element of type '" + newElementType.Name + "' to a collection of type '" + itemType.Name + "'"); this.Add(element as TItem); }
/// <summary> /// Initializes a new instance of the NavigationPropertyInstance class /// Will automatically wrap entity sets and entity instances in expanded links /// </summary> /// <param name="name">The property name</param> /// <param name="value">The property value</param> public NavigationPropertyInstance(string name, ODataPayloadElement value) : base(name) { if (value != null) { if (value.ElementType == ODataPayloadElementType.EntityInstance || value.ElementType == ODataPayloadElementType.EntitySetInstance) { value = new ExpandedLink() { ExpandedElement = value }; } } this.Value = value; }
/// <summary> /// Initializes a new instance of the ODataPayloadBody class /// </summary> /// <param name="serializedValue">The serialized binary representation of the payload</param> /// <param name="rootElement">The odata payload element representation of the payload</param> public ODataPayloadBody(byte[] serializedValue, ODataPayloadElement rootElement) { this.SerializedValue = serializedValue; this.RootElement = rootElement; }
/// <summary> /// Deserializes the given HTTP payload a error payload using the default encoding or returns null /// </summary> /// <param name="deserializer">The deserializer to extend</param> /// <param name="serialized">Bytes of the Payload</param> /// <param name="errorPayload">Error payload that is found</param> /// <returns>True if it finds and error, false if not</returns> public static bool TryDeserializeErrorPayload(this IPayloadErrorDeserializer deserializer, byte[] serialized, out ODataPayloadElement errorPayload) { ExceptionUtilities.CheckArgumentNotNull(deserializer, "deserializer"); return(deserializer.TryDeserializeErrorPayload(serialized, null, out errorPayload)); }
/// <summary> /// Serializes the given payload element into a form ready to be sent over HTTP using the default encoding. /// </summary> /// <param name="serializer">The serializer to extend</param> /// <param name="rootElement">The root payload element to serialize</param> /// <returns>The binary representation of the payload for this format, ready to be sent over HTTP</returns> public static byte[] SerializeToBinary(this IPayloadSerializer serializer, ODataPayloadElement rootElement) { ExceptionUtilities.CheckArgumentNotNull(serializer, "serializer"); return(serializer.SerializeToBinary(rootElement, null)); }
public static TElement DeserializeAndCast <TElement>(this IHttpRequest request, IProtocolFormatStrategySelector selector) where TElement : ODataPayloadElement { ExceptionUtilities.CheckArgumentNotNull(request, "request"); var odataRequest = request as ODataRequest; if (odataRequest != null) { ExceptionUtilities.CheckObjectNotNull(odataRequest.Body, "OData request body unexpectedly null"); var rootElement = odataRequest.Body.RootElement; var afterCast = rootElement as TElement; ExceptionUtilities.CheckObjectNotNull(afterCast, "Root element was of unexpected type '{0}'. Expected '{1}'", rootElement.ElementType, ODataPayloadElement.GetElementType <TElement>()); return(afterCast); } else { string contentType; ExceptionUtilities.Assert(request.TryGetHeaderValueIgnoreHeaderCase(HttpHeaders.ContentType, out contentType), "Cannot deserialize request. 'Content-Type' header not found"); return(DeserializeAndCast <TElement>(selector, null, contentType, request.GetRequestBody())); } }
/// <summary> /// Throws an invalid operation exception. By definition, one cannot add elements to an empty collection /// </summary> /// <param name="element">The element to add</param> public override void Add(ODataPayloadElement element) { throw new TaupoInvalidOperationException("Cannot add elements to an empty un-typed collection"); }
/// <summary> /// Initializes a new instance of the ODataPayloadElement class. /// Infers the ElementType using ODataPayloadElement.GetElementType with the current instance's type /// </summary> protected ODataPayloadElement() { this.ElementType = ODataPayloadElement.GetElementType(this.GetType()); this.Annotations = new List <ODataPayloadElementAnnotation>(); }
/// <summary> /// Initializes a new instance of the NavigationPropertyInstance class /// </summary> /// <param name="name">The property name</param> /// <param name="value">The navigation link value</param> /// <param name="associationLink">The ralationship link value</param> public NavigationPropertyInstance(string name, ODataPayloadElement value, DeferredLink associationLink) : this(name, value) { this.AssociationLink = associationLink; }
/// <summary> /// Initializes a new instance of the ExpandedLink class /// </summary> /// <param name="expanded">The expanded element</param> public ExpandedLink(ODataPayloadElement expanded) : base() { this.ExpandedElement = expanded; }
/// <summary> /// Gets the xml name to use for generating items in a collection based on the given element's annotations /// </summary> /// <param name="element">The element to get a name for</param> /// <returns>The xml name to use, or null if one cannot be determined</returns> public static XName GetCollectionItemElementName(this ODataPayloadElement element) { return(element.Annotations.OfType <CollectionItemElementNameAnnotation>().Select(a => XName.Get(a.LocalName, a.NamespaceName)).SingleOrDefault()); }