/// <summary>
        /// Initializes a new instance of the <see cref="DefaultRestierSerializerProvider" /> class.
        /// </summary>
        /// <param name="rootContainer">The container to get the service.</param>
        /// <param name="payloadValueConverter">The OData payload value converter to use.</param>
        public DefaultRestierSerializerProvider(IServiceProvider rootContainer, ODataPayloadValueConverter payloadValueConverter)
            : base(rootContainer)
        {
            Ensure.NotNull(rootContainer, nameof(rootContainer));
            Ensure.NotNull(payloadValueConverter, nameof(payloadValueConverter));

            this.resourceSetSerializer = new RestierResourceSetSerializer(this);
            this.primitiveSerializer   = new RestierPrimitiveSerializer(payloadValueConverter);
            this.rawSerializer         = new RestierRawSerializer(payloadValueConverter);
            this.resourceSerializer    = new RestierResourceSerializer(this);
            this.collectionSerializer  = new RestierCollectionSerializer(this);
            this.enumSerializer        = new RestierEnumSerializer(this);
        }
        /// <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);
            }
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RestierPrimitiveSerializer"/> class.
 /// </summary>
 /// <param name="payloadValueConverter"></param>
 public RestierPrimitiveSerializer(ODataPayloadValueConverter payloadValueConverter)
 {
     Ensure.NotNull(payloadValueConverter, nameof(payloadValueConverter));
     this.payloadValueConverter = payloadValueConverter;
 }