summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/ProtocolMessageSerializer.cs
blob: a9c92bce8f589da0b7190bcad4016c4ac2d68663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Linq;

namespace DotNetOAuth {
	/// <summary>
	/// Serializes/deserializes OAuth messages for/from transit.
	/// </summary>
	internal class ProtocolMessageSerializer<T> where T : IProtocolMessage {
		DataContractSerializer serializer;
		readonly XName rootElement = XName.Get("root", Protocol.DataContractNamespace);

		internal ProtocolMessageSerializer() {
			serializer = new DataContractSerializer(typeof(T), rootElement.LocalName, rootElement.NamespaceName);
		}

		/// <summary>
		/// Reads the data from a message instance and returns a series of name=value pairs for the fields that must be included in the message.
		/// </summary>
		/// <param name="message">The message to be serialized.</param>
		internal IDictionary<string, string> Serialize(T message) {
			if (message == null) throw new ArgumentNullException("message");

			var fields = new Dictionary<string, string>();
			Serialize(fields, message);
			return fields;
		}

		internal void Serialize(IDictionary<string, string> fields, T message) {
			if (fields == null) throw new ArgumentNullException("fields");
			if (message == null) throw new ArgumentNullException("message");

			message.EnsureValidMessage();
			using (XmlWriter writer = DictionaryXmlWriter.Create(fields)) {
				serializer.WriteObjectContent(writer, message);
			}
		}

		/// <summary>
		/// Reads name=value pairs into an OAuth message.
		/// </summary>
		/// <param name="fields">The name=value pairs that were read in from the transport.</param>
		internal T Deserialize(IDictionary<string, string> fields) {
			if (fields == null) throw new ArgumentNullException("fields");

			var reader = DictionaryXmlReader.Create(rootElement, fields);
			T result = (T)serializer.ReadObject(reader, false);
			result.EnsureValidMessage();
			return result;
		}
	}
}