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

Commit 04bbda7

Browse files
committed
Add System.Runtime.Serialization references
Fixed different classes to support Windows Phone
1 parent ae32e54 commit 04bbda7

4 files changed

Lines changed: 124 additions & 100 deletions

File tree

src/ServiceStack.Text.WP/ServiceStack.Text.WP.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<WarningLevel>4</WarningLevel>
4242
</PropertyGroup>
4343
<ItemGroup>
44+
<Reference Include="System.Runtime.Serialization" />
4445
<Reference Include="System.Windows" />
4546
<Reference Include="system" />
4647
<Reference Include="System.Core" />
@@ -250,6 +251,7 @@
250251
<Compile Include="..\ServiceStack.Text\XmlSerializer.cs">
251252
<Link>XmlSerializer.cs</Link>
252253
</Compile>
254+
<Compile Include="HashSet.cs" />
253255
<Compile Include="Properties\AssemblyInfo.cs" />
254256
</ItemGroup>
255257
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.$(TargetFrameworkProfile).Overrides.targets" />

src/ServiceStack.Text/AssemblyUtils.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System;
22
using System.IO;
33
using System.Reflection;
4-
using System.Text;
54
using System.Text.RegularExpressions;
65
#if SILVERLIGHT
7-
using System.Windows;
6+
87
#endif
98
using ServiceStack.Common.Support;
109

@@ -80,9 +79,9 @@ public static Type FindTypeFromLoadedAssemblies(string typeName)
8079
#if SILVERLIGHT4
8180
var assemblies = ((dynamic) AppDomain.CurrentDomain).GetAssemblies() as Assembly[];
8281
#else
83-
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
82+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
8483
#endif
85-
foreach (var assembly in assemblies)
84+
foreach (var assembly in assemblies)
8685
{
8786
var type = assembly.GetType(typeName);
8887
if (type != null)
@@ -99,9 +98,14 @@ private static Assembly LoadAssembly(string assemblyPath)
9998
{
10099
return Assembly.LoadFrom(assemblyPath);
101100
}
101+
#elif WINDOWS_PHONE
102+
private static Assembly LoadAssembly(string assemblyPath)
103+
{
104+
return Assembly.LoadFrom(assemblyPath);
105+
}
102106
#else
103107
private static Assembly LoadAssembly(string assemblyPath)
104-
{
108+
{
105109
var sri = Application.GetResourceStream(new Uri(assemblyPath, UriKind.Relative));
106110
var myPart = new AssemblyPart();
107111
var assembly = myPart.Load(sri.Stream);
@@ -112,8 +116,14 @@ private static Assembly LoadAssembly(string assemblyPath)
112116
#if !XBOX
113117
public static string GetAssemblyBinPath(Assembly assembly)
114118
{
115-
var binPathPos = assembly.CodeBase.LastIndexOf(UriSeperator);
116-
var assemblyPath = assembly.CodeBase.Substring(0, binPathPos + 1);
119+
#if WINDOWS_PHONE
120+
var codeBase = assembly.GetName().CodeBase;
121+
#else
122+
var codeBase = assembly.CodeBase;
123+
#endif
124+
125+
var binPathPos = codeBase.LastIndexOf(UriSeperator);
126+
var assemblyPath = codeBase.Substring(0, binPathPos + 1);
117127
if (assemblyPath.StartsWith(FileUri))
118128
{
119129
assemblyPath = assemblyPath.Remove(0, FileUri.Length);

src/ServiceStack.Text/Common/DeserializeCollection.cs

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,92 +14,95 @@
1414
using System.Collections.Generic;
1515
using System.Reflection;
1616
using System.Threading;
17+
#if WINDOWS_PHONE
18+
using ServiceStack.Text.WP;
19+
#endif
1720

1821
namespace ServiceStack.Text.Common
1922
{
20-
internal static class DeserializeCollection<TSerializer>
21-
where TSerializer : ITypeSerializer
22-
{
23-
private static readonly ITypeSerializer Serializer = JsWriter.GetTypeSerializer<TSerializer>();
24-
25-
public static ParseStringDelegate GetParseMethod(Type type)
26-
{
27-
var collectionInterface = type.GetTypeWithGenericInterfaceOf(typeof(ICollection<>));
28-
if (collectionInterface == null)
29-
throw new ArgumentException(string.Format("Type {0} is not of type ICollection<>", type.FullName));
30-
31-
//optimized access for regularly used types
32-
if (type.HasInterface(typeof(ICollection<string>)))
33-
return value => ParseStringCollection(value, type);
34-
35-
if (type.HasInterface(typeof(ICollection<int>)))
36-
return value => ParseIntCollection(value, type);
37-
38-
var elementType = collectionInterface.GetGenericArguments()[0];
39-
40-
var supportedTypeParseMethod = Serializer.GetParseFn(elementType);
41-
if (supportedTypeParseMethod != null)
42-
{
43-
var createCollectionType = type.HasAnyTypeDefinitionsOf(typeof(ICollection<>))
23+
internal static class DeserializeCollection<TSerializer>
24+
where TSerializer : ITypeSerializer
25+
{
26+
private static readonly ITypeSerializer Serializer = JsWriter.GetTypeSerializer<TSerializer>();
27+
28+
public static ParseStringDelegate GetParseMethod(Type type)
29+
{
30+
var collectionInterface = type.GetTypeWithGenericInterfaceOf(typeof(ICollection<>));
31+
if (collectionInterface == null)
32+
throw new ArgumentException(string.Format("Type {0} is not of type ICollection<>", type.FullName));
33+
34+
//optimized access for regularly used types
35+
if (type.HasInterface(typeof(ICollection<string>)))
36+
return value => ParseStringCollection(value, type);
37+
38+
if (type.HasInterface(typeof(ICollection<int>)))
39+
return value => ParseIntCollection(value, type);
40+
41+
var elementType = collectionInterface.GetGenericArguments()[0];
42+
43+
var supportedTypeParseMethod = Serializer.GetParseFn(elementType);
44+
if (supportedTypeParseMethod != null)
45+
{
46+
var createCollectionType = type.HasAnyTypeDefinitionsOf(typeof(ICollection<>))
4447
? null : type;
4548

46-
return value => ParseCollectionType(value, createCollectionType, elementType, supportedTypeParseMethod);
47-
}
49+
return value => ParseCollectionType(value, createCollectionType, elementType, supportedTypeParseMethod);
50+
}
4851

49-
return null;
50-
}
52+
return null;
53+
}
5154

52-
public static ICollection<string> ParseStringCollection(string value, Type createType)
53-
{
54-
var items = DeserializeArrayWithElements<string, TSerializer>.ParseGenericArray(value, Serializer.ParseString);
55-
return CreateAndPopulate(createType, items);
56-
}
55+
public static ICollection<string> ParseStringCollection(string value, Type createType)
56+
{
57+
var items = DeserializeArrayWithElements<string, TSerializer>.ParseGenericArray(value, Serializer.ParseString);
58+
return CreateAndPopulate(createType, items);
59+
}
5760

58-
public static ICollection<int> ParseIntCollection(string value, Type createType)
59-
{
60-
var items = DeserializeArrayWithElements<int, TSerializer>.ParseGenericArray(value, x => int.Parse(x));
61-
return CreateAndPopulate(createType, items);
62-
}
61+
public static ICollection<int> ParseIntCollection(string value, Type createType)
62+
{
63+
var items = DeserializeArrayWithElements<int, TSerializer>.ParseGenericArray(value, x => int.Parse(x));
64+
return CreateAndPopulate(createType, items);
65+
}
6366

64-
public static ICollection<T> ParseCollection<T>(string value, Type createType, ParseStringDelegate parseFn)
65-
{
66-
if (value == null) return null;
67+
public static ICollection<T> ParseCollection<T>(string value, Type createType, ParseStringDelegate parseFn)
68+
{
69+
if (value == null) return null;
6770

68-
var items = DeserializeArrayWithElements<T, TSerializer>.ParseGenericArray(value, parseFn);
69-
return CreateAndPopulate(createType, items);
70-
}
71+
var items = DeserializeArrayWithElements<T, TSerializer>.ParseGenericArray(value, parseFn);
72+
return CreateAndPopulate(createType, items);
73+
}
7174

72-
private static ICollection<T> CreateAndPopulate<T>(Type ofCollectionType, T[] withItems)
73-
{
74-
if (ofCollectionType == null) return new List<T>(withItems);
75+
private static ICollection<T> CreateAndPopulate<T>(Type ofCollectionType, T[] withItems)
76+
{
77+
if (ofCollectionType == null) return new List<T>(withItems);
7578

76-
var genericTypeDefinition = ofCollectionType.IsGenericType()
79+
var genericTypeDefinition = ofCollectionType.IsGenericType()
7780
? ofCollectionType.GetGenericTypeDefinition()
7881
: null;
7982
#if !XBOX
80-
if (genericTypeDefinition == typeof(HashSet<T>))
81-
return new HashSet<T>(withItems);
83+
if (genericTypeDefinition == typeof(HashSet<T>))
84+
return new HashSet<T>(withItems);
8285
#endif
83-
if (genericTypeDefinition == typeof(LinkedList<T>))
84-
return new LinkedList<T>(withItems);
85-
86-
var collection = (ICollection<T>)ofCollectionType.CreateInstance();
87-
foreach (var item in withItems)
88-
{
89-
collection.Add(item);
90-
}
91-
return collection;
92-
}
93-
94-
private static Dictionary<Type, ParseCollectionDelegate> ParseDelegateCache
86+
if (genericTypeDefinition == typeof(LinkedList<T>))
87+
return new LinkedList<T>(withItems);
88+
89+
var collection = (ICollection<T>)ofCollectionType.CreateInstance();
90+
foreach (var item in withItems)
91+
{
92+
collection.Add(item);
93+
}
94+
return collection;
95+
}
96+
97+
private static Dictionary<Type, ParseCollectionDelegate> ParseDelegateCache
9598
= new Dictionary<Type, ParseCollectionDelegate>();
9699

97-
private delegate object ParseCollectionDelegate(string value, Type createType, ParseStringDelegate parseFn);
100+
private delegate object ParseCollectionDelegate(string value, Type createType, ParseStringDelegate parseFn);
98101

99-
public static object ParseCollectionType(string value, Type createType, Type elementType, ParseStringDelegate parseFn)
100-
{
101-
ParseCollectionDelegate parseDelegate;
102-
if (ParseDelegateCache.TryGetValue(elementType, out parseDelegate))
102+
public static object ParseCollectionType(string value, Type createType, Type elementType, ParseStringDelegate parseFn)
103+
{
104+
ParseCollectionDelegate parseDelegate;
105+
if (ParseDelegateCache.TryGetValue(elementType, out parseDelegate))
103106
return parseDelegate(value, createType, parseFn);
104107

105108
var mi = typeof(DeserializeCollection<TSerializer>).GetMethod("ParseCollection", BindingFlags.Static | BindingFlags.Public);
@@ -115,8 +118,8 @@ public static object ParseCollectionType(string value, Type createType, Type ele
115118

116119
} while (!ReferenceEquals(
117120
Interlocked.CompareExchange(ref ParseDelegateCache, newCache, snapshot), snapshot));
118-
121+
119122
return parseDelegate(value, createType, parseFn);
120-
}
121-
}
123+
}
124+
}
122125
}

src/ServiceStack.Text/XmlSerializer.cs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
using System;
2-
using System.IO;
3-
#if !XBOX360 && !SILVERLIGHT
1+
2+
#if !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE
43
using System.IO.Compression;
54
#endif
5+
6+
using System;
7+
using System.IO;
68
using System.Runtime.Serialization;
79
using System.Text;
810
using System.Xml;
911

1012
namespace ServiceStack.Text
1113
{
12-
#if !XBOX
14+
#if !XBOX
1315
public class XmlSerializer
1416
{
1517
private readonly XmlDictionaryReaderQuotas quotas;
1618
private static readonly XmlWriterSettings XSettings = new XmlWriterSettings();
1719

1820
public static XmlSerializer Instance
1921
= new XmlSerializer(
20-
#if !SILVERLIGHT
22+
#if !SILVERLIGHT && !WINDOWS_PHONE
2123
new XmlDictionaryReaderQuotas { MaxStringContentLength = 1024 * 1024, }
2224
#endif
23-
);
25+
);
2426

2527
public XmlSerializer(XmlDictionaryReaderQuotas quotas=null, bool omitXmlDeclaration = false)
2628
{
@@ -33,13 +35,20 @@ private static object Deserialize(string xml, Type type, XmlDictionaryReaderQuot
3335
{
3436
try
3537
{
38+
#if WINDOWS_PHONE
39+
using (var reader = XmlDictionaryReader.Create(xml))
40+
{
41+
var serializer = new DataContractSerializer(type);
42+
return serializer.ReadObject(reader);
43+
}
44+
#else
3645
var bytes = Encoding.UTF8.GetBytes(xml);
37-
3846
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, quotas))
3947
{
4048
var serializer = new DataContractSerializer(type);
4149
return serializer.ReadObject(reader);
4250
}
51+
#endif
4352
}
4453
catch (Exception ex)
4554
{
@@ -88,9 +97,9 @@ public static string SerializeToString<T>(T from)
8897
serializer.WriteObject(xw, from);
8998
xw.Flush();
9099
ms.Seek(0, SeekOrigin.Begin);
91-
var reader = new StreamReader(ms);
92-
return reader.ReadToEnd();
93-
}
100+
var reader = new StreamReader(ms);
101+
return reader.ReadToEnd();
102+
}
94103
}
95104
}
96105
catch (Exception ex)
@@ -99,25 +108,25 @@ public static string SerializeToString<T>(T from)
99108
}
100109
}
101110

102-
public static void SerializeToWriter<T>(T value, TextWriter writer)
103-
{
104-
try
105-
{
111+
public static void SerializeToWriter<T>(T value, TextWriter writer)
112+
{
113+
try
114+
{
106115
#if !SILVERLIGHT
107116
using (var xw = new XmlTextWriter(writer))
108117
#else
109118
using (var xw = XmlWriter.Create(writer))
110119
#endif
111-
{
112-
var serializer = new DataContractSerializer(value.GetType());
113-
serializer.WriteObject(xw, value);
114-
}
115-
}
116-
catch (Exception ex)
117-
{
120+
{
121+
var serializer = new DataContractSerializer(value.GetType());
122+
serializer.WriteObject(xw, value);
123+
}
124+
}
125+
catch (Exception ex)
126+
{
118127
throw new SerializationException(string.Format("Error serializing object of type {0}", value.GetType().FullName), ex);
119-
}
120-
}
128+
}
129+
}
121130

122131
public static void SerializeToStream(object obj, Stream stream)
123132
{

0 commit comments

Comments
 (0)