Skip to content

Commit

Permalink
DateTimeOffset support
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders Lyman committed Dec 13, 2021
1 parent 5578286 commit fd08750
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ LocalTestRun.testrunconfig
.vs/
.git/
.github/
packages/
57 changes: 57 additions & 0 deletions SharpSerializer.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,64 @@ public void BinSerial_ShouldSerializeGuid()
Assert.AreEqual(parent.Guid, loaded.Guid, "same guid");
}
#endregion

#region Serial_DateTimeOffset with helpers
/// <summary>
/// Local testclass to be serialized
/// </summary>
public class ClassWithDateTimeOffset
{
public DateTimeOffset Date { get; set; }
}

[TestMethod]
public void XmlSerial_ShouldSerializeDateTimeOffset()
{
var parent = new ClassWithDateTimeOffset
{
Date = DateTimeOffset.Now,
};

var stream = new MemoryStream();
var settings = new SharpSerializerXmlSettings();
var serializer = new SharpSerializer(settings);

serializer.Serialize(parent, stream);

stream.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(stream);
System.Console.WriteLine(doc.InnerXml);

serializer = new SharpSerializer(settings);
stream.Position = 0;
ClassWithDateTimeOffset loaded = serializer.Deserialize(stream) as ClassWithDateTimeOffset;

Assert.AreEqual(parent.Date, loaded.Date, "same date");
}

[TestMethod]
public void BinSerial_ShouldSerializeDateTimeOffset()
{
var parent = new ClassWithDateTimeOffset
{
Date = DateTimeOffset.Now,
};

var stream = new MemoryStream();
var settings = new SharpSerializerBinarySettings(BinarySerializationMode.SizeOptimized);
var serializer = new SharpSerializer(settings);

serializer.Serialize(parent, stream);


serializer = new SharpSerializer(settings);
stream.Position = 0;
ClassWithDateTimeOffset loaded = serializer.Deserialize(stream) as ClassWithDateTimeOffset;

Assert.AreEqual(parent.Date, loaded.Date, "same date");
}
#endregion

#region BinSerial
[TestMethod]
Expand Down
6 changes: 6 additions & 0 deletions SharpSerializer/Advanced/SimpleValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public string ConvertToString(object value)
return ((DateTime)value).ToString("O");
}

if (value is DateTimeOffset)
{
return ((DateTimeOffset)value).ToString("O");
}

// Array of byte
if (value.GetType() == typeof(byte[]))
{
Expand Down Expand Up @@ -127,6 +132,7 @@ public object ConvertFromString(string text, Type type)
}

if (type == typeof (DateTime)) return DateTime.Parse(text, _cultureInfo, DateTimeStyles.RoundtripKind);
if (type == typeof (DateTimeOffset)) return DateTimeOffset.Parse(text, _cultureInfo, DateTimeStyles.RoundtripKind);
if (type == typeof (Decimal)) return Convert.ToDecimal(text, _cultureInfo);
if (type == typeof (Double)) return Convert.ToDouble(text, _cultureInfo);
if (type == typeof (Int16)) return Convert.ToInt16(text, _cultureInfo);
Expand Down
4 changes: 3 additions & 1 deletion SharpSerializer/Core/Binary/BinaryReaderTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace Polenter.Serialization.Core.Binary
Expand Down Expand Up @@ -110,7 +111,8 @@ private static object readValueCore(Type type, BinaryReader reader)
if (type == typeof (Byte)) return reader.ReadByte();
if (type == typeof (Char)) return reader.ReadChar();
if (type == typeof (DateTime)) return new DateTime(reader.ReadInt64());
if (type == typeof(Guid)) return new Guid(reader.ReadBytes(16));
if (type == typeof (DateTimeOffset)) return DateTimeOffset.ParseExact(reader.ReadString(), "O", new DateTimeFormatInfo());
if (type == typeof (Guid)) return new Guid(reader.ReadBytes(16));
if (type == typeof (Decimal)) return reader.ReadDecimal();
if (type == typeof (Double)) return reader.ReadDouble();
if (type == typeof (Int16)) return reader.ReadInt16();
Expand Down
5 changes: 5 additions & 0 deletions SharpSerializer/Core/Binary/BinaryWriterTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ private static void writeValueCore(object value, BinaryWriter writer)
writer.Write(((DateTime) value).Ticks);
return;
}
if (type == typeof(DateTimeOffset))
{
writer.Write(((DateTimeOffset)value).ToString("O"));
return;
}
if (type == typeof(Guid))
{
writer.Write(((Guid)value).ToByteArray());
Expand Down
4 changes: 4 additions & 0 deletions SharpSerializer/Core/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public static bool IsSimple(Type type)
{
return true;
}
if (type == typeof(DateTimeOffset))
{
return true;
}
if (type == typeof (TimeSpan))
{
return true;
Expand Down

0 comments on commit fd08750

Please sign in to comment.