forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeSerializer.cs
More file actions
160 lines (141 loc) · 4.31 KB
/
TypeSerializer.cs
File metadata and controls
160 lines (141 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// http://code.google.com/p/servicestack/wiki/TypeSerializer
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2011 Liquidbit Ltd.
//
// Licensed under the same terms of ServiceStack: new BSD license.
//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using ServiceStack.Text.Jsv;
namespace ServiceStack.Text
{
/// <summary>
/// Creates an instance of a Type from a string value
/// </summary>
public static class TypeSerializer
{
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
public const string DoubleQuoteString = "\"\"";
/// <summary>
/// Determines whether the specified type is convertible from string.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// <c>true</c> if the specified type is convertible from string; otherwise, <c>false</c>.
/// </returns>
public static bool CanCreateFromString(Type type)
{
return JsvReader.GetParseFn(type) != null;
}
/// <summary>
/// Parses the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static T DeserializeFromString<T>(string value)
{
if (string.IsNullOrEmpty(value)) return default(T);
return (T)JsvReader<T>.Parse(value);
}
public static T DeserializeFromReader<T>(TextReader reader)
{
return DeserializeFromString<T>(reader.ReadToEnd());
}
/// <summary>
/// Parses the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static object DeserializeFromString(string value, Type type)
{
return value == null
? null
: JsvReader.GetParseFn(type)(value);
}
public static object DeserializeFromReader(TextReader reader, Type type)
{
return DeserializeFromString(reader.ReadToEnd(), type);
}
public static string SerializeToString<T>(T value)
{
if (value == null) return null;
if (typeof(T) == typeof(string)) return value as string;
var sb = new StringBuilder(4096);
using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
{
JsvWriter<T>.WriteObject(writer, value);
}
return sb.ToString();
}
public static void SerializeToWriter<T>(T value, TextWriter writer)
{
if (value == null) return;
if (typeof(T) == typeof(string))
{
writer.Write(value);
return;
}
JsvWriter<T>.WriteObject(writer, value);
}
public static T Clone<T>(T value)
{
var serializedValue = SerializeToString(value);
var cloneObj = DeserializeFromString<T>(serializedValue);
return cloneObj;
}
public static void SerializeToStream<T>(T value, Stream stream)
{
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
JsvWriter<T>.WriteObject(writer, value);
writer.Flush();
}
public static T DeserializeFromStream<T>(Stream stream)
{
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
{
return DeserializeFromString<T>(reader.ReadToEnd());
}
}
public static object DeserializeFromStream(Type type, Stream stream)
{
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
{
return DeserializeFromString(reader.ReadToEnd(), type);
}
}
/// <summary>
/// Useful extension method to get the Dictionary[string,string] representation of any POCO type.
/// </summary>
/// <returns></returns>
public static Dictionary<string, string> ToStringDictionary<T>(this T obj)
where T : class
{
var jsv = SerializeToString(obj);
var map = DeserializeFromString<Dictionary<string, string>>(jsv);
return map;
}
/// <summary>
/// Recursively prints the contents of any POCO object in a human-friendly, readable format
/// </summary>
/// <returns></returns>
public static string Dump<T>(this T instance)
{
return SerializeAndFormat(instance);
}
public static string SerializeAndFormat<T>(this T instance)
{
var dtoStr = SerializeToString(instance);
var formatStr = JsvFormatter.Format(dtoStr);
return formatStr;
}
}
}