public async Task WriteParametersAsync(SerializerParameters parameters, CancellationToken cancellationToken = new CancellationToken())
        {
            BlockBlob.Properties.ContentType     = parameters?.ContentType;
            BlockBlob.Properties.ContentEncoding = parameters?.ContentEncoding;

            await BlockBlob.SetPropertiesAsync();
        }
Ejemplo n.º 2
0
        public Task <object> DeserializeAsync(Stream stream, SerializerParameters parameters, CancellationToken cancellationToken = new CancellationToken())
        {
            Encoding encoding;

            CheckParameters(parameters, out encoding);

            using (var textReader = new StreamReader(stream, encoding, true, BufferSize, true))
                using (var reader = new JsonTextReader(textReader))
                {
                    var     serializer = JsonSerializer.Create();
                    dynamic item       = serializer.Deserialize(reader);
                    return(Task.FromResult(item));
                }
        }
        protected virtual void CheckParameters(SerializerParameters parameters, out Encoding encoding)
        {
            if (parameters != null)
            {
                if (parameters.ContentEncoding != null && parameters.ContentEncoding != "identity")
                {
                    throw new NotSupportedException($"Do not support Content-Encoding: {parameters.ContentEncoding}");
                }

                if (!string.IsNullOrEmpty(parameters.ContentType))
                {
                    MediaTypeHeaderValue contentType;
                    if (!MediaTypeHeaderValue.TryParse(parameters.ContentType, out contentType))
                    {
                        throw new FormatException($"Cannot parse Content-Type: {parameters.ContentType}");
                    }

                    if (contentType.MediaType != "application/json")
                    {
                        throw new NotSupportedException($"Invalid media type: {contentType.MediaType}");
                    }

                    if (!string.IsNullOrEmpty(contentType.CharSet))
                    {
                        encoding = Encoding.GetEncoding(contentType.CharSet, Encoding.EncoderFallback, Encoding.DecoderFallback);
                    }
                    else
                    {
                        encoding = Encoding;
                    }

                    return;
                }
            }
            encoding = Encoding;
        }