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

Commit 9142303

Browse files
committed
Add support for converting to and from ValueTypes
1 parent 8d87e20 commit 9142303

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Runtime.Serialization;
1111
using System.Threading;
1212
using ServiceStack.Text;
13+
using ServiceStack.Text.Common;
1314

1415
namespace ServiceStack
1516
{
@@ -24,14 +25,10 @@ public static T ConvertTo<T>(this object from)
2425
return default(T);
2526

2627
if (from.GetType() == typeof(T))
27-
{
2828
return (T)from;
29-
}
3029

31-
if (from.GetType().IsValueType())
32-
{
33-
return (T)Convert.ChangeType(from, typeof(T), provider: null);
34-
}
30+
if (from.GetType().IsValueType() || typeof(T).IsValueType())
31+
return (T)ChangeValueType(from, typeof(T));
3532

3633
if (typeof(IEnumerable).IsAssignableFromType(typeof(T)))
3734
{
@@ -45,6 +42,40 @@ public static T ConvertTo<T>(this object from)
4542
return to.PopulateWith(from);
4643
}
4744

45+
public static object ConvertTo(this object from, Type type)
46+
{
47+
if (from == null)
48+
return null;
49+
50+
if (from.GetType() == type)
51+
return from;
52+
53+
if (from.GetType().IsValueType() || type.IsValueType())
54+
return ChangeValueType(from, type);
55+
56+
if (typeof(IEnumerable).IsAssignableFromType(type))
57+
{
58+
var listResult = TranslateListWithElements.TryTranslateCollections(
59+
from.GetType(), type, from);
60+
61+
return listResult;
62+
}
63+
64+
var to = type.CreateInstance();
65+
return to.PopulateInstance(from);
66+
}
67+
68+
private static object ChangeValueType(object from, Type type)
69+
{
70+
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);
75+
76+
return Convert.ChangeType(from, type, provider: null);
77+
}
78+
4879
private static readonly Dictionary<Type, List<string>> TypePropertyNamesMap = new Dictionary<Type, List<string>>();
4980

5081
public static List<string> GetPropertyNames(this Type type)
@@ -266,6 +297,18 @@ public static To PopulateWith<To, From>(this To to, From from)
266297
return to;
267298
}
268299

300+
public static object PopulateInstance(this object to, object from)
301+
{
302+
if (to == null || from == null)
303+
return null;
304+
305+
var assignmentDefinition = GetAssignmentDefinition(to.GetType(), from.GetType());
306+
307+
assignmentDefinition.PopulateWithNonDefaultValues(to, from);
308+
309+
return to;
310+
}
311+
269312
public static To PopulateWithNonDefaultValues<To, From>(this To to, From from)
270313
{
271314
if (Equals(to, default(To)) || Equals(from, default(From))) return default(To);

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,30 @@ public void Can_change_ignored_properties()
569569

570570
Assert.That(dto.ToJson(), Is.EqualTo("{\"Id\":0,\"JsonIgnoreId\":1}"));
571571
}
572+
573+
[Test]
574+
public void Does_convert_to_ValueType()
575+
{
576+
Assert.That("1".ConvertTo(typeof(int)), Is.EqualTo(1));
577+
Assert.That("1".ConvertTo(typeof(long)), Is.EqualTo(1L));
578+
Assert.That("1.1".ConvertTo(typeof(float)), Is.EqualTo(1.1f));
579+
Assert.That("1.1".ConvertTo(typeof(double)), Is.EqualTo(1.1d));
580+
Assert.That("1.1".ConvertTo(typeof(decimal)), Is.EqualTo(1.1M));
581+
582+
Assert.That("2001-01-01".ConvertTo<DateTime>(), Is.EqualTo(new DateTime(2001, 01, 01)));
583+
}
584+
585+
[Test]
586+
public void Does_convert_from_ValueType_to_strings()
587+
{
588+
Assert.That(1.ConvertTo(typeof(string)), Is.EqualTo("1"));
589+
Assert.That(1L.ConvertTo(typeof(string)), Is.EqualTo("1"));
590+
Assert.That(1.1f.ConvertTo(typeof(string)), Is.EqualTo("1.1"));
591+
Assert.That(1.1d.ConvertTo(typeof(string)), Is.EqualTo("1.1"));
592+
Assert.That(1.1M.ConvertTo(typeof(string)), Is.EqualTo("1.1"));
593+
594+
Assert.That(new DateTime(2001, 01, 01).ConvertTo<string>(), Is.EqualTo("2001-01-01"));
595+
}
572596
}
573597

574598
public class Test

0 commit comments

Comments
 (0)