1
//-----------------------------------------------------------------------
2 // <copyright file="TimespanSecondsEncoder.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.Extensions
.ProviderAuthenticationPolicy
{
9 using System
.Globalization
;
10 using DotNetOpenAuth
.Messaging
.Reflection
;
13 /// Encodes and decodes the <see cref="TimeSpan"/> as an integer of total seconds.
15 internal class TimespanSecondsEncoder
: IMessagePartEncoder
{
16 #region IMessagePartEncoder Members
19 /// Encodes the specified value.
21 /// <param name="value">The value. Guaranteed to never be null.</param>
23 /// The <paramref name="value"/> in string form, ready for message transport.
25 public string Encode(object value) {
26 TimeSpan
? timeSpan
= value as TimeSpan
?;
27 if (timeSpan
.HasValue
) {
28 return timeSpan
.Value
.TotalSeconds
.ToString(CultureInfo
.InvariantCulture
);
35 /// Decodes the specified value.
37 /// <param name="value">The string value carried by the transport. Guaranteed to never be null, although it may be empty.</param>
39 /// The deserialized form of the given string.
41 /// <exception cref="FormatException">Thrown when the string value given cannot be decoded into the required object type.</exception>
42 public object Decode(string value) {
43 return TimeSpan
.FromSeconds(double.Parse(value, CultureInfo
.InvariantCulture
));