Skip to content
This repository was archived by the owner on Apr 10, 2018. It is now read-only.

Commit 11805c4

Browse files
committed
Cleanup
1 parent e4b5a0f commit 11805c4

5 files changed

Lines changed: 63 additions & 51 deletions

File tree

RestSharp.Portable/Deserializers/JsonDeserializer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using Newtonsoft.Json;
1+
using System;
22
using System.IO;
33

4+
using Newtonsoft.Json;
5+
46
namespace RestSharp.Portable.Deserializers
57
{
68
/// <summary>

RestSharp.Portable/Serializers/ISerializer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ namespace RestSharp.Portable.Serializers
77
/// </summary>
88
public interface ISerializer
99
{
10+
/// <summary>
11+
/// Gets or sets the content type produced by the serializer
12+
/// </summary>
13+
MediaTypeHeaderValue ContentType { get; set; }
14+
1015
/// <summary>
1116
/// Serialize the object into a byte array
1217
/// </summary>
1318
/// <param name="obj">Object to serialize</param>
1419
/// <returns>Byte array to send in the request body</returns>
1520
byte[] Serialize(object obj);
16-
/// <summary>
17-
/// Content type produced by the serializer
18-
/// </summary>
19-
MediaTypeHeaderValue ContentType { get; set; }
2021
}
2122
}

RestSharp.Portable/Serializers/JsonSerializer.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ public class JsonSerializer : ISerializer
1212
private static readonly JsonSerializer _defaultJsonSerializer = new JsonSerializer();
1313

1414
/// <summary>
15-
/// Default JSON serializer for AddJsonBody
16-
/// </summary>
17-
internal static JsonSerializer Default { get { return _defaultJsonSerializer; } }
18-
19-
/// <summary>
20-
/// Constructor which initializes this serializer
15+
/// Initializes a new instance of the <see cref="JsonSerializer" /> class.
2116
/// </summary>
2217
public JsonSerializer()
2318
{
@@ -28,11 +23,19 @@ public JsonSerializer()
2823
}
2924

3025
/// <summary>
31-
/// Configure the JsonSerializer
26+
/// Gets or sets the content type produced by the serializer
3227
/// </summary>
33-
/// <param name="serializer">The serializer to configure</param>
34-
protected virtual void ConfigureSerializer(Newtonsoft.Json.JsonSerializer serializer)
28+
/// <remarks>
29+
/// This serializer will return application/json
30+
/// </remarks>
31+
public MediaTypeHeaderValue ContentType { get; set; }
32+
33+
/// <summary>
34+
/// Gets the default JSON serializer for <see cref="RestRequestExtensions.AddJsonBody"/>
35+
/// </summary>
36+
internal static JsonSerializer Default
3537
{
38+
get { return _defaultJsonSerializer; }
3639
}
3740

3841
/// <summary>
@@ -51,11 +54,11 @@ public byte[] Serialize(object obj)
5154
}
5255

5356
/// <summary>
54-
/// Content type produced by the serializer
57+
/// Configure the <see cref="JsonSerializer"/>
5558
/// </summary>
56-
/// <remarks>
57-
/// This serializer will return application/json
58-
/// </remarks>
59-
public MediaTypeHeaderValue ContentType { get; set; }
59+
/// <param name="serializer">The serializer to configure</param>
60+
protected virtual void ConfigureSerializer(Newtonsoft.Json.JsonSerializer serializer)
61+
{
62+
}
6063
}
6164
}

RestSharp.Portable/Serializers/XmlDataContractSerializer.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,11 @@ public class XmlDataContractSerializer : ISerializer
1313
{
1414
private static readonly Encoding _defaultEncoding = new UTF8Encoding(false);
1515
private static readonly XmlDataContractSerializer _defaultXmlDataContractSerializer = new XmlDataContractSerializer();
16+
private MediaTypeHeaderValue _defaultContentType;
17+
private MediaTypeHeaderValue _contentType;
1618

1719
/// <summary>
18-
/// Default XML serializer for AddXmlBody
19-
/// </summary>
20-
internal static XmlDataContractSerializer Default { get { return _defaultXmlDataContractSerializer; } }
21-
22-
/// <summary>
23-
/// The configuration used to create an XML writer
24-
/// </summary>
25-
protected XmlWriterSettings XmlWriterSettings { get; set; }
26-
27-
/// <summary>
28-
/// Constructor
20+
/// Initializes a new instance of the <see cref="XmlDataContractSerializer" /> class.
2921
/// </summary>
3022
public XmlDataContractSerializer()
3123
{
@@ -36,27 +28,7 @@ public XmlDataContractSerializer()
3628
}
3729

3830
/// <summary>
39-
/// Serialize the object into a byte array
40-
/// </summary>
41-
/// <param name="obj">Object to serialize</param>
42-
/// <returns>Byte array to send in the request body</returns>
43-
public byte[] Serialize(object obj)
44-
{
45-
var serializer = CreateSerializer(obj);
46-
var temp = new MemoryStream();
47-
using (var writer = XmlWriter.Create(temp, XmlWriterSettings))
48-
{
49-
serializer.WriteObject(writer, obj);
50-
}
51-
var result = temp.ToArray();
52-
return result;
53-
}
54-
55-
private MediaTypeHeaderValue _defaultContentType;
56-
private MediaTypeHeaderValue _contentType;
57-
58-
/// <summary>
59-
/// Content type produced by the serializer
31+
/// Gets or sets the content type produced by the serializer
6032
/// </summary>
6133
/// <remarks>
6234
/// As long as there is no manually set content type, the content type character set will always reflect the encoding
@@ -85,10 +57,42 @@ public MediaTypeHeaderValue ContentType
8557
}
8658
}
8759

60+
/// <summary>
61+
/// Gets the default XML serializer for AddXmlBody
62+
/// </summary>
63+
internal static XmlDataContractSerializer Default
64+
{
65+
get { return _defaultXmlDataContractSerializer; }
66+
}
67+
68+
/// <summary>
69+
/// Gets or sets the configuration used to create an XML writer
70+
/// </summary>
71+
protected XmlWriterSettings XmlWriterSettings { get; set; }
72+
73+
/// <summary>
74+
/// Serialize the object into a byte array
75+
/// </summary>
76+
/// <param name="obj">Object to serialize</param>
77+
/// <returns>Byte array to send in the request body</returns>
78+
public byte[] Serialize(object obj)
79+
{
80+
var serializer = CreateSerializer(obj);
81+
var temp = new MemoryStream();
82+
using (var writer = XmlWriter.Create(temp, XmlWriterSettings))
83+
{
84+
serializer.WriteObject(writer, obj);
85+
}
86+
87+
var result = temp.ToArray();
88+
return result;
89+
}
90+
8891
/// <summary>
8992
/// Create a new data contract serializer
9093
/// </summary>
9194
/// <param name="obj">The object to create the serializer for</param>
95+
/// <returns>A new instance of the serializer for the given instance.</returns>
9296
protected virtual DataContractSerializer CreateSerializer(object obj)
9397
{
9498
return new DataContractSerializer(obj.GetType());

Settings.StyleCop

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<CollectionProperty Name="RecognizedWords">
55
<Value>deserialized</Value>
66
<Value>deserializer</Value>
7+
<Value>json</Value>
8+
<Value>Json</Value>
79
</CollectionProperty>
810
</GlobalSettings>
911
<Analyzers>

0 commit comments

Comments
 (0)