Skip to content

Commit 6681ee1

Browse files
committed
more serializer options for all formats
1 parent e1f2e85 commit 6681ee1

4 files changed

Lines changed: 133 additions & 99 deletions

File tree

src/ServiceStack.Text/Env.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static Env()
2323
+ (IsMonoTouch ? " MonoTouch" : "");
2424
}
2525

26-
public static decimal ServiceStackVersion = 1.67m;
26+
public static decimal ServiceStackVersion = 1.68m;
2727

2828
public static bool IsUnix { get; set; }
2929

Lines changed: 98 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,99 @@
1-
//
2-
// http://code.google.com/p/servicestack/wiki/TypeSerializer
3-
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
4-
//
5-
// Authors:
6-
// Demis Bellot (demis.bellot@gmail.com)
7-
//
8-
// Copyright 2010 Liquidbit Ltd.
9-
//
10-
// Licensed under the same terms of ServiceStack: new BSD license.
11-
//
12-
13-
using System;
14-
using System.Globalization;
15-
using System.IO;
16-
using System.Text;
17-
using ServiceStack.Text.Json;
18-
19-
namespace ServiceStack.Text
20-
{
21-
/// <summary>
22-
/// Creates an instance of a Type from a string value
23-
/// </summary>
24-
public static class JsonSerializer
25-
{
26-
public static T DeserializeFromString<T>(string value)
27-
{
28-
if (string.IsNullOrEmpty(value)) return default(T);
29-
return (T)JsonReader<T>.Parse(value);
30-
}
31-
32-
public static T DeserializeFromReader<T>(TextReader reader)
33-
{
34-
return DeserializeFromString<T>(reader.ReadToEnd());
35-
}
36-
37-
public static object DeserializeFromString(string value, Type type)
38-
{
39-
return value == null
40-
? null
41-
: JsonReader.GetParseFn(type)(value);
42-
}
43-
44-
public static object DeserializeFromReader(TextReader reader, Type type)
45-
{
46-
return DeserializeFromString(reader.ReadToEnd(), type);
47-
}
48-
49-
public static string SerializeToString<T>(T value)
50-
{
51-
if (value == null) return null;
52-
if (typeof(T) == typeof(string)) return value as string;
53-
54-
var sb = new StringBuilder(4096);
55-
using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
56-
{
57-
JsonWriter<T>.WriteObject(writer, value);
58-
}
59-
return sb.ToString();
60-
}
61-
62-
public static void SerializeToWriter<T>(T value, TextWriter writer)
63-
{
64-
if (value == null) return;
65-
if (typeof(T) == typeof(string))
66-
{
67-
writer.Write(value);
68-
return;
69-
}
70-
71-
JsonWriter<T>.WriteObject(writer, value);
72-
}
73-
74-
public static void SerializeToStream<T>(T value, Stream stream)
75-
{
76-
using (var writer = new StreamWriter(stream, Encoding.UTF8))
77-
{
78-
JsonWriter<T>.WriteObject(writer, value);
79-
}
80-
}
81-
82-
public static T DeserializeFromStream<T>(Stream stream)
83-
{
84-
using (var reader = new StreamReader(stream, Encoding.UTF8))
85-
{
86-
return DeserializeFromString<T>(reader.ReadToEnd());
87-
}
88-
}
89-
90-
}
1+
//
2+
// http://code.google.com/p/servicestack/wiki/TypeSerializer
3+
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
4+
//
5+
// Authors:
6+
// Demis Bellot (demis.bellot@gmail.com)
7+
//
8+
// Copyright 2010 Liquidbit Ltd.
9+
//
10+
// Licensed under the same terms of ServiceStack: new BSD license.
11+
//
12+
13+
using System;
14+
using System.Globalization;
15+
using System.IO;
16+
using System.Text;
17+
using ServiceStack.Text.Json;
18+
19+
namespace ServiceStack.Text
20+
{
21+
/// <summary>
22+
/// Creates an instance of a Type from a string value
23+
/// </summary>
24+
public static class JsonSerializer
25+
{
26+
public static T DeserializeFromString<T>(string value)
27+
{
28+
if (string.IsNullOrEmpty(value)) return default(T);
29+
return (T)JsonReader<T>.Parse(value);
30+
}
31+
32+
public static T DeserializeFromReader<T>(TextReader reader)
33+
{
34+
return DeserializeFromString<T>(reader.ReadToEnd());
35+
}
36+
37+
public static object DeserializeFromString(string value, Type type)
38+
{
39+
return value == null
40+
? null
41+
: JsonReader.GetParseFn(type)(value);
42+
}
43+
44+
public static object DeserializeFromReader(TextReader reader, Type type)
45+
{
46+
return DeserializeFromString(reader.ReadToEnd(), type);
47+
}
48+
49+
public static string SerializeToString<T>(T value)
50+
{
51+
if (value == null) return null;
52+
if (typeof(T) == typeof(string)) return value as string;
53+
54+
var sb = new StringBuilder(4096);
55+
using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
56+
{
57+
JsonWriter<T>.WriteObject(writer, value);
58+
}
59+
return sb.ToString();
60+
}
61+
62+
public static void SerializeToWriter<T>(T value, TextWriter writer)
63+
{
64+
if (value == null) return;
65+
if (typeof(T) == typeof(string))
66+
{
67+
writer.Write(value);
68+
return;
69+
}
70+
71+
JsonWriter<T>.WriteObject(writer, value);
72+
}
73+
74+
public static void SerializeToStream<T>(T value, Stream stream)
75+
{
76+
using (var writer = new StreamWriter(stream, Encoding.UTF8))
77+
{
78+
JsonWriter<T>.WriteObject(writer, value);
79+
}
80+
}
81+
82+
public static T DeserializeFromStream<T>(Stream stream)
83+
{
84+
using (var reader = new StreamReader(stream, Encoding.UTF8))
85+
{
86+
return DeserializeFromString<T>(reader.ReadToEnd());
87+
}
88+
}
89+
90+
public static object DeserializeFromStream(Type type, Stream stream)
91+
{
92+
using (var reader = new StreamReader(stream, Encoding.UTF8))
93+
{
94+
return DeserializeFromString(reader.ReadToEnd(), type);
95+
}
96+
}
97+
98+
}
9199
}

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,14 @@ public static T DeserializeFromStream<T>(Stream stream)
130130
return DeserializeFromString<T>(reader.ReadToEnd());
131131
}
132132
}
133+
134+
public static object DeserializeFromStream(Type type, Stream stream)
135+
{
136+
using (var reader = new StreamReader(stream, Encoding.UTF8))
137+
{
138+
return DeserializeFromString(reader.ReadToEnd(), type);
139+
}
140+
}
141+
133142
}
134143
}

src/ServiceStack.Text/XmlSerializer.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ public static T DeserializeFromReader<T>(TextReader reader)
5656
return DeserializeFromString<T>(reader.ReadToEnd());
5757
}
5858

59+
public static T DeserializeFromStream<T>(Stream stream)
60+
{
61+
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
62+
63+
return (T)serializer.ReadObject(stream);
64+
}
65+
66+
public static object DeserializeFromStream(Type type, Stream stream)
67+
{
68+
var serializer = new System.Runtime.Serialization.DataContractSerializer(type);
69+
return serializer.ReadObject(stream);
70+
}
71+
5972
public static string SerializeToString<T>(T from)
6073
{
6174
try
@@ -88,17 +101,21 @@ public static string SerializeToString<T>(T from)
88101

89102
public static void SerializeToWriter<T>(T value, TextWriter writer)
90103
{
91-
if (value == null) return;
92-
if (typeof(T) == typeof(string))
104+
try
93105
{
94-
writer.Write(value);
95-
return;
106+
using (var xw = new XmlTextWriter(writer))
107+
{
108+
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
109+
serializer.WriteObject(xw, value);
110+
}
111+
}
112+
catch (Exception ex)
113+
{
114+
throw new SerializationException(string.Format("Error serializing object of type {0}", typeof(T).FullName), ex);
96115
}
97-
98-
JsvWriter<T>.WriteObject(writer, value);
99116
}
100117

101-
public static void CompressToStream<XmlDto>(XmlDto from, Stream stream)
118+
public static void CompressToStream<TXmlDto>(TXmlDto from, Stream stream)
102119
{
103120
using (var deflateStream = new DeflateStream(stream, CompressionMode.Compress))
104121
using (var xw = new XmlTextWriter(deflateStream, Encoding.UTF8))
@@ -118,7 +135,7 @@ public static void SerializeToStream(object obj, Stream stream)
118135
}
119136
}
120137

121-
public static byte[] Compress<XmlDto>(XmlDto from)
138+
public static byte[] Compress<TXmlDto>(TXmlDto from)
122139
{
123140
using (var ms = new MemoryStream())
124141
{

0 commit comments

Comments
 (0)