/// <summary>
        /// Writes a primitive value.
        /// </summary>
        /// <param name="value">The value to write.</param>
        /// <param name="actualTypeReference">The actual type reference of the primitive value.</param>
        /// <param name="expectedTypeReference">The expected type reference of the primitive value.</param>
        public virtual void WritePrimitiveValue(
            object value,
            IEdmTypeReference actualTypeReference,
            IEdmTypeReference expectedTypeReference)
        {
            Debug.Assert(value != null, "value != null");

            if (actualTypeReference == null)
            {
                // Try convert primitive values from their actual CLR types to their underlying CLR types.
                value = this.Model.ConvertToUnderlyingTypeIfUIntValue(value, expectedTypeReference);
                actualTypeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(value.GetType());
            }

            ODataPayloadValueConverter converter = this.JsonLightOutputContext.PayloadValueConverter;

            // Skip validation if user has set custom PayloadValueConverter
            if (expectedTypeReference != null && converter.GetType() == typeof(ODataPayloadValueConverter))
            {
                this.WriterValidator.ValidateIsExpectedPrimitiveType(value, (IEdmPrimitiveTypeReference)actualTypeReference, expectedTypeReference);
            }

            value = converter.ConvertToPayloadValue(value, expectedTypeReference);

            if (actualTypeReference != null && actualTypeReference.IsSpatial())
            {
                PrimitiveConverter.Instance.WriteJsonLight(value, this.JsonWriter);
            }
            else
            {
                this.JsonWriter.WritePrimitiveValue(value);
            }
        }
Esempio n. 2
0
        internal static object ConvertToPayloadValue(object value, ODataSerializerContext writeContext, ODataPayloadValueConverter payloadValueConverter)
        {
            Ensure.NotNull(writeContext, nameof(writeContext));

            IEdmTypeReference edmTypeReference = null;

            if (writeContext.Path != null)
            {
                // Try to get the EDM type of the value from the path.
                var edmType = writeContext.Path.EdmType as IEdmPrimitiveType;
                if (edmType != null)
                {
                    // Just created to call the payload value converter.
                    edmTypeReference = new EdmPrimitiveTypeReference(edmType, true /*isNullable*/);
                }
            }

            return(payloadValueConverter.ConvertToPayloadValue(value, edmTypeReference));
        }
        /// <summary>
        /// Asynchronously writes a primitive value.
        /// </summary>
        /// <param name="value">The value to write.</param>
        /// <param name="actualTypeReference">The actual type reference of the primitive value.</param>
        /// <param name="expectedTypeReference">The expected type reference of the primitive value.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        public virtual async Task WritePrimitiveValueAsync(
            object value,
            IEdmTypeReference actualTypeReference,
            IEdmTypeReference expectedTypeReference)
        {
            Debug.Assert(value != null, "value != null");

            if (actualTypeReference == null)
            {
                // Try convert primitive values from their actual CLR types to their underlying CLR types.
                value = this.Model.ConvertToUnderlyingTypeIfUIntValue(value, expectedTypeReference);
                actualTypeReference = EdmLibraryExtensions.GetPrimitiveTypeReference(value.GetType());
            }

            ODataPayloadValueConverter converter = this.JsonLightOutputContext.PayloadValueConverter;

            // Skip validation if user has set custom PayloadValueConverter
            if (expectedTypeReference != null && converter.GetType() == typeof(ODataPayloadValueConverter))
            {
                this.WriterValidator.ValidateIsExpectedPrimitiveType(value, (IEdmPrimitiveTypeReference)actualTypeReference, expectedTypeReference);
            }

            value = converter.ConvertToPayloadValue(value, expectedTypeReference);

            if (actualTypeReference != null && actualTypeReference.IsSpatial())
            {
                // TODO: Implement asynchronous handling of spatial types in a separate PR
                await TaskUtils.GetTaskForSynchronousOperation(() =>
                {
                    PrimitiveConverter.Instance.WriteJsonLight(value, this.JsonWriter);
                }).ConfigureAwait(false);
            }
            else
            {
                await this.AsynchronousJsonWriter.WritePrimitiveValueAsync(value).ConfigureAwait(false);
            }
        }