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}
0 commit comments