Пример #1
0
        private Entity ReadEntity(string mediaType, TextReader input)
        {
            bool isJson = this.defaultFormat == Formats.Json;

            if (mediaType != null)
            {
                if (mediaType.Contains("json"))
                {
                    isJson = true;
                }
                else if (mediaType.Contains("xml"))
                {
                    isJson = false;
                }
            }

            if (isJson)
            {
                var serializer = new SirenSerializer();
                var entity     = serializer.Deserialize(input);
                return(entity);
            }
            else
            {
                var serializer = new DataContractSerializer(typeof(Entity), new[] { typeof(DBNull) });
                using (var writer = XmlReader.Create(input, new XmlReaderSettings {
                    CloseInput = false
                }))
                {
                    var entity = (Entity)serializer.ReadObject(writer);
                    return(entity);
                }
            }
        }
Пример #2
0
        private void WriteEntity(Entity entity, string mediaType, TextWriter output)
        {
            bool isJson = this.defaultFormat == Formats.Json;

            if (mediaType != null)
            {
                if (mediaType.Contains("json"))
                {
                    isJson = true;
                }
                else if (mediaType.Contains("xml"))
                {
                    isJson = false;
                }
            }

            if (isJson)
            {
                var serializer = new SirenSerializer();
                serializer.Serialize(entity, output);
            }
            else
            {
                var serializer = new DataContractSerializer(typeof(Entity), new[] { typeof(DBNull) });
                using (var writer = XmlWriter.Create(output, new XmlWriterSettings {
                    CloseOutput = false
                }))
                {
                    serializer.WriteObject(writer, entity);
                    writer.Flush();
                }
            }

            output.Flush();
        }