Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit bca706f

Browse files
committed
Use JSV for to or from string ValueType conversions
1 parent 86a8b67 commit bca706f

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ App_Data/
4242
*.resharper.user
4343
*.suo
4444
*.user
45+
.vs/
4546

4647
NuGet/
4748
NuGet.Pcl/

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ public static object ConvertTo(this object from, Type type)
6868
private static object ChangeValueType(object from, Type type)
6969
{
7070
var strValue = from as string;
71-
if (type == typeof(DateTime) && strValue != null)
72-
return DateTimeSerializer.ParseShortestXsdDateTime(strValue);
73-
if (from is DateTime)
74-
return DateTimeSerializer.ToShortestXsdDateTimeString((DateTime)from);
71+
if (strValue != null)
72+
return TypeSerializer.DeserializeFromString(strValue, type);
73+
74+
if (type == typeof(string))
75+
return from.ToJsv();
7576

7677
return Convert.ChangeType(from, type, provider: null);
7778
}

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ public void Does_convert_to_ValueType()
585585
Assert.That("1.1".ConvertTo(typeof(decimal)), Is.EqualTo(1.1M));
586586

587587
Assert.That("2001-01-01".ConvertTo<DateTime>(), Is.EqualTo(new DateTime(2001, 01, 01)));
588+
Assert.That("98ece8400be4452eb6ad7c3a4404f119".ConvertTo<Guid>(), Is.EqualTo(new Guid("98ece8400be4452eb6ad7c3a4404f119")));
588589
}
589590

590591
[Test]
@@ -595,8 +596,9 @@ public void Does_convert_from_ValueType_to_strings()
595596
Assert.That(1.1f.ConvertTo(typeof(string)), Is.EqualTo("1.1"));
596597
Assert.That(1.1d.ConvertTo(typeof(string)), Is.EqualTo("1.1"));
597598
Assert.That(1.1M.ConvertTo(typeof(string)), Is.EqualTo("1.1"));
598-
599+
599600
Assert.That(new DateTime(2001, 01, 01).ConvertTo<string>(), Is.EqualTo("2001-01-01"));
601+
Assert.That(new Guid("98ECE840-0BE4-452E-B6AD-7C3A4404F119").ConvertTo<string>(), Is.EqualTo("98ece8400be4452eb6ad7c3a4404f119"));
600602
}
601603

602604
[Test]

0 commit comments

Comments
 (0)