2
// http://code.google.com/p/servicestack/wiki/TypeSerializer
3
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
6
// Demis Bellot (demis.bellot@gmail.com)
8
// Copyright 2011 Liquidbit Ltd.
10
// Licensed under the same terms of ServiceStack: new BSD license.
16
using ServiceStack.Text.Jsv;
18
namespace ServiceStack.Text
20
public class TypeSerializer<T> : ITypeSerializer<T>
22
public bool CanCreateFromString(Type type)
24
return JsvReader.GetParseFn(type) != null;
28
/// Parses the specified value.
30
/// <param name="value">The value.</param>
31
/// <returns></returns>
32
public T DeserializeFromString(string value)
34
if (string.IsNullOrEmpty(value)) return default(T);
35
return (T)JsvReader<T>.Parse(value);
38
public T DeserializeFromReader(TextReader reader)
40
return DeserializeFromString(reader.ReadToEnd());
43
public string SerializeToString(T value)
45
if (value == null) return null;
46
if (typeof(T) == typeof(string)) return value as string;
48
var sb = new StringBuilder();
49
using (var writer = new StringWriter(sb))
51
JsvWriter<T>.WriteObject(writer, value);
56
public void SerializeToWriter(T value, TextWriter writer)
58
if (value == null) return;
59
if (typeof(T) == typeof(string))
65
JsvWriter<T>.WriteObject(writer, value);
b'\\ No newline at end of file'