Skip to content

Commit 7ebb9a9

Browse files
author
Chung, Chun-Yi
committed
[Enhance] JSON string convert to dynamic
1 parent e834152 commit 7ebb9a9

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

IsTo.Tests/IsTo.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<Compile Include="To\ToOfGenericToDateTime.cs" />
8484
<Compile Include="To\ToOfGenericToDecimal.cs" />
8585
<Compile Include="To\ToOfGenericToDouble.cs" />
86+
<Compile Include="To\ToOfGenericToDynamic.cs" />
8687
<Compile Include="To\ToOfGenericToEnum.cs" />
8788
<Compile Include="To\ToOfGenericToInt16.cs" />
8889
<Compile Include="To\ToOfGenericToInt32.cs" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) kuicker.org. All rights reserved.
2+
// Modified By YYYY-MM-DD
3+
// kevinjong 2016-03-08 - Creation
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using Xunit;
8+
9+
namespace IsTo.Tests
10+
{
11+
public class ToOfGenericToDynamic
12+
{
13+
[Fact]
14+
public void ByString()
15+
{
16+
var value = @"
17+
{
18+
""A"": ""abc"",
19+
""B"": 123
20+
}
21+
";
22+
var result = value.To<dynamic>();
23+
var date = new DateTime(2016, 3, 8);
24+
result.C = date;
25+
26+
Assert.True(result.A == "abc");
27+
Assert.True(result.B == 123);
28+
Assert.True(result.C == date);
29+
}
30+
}
31+
}

IsTo/To/ToExtender.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Drawing;
99
using System.IO;
10+
using Newtonsoft.Json;
1011

1112
namespace IsTo
1213
{
@@ -31,9 +32,7 @@ public static T To<T>(
3132
string format)
3233
{
3334
T result;
34-
if(value.TryTo<T>(
35-
out result,
36-
format)) {
35+
if(value.TryTo<T>(out result, format)) {
3736
return result;
3837
} else {
3938
return airbag;
@@ -48,6 +47,18 @@ public static bool TryTo<T>(
4847
result = default(T);
4948
if(null == value) { return false; }
5049

50+
var str = value.ToString();
51+
if(value.Is<string>() && IsJson(str)) {
52+
var t = typeof(T);
53+
if(!t.IsPrimitive && !t.Is<byte[]>()) {
54+
try {
55+
result = JsonConvert
56+
.DeserializeObject<T>(str);
57+
return true;
58+
} catch(Exception ex) { }
59+
}
60+
}
61+
5162
if(value.Is<T>()) {
5263
try {
5364
result = (T)value;
@@ -56,10 +67,7 @@ public static bool TryTo<T>(
5667
}
5768

5869
object val;
59-
if(TryTo(
60-
value,
61-
typeof(T),
62-
out val, format)) {
70+
if(TryTo(value, typeof(T), out val, format)) {
6371
try {
6472
result = (T)val;
6573
return true;
@@ -69,15 +77,13 @@ public static bool TryTo<T>(
6977
return false;
7078
}
7179

72-
7380
public static object To(
7481
this object value,
7582
Type type)
7683
{
7784
return value.To(type, string.Empty);
7885
}
7986

80-
8187
public static object To(
8288
this object value,
8389
Type type,
@@ -132,9 +138,6 @@ private static bool TryTo(
132138
out result,
133139
format
134140
);
135-
136-
137-
138141
case TypeCategory.Enum:
139142
return TryFromEnum(
140143
value,
@@ -159,7 +162,6 @@ private static bool TryTo(
159162
out result,
160163
format
161164
);
162-
163165
case TypeCategory.Stream:
164166
return TryFromStream(
165167
value as Stream,
@@ -313,13 +315,7 @@ private static bool TryTo(
313315
format
314316
);
315317
case TypeCategory.Struct:
316-
return TryFromStruct(
317-
value,
318-
from,
319-
to,
320-
out result,
321-
format
322-
);
318+
return TryFromStruct(value, from, to, out result, format);
323319
case TypeCategory.Null:
324320
return false;
325321
case TypeCategory.Others:

0 commit comments

Comments
 (0)