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

Commit 9eb3f93

Browse files
committed
Include alt csv deserialize methods in tests + convert results from late-bound API as well
1 parent 1040fd5 commit 9eb3f93

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

src/ServiceStack.Text/CsvSerializer.cs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ public static object DeserializeFromStream(Type type, Stream stream)
149149
if (stream == null) return null;
150150
using (var reader = new StreamReader(stream, UTF8Encoding))
151151
{
152-
var readFn = GetReadFn(type);
153-
return readFn(reader.ReadToEnd());
152+
return DeserializeFromString(type, reader.ReadToEnd());
154153
}
155154
}
156155

@@ -163,13 +162,16 @@ public static T DeserializeFromString<T>(string text)
163162
{
164163
if (string.IsNullOrEmpty(text)) return default(T);
165164
var results = CsvSerializer<T>.ReadObject(text);
166-
return CsvSerializer<T>.ConvertFrom(results);
165+
return ConvertFrom<T>(results);
167166
}
168167

169168
public static object DeserializeFromString(Type type, string text)
170169
{
171170
if (string.IsNullOrEmpty(text)) return null;
172-
return GetReadFn(type)(text);
171+
var fn = GetReadFn(type);
172+
var result = fn(text);
173+
var converted = ConvertFrom(type, result);
174+
return converted;
173175
}
174176

175177
public static void WriteLateBoundObject(TextWriter writer, object value)
@@ -185,6 +187,50 @@ public static object ReadLateBoundObject(Type type, string value)
185187
var readFn = GetReadFn(type);
186188
return readFn(value);
187189
}
190+
191+
internal static T ConvertFrom<T>(object results)
192+
{
193+
if (typeof(T).IsAssignableFromType(results.GetType()))
194+
return (T)results;
195+
196+
foreach (var ci in typeof(T).GetAllConstructors())
197+
{
198+
var ciParams = ci.GetParameters();
199+
if (ciParams.Length == 1)
200+
{
201+
var pi = ciParams.First();
202+
if (pi.ParameterType.IsAssignableFromType(typeof(T)))
203+
{
204+
var to = ci.Invoke(new[] { results });
205+
return (T)to;
206+
}
207+
}
208+
}
209+
210+
return results.ConvertTo<T>();
211+
}
212+
213+
internal static object ConvertFrom(Type type, object results)
214+
{
215+
if (type.IsAssignableFromType(results.GetType()))
216+
return results;
217+
218+
foreach (var ci in type.GetAllConstructors())
219+
{
220+
var ciParams = ci.GetParameters();
221+
if (ciParams.Length == 1)
222+
{
223+
var pi = ciParams.First();
224+
if (pi.ParameterType.IsAssignableFromType(type))
225+
{
226+
var to = ci.Invoke(new[] { results });
227+
return to;
228+
}
229+
}
230+
}
231+
232+
return results.ConvertTo(type);
233+
}
188234
}
189235

190236
public static class CsvSerializer<T>
@@ -486,39 +532,12 @@ public static object ReadObject(string value)
486532
{
487533
return ReadCacheFn(value);
488534
}
489-
catch (Exception ex)
490-
{
491-
throw;
492-
}
493535
finally
494536
{
495537
JsState.IsCsv = hold;
496538
}
497539
}
498540

499-
public static T ConvertFrom(object results)
500-
{
501-
if (typeof(T).IsAssignableFromType(results.GetType()))
502-
{
503-
return (T)results;
504-
}
505-
506-
foreach (var ci in typeof(T).GetAllConstructors())
507-
{
508-
var ciParams = ci.GetParameters();
509-
if (ciParams.Length == 1)
510-
{
511-
var pi = ciParams.First();
512-
if (pi.ParameterType.IsAssignableFromType(typeof(T)))
513-
{
514-
var to = ci.Invoke(new[] { results });
515-
return (T)to;
516-
}
517-
}
518-
}
519-
520-
return results.ConvertTo<T>();
521-
}
522541

523542
}
524543
}

tests/ServiceStack.Text.Tests/CsvSerializerTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public object SerializeAndDeserialize<T>(T data)
5454
AssertEqual(dto, data);
5555
}
5656

57+
using (var ms = new MemoryStream(csv.ToUtf8Bytes()))
58+
{
59+
dto = (T)CsvSerializer.DeserializeFromStream(typeof(T), ms);
60+
AssertEqual(dto, data);
61+
}
62+
5763
return dto;
5864
}
5965

0 commit comments

Comments
 (0)