8000 fixes #643 add xmlserializer cache by Styxxy · Pull Request #646 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fixes #643 add xmlserializer cache #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Refit/XmlContentSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Http;
using System.Text;
Expand All @@ -12,6 +13,7 @@ namespace Refit
public class XmlContentSerializer : IContentSerializer
{
private readonly XmlContentSerializerSettings settings;
private readonly ConcurrentDictionary<Type, XmlSerializer> serializerCache = new ConcurrentDictionary<Type, XmlSerializer>();

public XmlContentSerializer() : this(new XmlContentSerializerSettings())
{
Expand All @@ -24,8 +26,8 @@ public XmlContentSerializer(XmlContentSerializerSettings settings)

public Task<HttpContent> SerializeAsync<T>(T item)
{
var xmlSerializer = new XmlSerializer(item.GetType(), settings.XmlAttributeOverrides);

var xmlSerializer = serializerCache.GetOrAdd(item.GetType(), t => new XmlSerializer(t, settings.XmlAttributeOverrides));
using (var stream = new MemoryStream())
{
using (var writer = XmlWriter.Create(stream, settings.XmlReaderWriterSettings.WriterSettings))
Expand All @@ -41,8 +43,8 @@ public Task<HttpContent> SerializeAsync<T>(T item)

public async Task<T> DeserializeAsync<T>(HttpContent content)
{
var xmlSerializer = new XmlSerializer(typeof(T), settings.XmlAttributeOverrides);

var xmlSerializer = serializerCache.GetOrAdd(typeof(T), t => new XmlSerializer(t, settings.XmlAttributeOverrides));
using (var input = new StringReader(await content.ReadAsStringAsync().ConfigureAwait(false)))
using (var reader = XmlReader.Create(input, settings.XmlReaderWriterSettings.ReaderSettings))
return (T)xmlSerializer.Deserialize(reader);
Expand Down
0