/// <summary> /// Serializes the given root element to a binary representation /// </summary> /// <param name="rootElement">The root element</param> /// <param name="encodingName">Optional name of an encoding to use if it is relevant to the current format</param> /// <returns>The serialized value</returns> public override byte[] SerializeToBinary(ODataPayloadElement rootElement, string encodingName) { ExceptionUtilities.CheckArgumentNotNull(rootElement, "rootElement"); ExceptionUtilities.Assert(rootElement.ElementType == ODataPayloadElementType.ComplexInstance, "Only complex instances form values can be serialized as html forms. Type was: {0}", rootElement.ElementType); var complexInstance = (ComplexInstance)rootElement; var queryOpions = new List <KeyValuePair <string, string> >(); foreach (var property in complexInstance.Properties) { string value; if (property.ElementType == ODataPayloadElementType.PrimitiveProperty) { value = this.LiteralConverter.SerializePrimitive(((PrimitiveProperty)property).Value.ClrValue); } else if (property.ElementType == ODataPayloadElementType.ComplexProperty) { value = this.JsonConverter.ConvertToJson(((ComplexProperty)property).Value); } else if (property.ElementType == ODataPayloadElementType.PrimitiveMultiValueProperty) { value = this.JsonConverter.ConvertToJson(((PrimitiveMultiValueProperty)property).Value); } else { ExceptionUtilities.Assert(property.ElementType == ODataPayloadElementType.ComplexMultiValueProperty, "Only primitive, complex, and multi-value properties are supported"); value = this.JsonConverter.ConvertToJson(((ComplexMultiValueProperty)property).Value); } var option = new KeyValuePair <string, string>(property.Name, value); queryOpions.Add(option); } StringBuilder builder = new StringBuilder(); UriHelpers.ConcatenateQueryOptions(builder, queryOpions); var encoding = HttpUtilities.GetEncodingOrDefault(encodingName); return(encoding.GetBytes(builder.ToString())); }
/// <summary> /// Concatenates the given segments and query options into one string using standard delimiters /// </summary> /// <param name="segments">The segments in the order they should appear</param> /// <param name="queryOptions">The query options in the order they should appear</param> /// <returns>The concatenated uri string</returns> protected string ConcatenateSegmentsAndQueryOptions(IEnumerable <ODataUriSegment> segments, IEnumerable <KeyValuePair <string, string> > queryOptions) { ExceptionUtilities.CheckArgumentNotNull(segments, "segments"); ExceptionUtilities.CheckArgumentNotNull(queryOptions, "queryOptions"); StringBuilder builder = new StringBuilder(); if (segments.Any()) { this.ConcatenateSegments(builder, segments); } if (queryOptions.Any()) { builder.Append('?'); UriHelpers.ConcatenateQueryOptions(builder, queryOptions); } return(builder.ToString()); }