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

Commit 17f0625

Browse files
committed
implement remaining CSV Deserialize API's
1 parent aea175c commit 17f0625

File tree

2 files changed

+92
-59
lines changed

2 files changed

+92
-59
lines changed

src/ServiceStack.Text/CsvSerializer.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Globalization;
44
using System.IO;
5+
using System.Linq;
56
using System.Reflection;
67
using System.Text;
78
using System.Threading;
@@ -136,15 +137,21 @@ public static void SerializeToStream(object obj, Stream stream)
136137

137138
public static T DeserializeFromStream<T>(Stream stream)
138139
{
139-
throw new NotImplementedException();
140+
if (stream == null) return default(T);
141+
using (var reader = new StreamReader(stream, UTF8Encoding))
142+
{
143+
return DeserializeFromString<T>(reader.ReadToEnd());
144+
}
140145
}
141146

142147
public static object DeserializeFromStream(Type type, Stream stream)
143148
{
144149
if (stream == null) return null;
145-
var reader = new StreamReader(stream, UTF8Encoding);
146-
var readFn = GetReadFn(type);
147-
return readFn(reader.ReadToEnd());
150+
using (var reader = new StreamReader(stream, UTF8Encoding))
151+
{
152+
var readFn = GetReadFn(type);
153+
return readFn(reader.ReadToEnd());
154+
}
148155
}
149156

150157
public static T DeserializeFromString<T>(string text)
@@ -156,7 +163,8 @@ public static T DeserializeFromString<T>(string text)
156163

157164
public static object DeserializeFromString(Type type, string text)
158165
{
159-
throw new NotImplementedException();
166+
if (string.IsNullOrEmpty(text)) return null;
167+
return GetReadFn(type)(text);
160168
}
161169

162170
public static void WriteLateBoundObject(TextWriter writer, object value)

tests/ServiceStack.Text.Tests/Support/MovieDtos.cs

Lines changed: 79 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,55 @@
55

66
namespace ServiceStack.Text.Tests.Support
77
{
8-
public static class MoviesData
9-
{
10-
public static List<Movie> Movies = new List<Movie>
11-
{
12-
new Movie { ImdbId = "tt0111161", Title = "The Shawshank Redemption", Rating = 9.2m, Director = "Frank Darabont", ReleaseDate = new DateTime(1995,2,17), TagLine = "Fear can hold you prisoner. Hope can set you free.", Genres = new List<string>{"Crime","Drama"}, },
13-
new Movie { ImdbId = "tt0068646", Title = "The Godfather", Rating = 9.2m, Director = "Francis Ford Coppola", ReleaseDate = new DateTime(1972,3,24), TagLine = "An offer you can't refuse.", Genres = new List<string> {"Crime","Drama", "Thriller"}, },
14-
new Movie { ImdbId = "tt1375666", Title = "Inception", Rating = 9.2m, Director = "Christopher Nolan", ReleaseDate = new DateTime(2010,7,16), TagLine = "Your mind is the scene of the crime", Genres = new List<string>{"Action", "Mystery", "Sci-Fi", "Thriller"}, },
15-
new Movie { ImdbId = "tt0071562", Title = "The Godfather: Part II", Rating = 9.0m, Director = "Francis Ford Coppola", ReleaseDate = new DateTime(1974,12,20), Genres = new List<string> {"Crime","Drama", "Thriller"}, },
16-
new Movie { ImdbId = "tt0060196", Title = "The Good, the Bad and the Ugly", Rating = 9.0m, Director = "Sergio Leone", ReleaseDate = new DateTime(1967,12,29), TagLine = "They formed an alliance of hate to steal a fortune in dead man's gold", Genres = new List<string>{"Adventure","Western"}, },
17-
};
18-
19-
}
20-
21-
[DataContract]
22-
public class Movie
23-
{
24-
public Movie()
25-
{
26-
this.Genres = new List<string>();
27-
}
8+
public static class MoviesData
9+
{
10+
public static List<Movie> Movies = new List<Movie>
11+
{
12+
new Movie { ImdbId = "tt0111161", Title = "The Shawshank Redemption", Rating = 9.2m, Director = "Frank Darabont", ReleaseDate = new DateTime(1995,2,17), TagLine = "Fear can hold you prisoner. Hope can set you free.", Genres = new List<string>{"Crime","Drama"}, },
13+
new Movie { ImdbId = "tt0068646", Title = "The Godfather", Rating = 9.2m, Director = "Francis Ford Coppola", ReleaseDate = new DateTime(1972,3,24), TagLine = "An offer you can't refuse.", Genres = new List<string> {"Crime","Drama", "Thriller"}, },
14+
new Movie { ImdbId = "tt1375666", Title = "Inception", Rating = 9.2m, Director = "Christopher Nolan", ReleaseDate = new DateTime(2010,7,16), TagLine = "Your mind is the scene of the crime", Genres = new List<string>{"Action", "Mystery", "Sci-Fi", "Thriller"}, },
15+
new Movie { ImdbId = "tt0071562", Title = "The Godfather: Part II", Rating = 9.0m, Director = "Francis Ford Coppola", ReleaseDate = new DateTime(1974,12,20), Genres = new List<string> {"Crime","Drama", "Thriller"}, },
16+
new Movie { ImdbId = "tt0060196", Title = "The Good, the Bad and the Ugly", Rating = 9.0m, Director = "Sergio Leone", ReleaseDate = new DateTime(1967,12,29), TagLine = "They formed an alliance of hate to steal a fortune in dead man's gold", Genres = new List<string>{"Adventure","Western"}, },
17+
};
18+
19+
}
20+
21+
public class Movies : List<Movie>, IEquatable<Movies>
22+
{
23+
public Movies() {}
24+
25+
public Movies(IEnumerable<Movie> collection) : base(collection) {}
26+
27+
public bool Equals(Movies other)
28+
{
29+
return base.Equals(other);
30+
}
31+
32+
public override bool Equals(object obj)
33+
{
34+
if (ReferenceEquals(null, obj)) return false;
35+
if (ReferenceEquals(this, obj)) return true;
36+
if (obj.GetType() != this.GetType()) return false;
37+
return Equals((Movies) obj);
38+
}
39+
40+
public override int GetHashCode()
41+
{
42+
return base.GetHashCode();
43+
}
44+
}
45+
46+
[DataContract]
47+
public class Movie
48+
{
49+
public Movie()
50+
{
51+
this.Genres = new List<string>();
52+
}
2853

2954
[DataMember(EmitDefaultValue = false, IsRequired = false)]
3055
[AutoIncrement]
31-
public int Id { get; set; }
56+
public int Id { get; set; }
3257

3358
[DataMember(Order = 3, EmitDefaultValue = false, IsRequired = false)]
3459
public string ImdbId { get; set; }
@@ -51,34 +76,34 @@ public Movie()
5176
[DataMember(Order = 8, EmitDefaultValue = false, IsRequired = false)]
5277
public List<string> Genres { get; set; }
5378

54-
#region AutoGen ReSharper code, only required by tests
55-
public bool Equals(Movie other)
56-
{
57-
if (ReferenceEquals(null, other)) return false;
58-
if (ReferenceEquals(this, other)) return true;
59-
return Equals(other.ImdbId, ImdbId)
60-
&& Equals(other.Title, Title)
61-
&& other.Rating == Rating
62-
&& Equals(other.Director, Director)
63-
&& other.ReleaseDate.Equals(ReleaseDate)
64-
&& Equals(other.TagLine, TagLine)
65-
&& Genres.EquivalentTo(other.Genres);
66-
}
67-
68-
public override bool Equals(object obj)
69-
{
70-
if (ReferenceEquals(null, obj)) return false;
71-
if (ReferenceEquals(this, obj)) return true;
72-
if (obj.GetType() != typeof(Movie)) return false;
73-
return Equals((Movie)obj);
74-
}
75-
76-
public override int GetHashCode()
77-
{
78-
return ImdbId != null ? ImdbId.GetHashCode() : 0;
79-
}
80-
#endregion
81-
}
79+
#region AutoGen ReSharper code, only required by tests
80+
public bool Equals(Movie other)
81+
{
82+
if (ReferenceEquals(null, other)) return false;
83+
if (ReferenceEquals(this, other)) return true;
84+
return Equals(other.ImdbId, ImdbId)
85+
&& Equals(other.Title, Title)
86+
&& other.Rating == Rating
87+
&& Equals(other.Director, Director)
88+
&& other.ReleaseDate.Equals(ReleaseDate)
89+
&& Equals(other.TagLine, TagLine)
90+
&& Genres.EquivalentTo(other.Genres);
91+
}
92+
93+
public override bool Equals(object obj)
94+
{
95+
if (ReferenceEquals(null, obj)) return false;
96+
if (ReferenceEquals(this, obj)) return true;
97+
if (obj.GetType() != typeof(Movie)) return false;
98+
return Equals((Movie)obj);
99+
}
100+
101+
public override int GetHashCode()
102+
{
103+
return ImdbId != null ? ImdbId.GetHashCode() : 0;
104+
}
105+
#endregion
106+
}
82107

83108
[DataContract]
84109
public class MovieResponse : IEquatable<MovieResponse>
@@ -98,7 +123,7 @@ public override bool Equals(object obj)
98123
if (ReferenceEquals(null, obj)) return false;
99124
if (ReferenceEquals(this, obj)) return true;
100125
if (obj.GetType() != this.GetType()) return false;
101-
return Equals((MovieResponse) obj);
126+
return Equals((MovieResponse)obj);
102127
}
103128

104129
public override int GetHashCode()
@@ -128,14 +153,14 @@ public override bool Equals(object obj)
128153
if (ReferenceEquals(null, obj)) return false;
129154
if (ReferenceEquals(this, obj)) return true;
130155
if (obj.GetType() != this.GetType()) return false;
131-
return Equals((MoviesResponse) obj);
156+
return Equals((MoviesResponse)obj);
132157
}
133158

134159
public override int GetHashCode()
135160
{
136161
unchecked
137162
{
138-
return (Id*397) ^ (Movies != null ? Movies.GetHashCode() : 0);
163+
return (Id * 397) ^ (Movies != null ? Movies.GetHashCode() : 0);
139164
}
140165
}
141166
}
@@ -159,14 +184,14 @@ public override bool Equals(object obj)
159184
if (ReferenceEquals(null, obj)) return false;
160185
if (ReferenceEquals(this, obj)) return true;
161186
if (obj.GetType() != this.GetType()) return false;
162-
return Equals((MoviesResponse2) obj);
187+
return Equals((MoviesResponse2)obj);
163188
}
164189

165190
public override int GetHashCode()
166191
{
167192
unchecked
168193
{
169-
return (Id*397) ^ (Movies != null ? Movies.GetHashCode() : 0);
194+
return (Id * 397) ^ (Movies != null ? Movies.GetHashCode() : 0);
170195
}
171196
}
172197
}

0 commit comments

Comments
 (0)