Example #1
0
        /// <summary>Serializes the specified stream.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="obj">The object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override Task Serialize(Stream stream, object obj, IMediaSerializerOptions options)
        {
            var version = string.Empty;

            var context = this.contextResolver?.GetContext();

            context?.TryGetItem("openapi_version", out version);

            if (obj is OpenApiDocument document && document != null)
            {
                if (version == "2")
                {
                    document.Serialize(
                        stream: stream,
                        specVersion: OpenApiSpecVersion.OpenApi2_0,
                        format: OpenApiFormat.Yaml);
                }
                else
                {
                    document.Serialize(
                        stream: stream,
                        specVersion: OpenApiSpecVersion.OpenApi3_0,
                        format: OpenApiFormat.Yaml);
                }
            }

            return(Task.CompletedTask);
        }
Example #2
0
        /// <summary>Serializes the specified stream.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="obj">The object.</param>
        /// <param name="options">The options.</param>
        protected override async Task Serialize(Stream stream, object obj, IMediaSerializerOptions options)
        {
            if (obj != null)
            {
                var settings = this.jsonFormattingConfiguration?.SerializerOptions;

                await JsonSerializer.SerializeAsync(stream, obj, settings).ConfigureAwait(false);
            }
        }
Example #3
0
        /// <summary>Reads the type.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="objType">Type of the object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public async Task <object> ReadType(Stream stream, Type objType, IMediaSerializerOptions options)
        {
            string obj = null;

            using (var reader = new StreamReader(stream, options?.Encoding, true, 1024))
            {
                obj = await reader.ReadToEndAsync().ConfigureAwait(false);
            }

            return(obj);
        }
Example #4
0
        /// <summary>Deserializes the specified stream.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="objType">Type of the object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override Task <object> Deserialize(Stream stream, Type objType, IMediaSerializerOptions options)
        {
            object obj        = null;
            var    serializer = new XmlSerializer(objType);
            var    settings   = xmlFormattingConfiguration?.ReaderSerializerSettings;

            using (XmlReader reader = XmlReader.Create(stream, settings))
            {
                obj = serializer.Deserialize(reader);
            }

            return(Task.FromResult(obj));
        }
Example #5
0
        /// <summary>Serializes the specified stream.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="obj">The object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override Task Serialize(Stream stream, object obj, IMediaSerializerOptions options)
        {
            if (obj != null)
            {
                var serializer = new XmlSerializer(obj.GetType());
                var settings   = xmlFormattingConfiguration?.WriterSerializerSettings;

                using (var writer = XmlWriter.Create(stream, settings))
                {
                    serializer.Serialize(writer, obj);
                }
            }

            return(Task.CompletedTask);
        }
Example #6
0
        /// <summary>Reads the type.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="objType">Type of the object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public virtual async Task <object> ReadType(Stream stream, Type objType, IMediaSerializerOptions options)
        {
            object obj = null;

            using (var reader = new StreamReader(stream, true))
            {
                var result = await PreSerializationReadType(stream, objType, options).ConfigureAwait(false);

                if (result?.HasRead ?? false)
                {
                    return(result.ObjectResult);
                }

                obj = await Deserialize(
                    stream : stream,
                    objType : objType,
                    options : options).ConfigureAwait(false);
            }

            return(obj);
        }
Example #7
0
        /// <summary>Writes the type.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="obj">The object.</param>
        /// <param name="options">The options.</param>
        /// <param name="preWriteCallback">The pre write callback.</param>
        /// <returns></returns>
        public async Task <long> WriteType(Stream stream, object obj, IMediaSerializerOptions options, Action <long> preWriteCallback = null)
        {
            long length = 0;

            if (obj != null)
            {
                using (var ms = new MemoryStream())
                    using (var writer = new StreamWriter(ms, options?.Encoding, 1024))
                    {
                        writer.Write(obj.ToString());
                        writer.Flush();
                        length = ms.Length;
                        ms.Seek(0, SeekOrigin.Begin);

                        preWriteCallback?.Invoke(length);

                        await ms.CopyToAsync(stream).ConfigureAwait(false);
                    }
            }

            return(length);
        }
Example #8
0
        /// <summary>Writes the type.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="obj">The object.</param>
        /// <param name="options">The options.</param>
        /// <param name="preWriteCallback">The pre write callback.</param>
        /// <returns></returns>
        public virtual async Task <long> WriteType(Stream stream, object obj, IMediaSerializerOptions options, Action <long> preWriteCallback = null)
        {
            long length = 0;

            if (obj != null)
            {
                using (var ms = new MemoryStream())
                {
                    await Serialize(
                        stream : ms,
                        obj : obj,
                        options : options).ConfigureAwait(false);

                    length = ms.Length;
                    ms.Seek(0, SeekOrigin.Begin);

                    preWriteCallback?.Invoke(length);

                    await ms.CopyToAsync(stream).ConfigureAwait(false);
                }
            }

            return(length);
        }
Example #9
0
 /// <summary>Serializes the specified stream.</summary>
 /// <param name="stream">The stream.</param>
 /// <param name="obj">The object.</param>
 /// <param name="options">The options.</param>
 /// <returns></returns>
 protected abstract Task Serialize(Stream stream, object obj, IMediaSerializerOptions options);
Example #10
0
 /// <summary>Deserializes the specified stream.</summary>
 /// <param name="stream">The stream.</param>
 /// <param name="objType">Type of the object.</param>
 /// <param name="options">The options.</param>
 /// <returns></returns>
 protected abstract Task <object> Deserialize(Stream stream, Type objType, IMediaSerializerOptions options);
Example #11
0
        /// <summary>Pres the type of the serialization read.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="objType">Type of the object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected virtual Task <PreSerializationResult> PreSerializationReadType(Stream stream, Type objType, IMediaSerializerOptions options)
        {
            //if (objType.IsGenericType && objType.GetGenericTypeDefinition() == typeof(ArraySegment<>))
            //{
            //    var typeObject = objType.GetGenericArguments()[0];
            //    var bufferArrayType = typeObject.MakeArrayType();

            //    var bufferobj = await Deserialize(
            //        stream: stream,
            //        objType: bufferArrayType,
            //        options: options).ConfigureAwait(false);

            //    var obj = typeof(ArraySegment<>)
            //        .MakeGenericType(typeObject)
            //        .GetConstructor(new Type[] { typeObject.MakeArrayType() })
            //        .Invoke(new object[] { bufferobj });


            //    return PreSerializationResult.Handled(obj);
            //}

            return(Task.FromResult(PreSerializationResult.NotHandled()));
        }
Example #12
0
 /// <summary>Serializes the specified stream.</summary>
 /// <param name="stream">The stream.</param>
 /// <param name="obj">The object.</param>
 /// <param name="options">The options.</param>
 /// <returns></returns>
 /// <exception cref="NotSupportedException"></exception>
 protected override Task Serialize(Stream stream, object obj, IMediaSerializerOptions options)
 {
     throw new NotSupportedException($"{nameof(DeepSleepFormUrlEncodedMediaSerializer)} does not support writing.");
 }
Example #13
0
        /// <summary>Deserializes the specified stream.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="objType">Type of the object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override async Task <object> Deserialize(Stream stream, Type objType, IMediaSerializerOptions options)
        {
            string   data         = null;
            Encoding readEncoding = Encoding.Default;

            using (var reader = new StreamReader(stream, true))
            {
                data = await reader.ReadToEndAsync().ConfigureAwait(false);

                readEncoding = reader.CurrentEncoding;
            }

            if (readEncoding.EncodingName != Encoding.Default.EncodingName)
            {
                data = Encoding.Default
                       .GetString(Encoding.Conver‌​t(readEncoding, Encoding.Default, readEncoding.GetBytes(data)));
            }

            var obj = await this.serializer.Deserialize(data, objType).ConfigureAwait(false);

            return(obj);
        }
Example #14
0
 /// <summary>Deserializes the specified stream.</summary>
 /// <param name="stream">The stream.</param>
 /// <param name="objType">Type of the object.</param>
 /// <param name="options">The options.</param>
 /// <returns></returns>
 protected override Task <object> Deserialize(Stream stream, Type objType, IMediaSerializerOptions options)
 {
     return(Task.FromResult(null as object));
 }
Example #15
0
        /// <summary>Deserializes the specified stream.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="objType">Type of the object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override async Task <object> Deserialize(Stream stream, Type objType, IMediaSerializerOptions options)
        {
            var settings = this.jsonFormattingConfiguration?.SerializerOptions;

            return(await JsonSerializer.DeserializeAsync(stream, objType, settings).ConfigureAwait(false));
        }
Example #16
0
        /// <summary>Deserializes the specified stream.</summary>
        /// <param name="stream">The stream.</param>
        /// <param name="objType">Type of the object.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override async Task <object> Deserialize(Stream stream, Type objType, IMediaSerializerOptions options)
        {
            MultipartHttpRequest multipart;

            multipart = await this.multipartStreamReader.ReadAsMultipart(stream).ConfigureAwait(false);

            if (multipart == null)
            {
                return(multipart);
            }

            if (objType.IsAssignableFrom(multipart.GetType()) || objType.IsSubclassOf(typeof(MultipartHttpRequest)))
            {
                return(multipart);
            }

            var simplePartNames = multipart.Sections
                                  .Where(s => s.IsFileSection() == false)
                                  .Select(s => s.Name)
                                  .Distinct()
                                  .ToList();

            var formUrlEncoded = string.Empty;

            foreach (var partName in simplePartNames)
            {
                var values = multipart.Sections
                             .Where(s => s.Name == partName)
                             .Select(s => s.Value)
                             .ToList();

                formUrlEncoded += $"{UrlEncode(partName)}={UrlEncode(string.Join(",", values))}&";
            }

            var filePartNames = multipart.Sections
                                .Where(s => s.IsFileSection() == true)
                                .Select(s => s.Name)
                                .Distinct()
                                .ToList();

            foreach (var partName in filePartNames)
            {
                var fileSections = multipart.Sections
                                   .Where(s => s.Name == partName)
                                   .ToList();

                for (int i = 0; i < fileSections.Count; i++)
                {
                    var section = fileSections[i];
                    formUrlEncoded += $"{UrlEncode(partName)}[{i}].{nameof(section.TempFileName)}={UrlEncode(section.TempFileName)}&";
                    formUrlEncoded += $"{UrlEncode(partName)}[{i}].{nameof(section.ContentType)}={UrlEncode(section.ContentType)}&";
                    formUrlEncoded += $"{UrlEncode(partName)}[{i}].{nameof(section.ContentDisposition)}={UrlEncode(section.ContentDisposition)}&";
                }
            }


            if (formUrlEncoded.EndsWith("&"))
            {
                formUrlEncoded = formUrlEncoded.Substring(0, formUrlEncoded.Length - 1);
            }

            var customMultipart = await this.formUrlEncodedObjectSerializer.Deserialize(formUrlEncoded, objType, false).ConfigureAwait(false);

            return(customMultipart);
        }