@@ -52,6 +52,7 @@ public static object DeserializeFromReader(TextReader reader, Type type)
5252 public static string SerializeToString < T > ( T value )
5353 {
5454 if ( value == null ) return null ;
55+ if ( typeof ( T ) == typeof ( object ) ) return SerializeToString ( value , value . GetType ( ) ) ;
5556
5657 var sb = new StringBuilder ( 4096 ) ;
5758 using ( var writer = new StringWriter ( sb , CultureInfo . InvariantCulture ) )
@@ -68,41 +69,6 @@ public static string SerializeToString<T>(T value)
6869 return sb . ToString ( ) ;
6970 }
7071
71- public static void SerializeToWriter < T > ( T value , TextWriter writer )
72- {
73- if ( value == null ) return ;
74- if ( typeof ( T ) == typeof ( string ) )
75- {
76- writer . Write ( value ) ;
77- return ;
78- }
79-
80- JsonWriter < T > . WriteObject ( writer , value ) ;
81- }
82-
83- public static void SerializeToStream < T > ( T value , Stream stream )
84- {
85- var writer = new StreamWriter ( stream , UTF8EncodingWithoutBom ) ;
86- JsonWriter < T > . WriteObject ( writer , value ) ;
87- writer . Flush ( ) ;
88- }
89-
90- public static T DeserializeFromStream < T > ( Stream stream )
91- {
92- using ( var reader = new StreamReader ( stream , UTF8EncodingWithoutBom ) )
93- {
94- return DeserializeFromString < T > ( reader . ReadToEnd ( ) ) ;
95- }
96- }
97-
98- public static object DeserializeFromStream ( Type type , Stream stream )
99- {
100- using ( var reader = new StreamReader ( stream , UTF8EncodingWithoutBom ) )
101- {
102- return DeserializeFromString ( reader . ReadToEnd ( ) , type ) ;
103- }
104- }
105-
10672 public static string SerializeToString ( object value , Type type )
10773 {
10874 if ( value == null ) return null ;
@@ -122,6 +88,23 @@ public static string SerializeToString(object value, Type type)
12288 return sb . ToString ( ) ;
12389 }
12490
91+ public static void SerializeToWriter < T > ( T value , TextWriter writer )
92+ {
93+ if ( value == null ) return ;
94+ if ( typeof ( T ) == typeof ( string ) )
95+ {
96+ writer . Write ( value ) ;
97+ return ;
98+ }
99+ if ( typeof ( T ) == typeof ( object ) )
100+ {
101+ SerializeToWriter ( value , value . GetType ( ) , writer ) ;
102+ return ;
103+ }
104+
105+ JsonWriter < T > . WriteObject ( writer , value ) ;
106+ }
107+
125108 public static void SerializeToWriter ( object value , Type type , TextWriter writer )
126109 {
127110 if ( value == null ) return ;
@@ -134,11 +117,40 @@ public static void SerializeToWriter(object value, Type type, TextWriter writer)
134117 JsonWriter . GetWriteFn ( type ) ( writer , value ) ;
135118 }
136119
120+ public static void SerializeToStream < T > ( T value , Stream stream )
121+ {
122+ if ( typeof ( T ) == typeof ( object ) )
123+ {
124+ SerializeToStream ( value , value . GetType ( ) , stream ) ;
125+ return ;
126+ }
127+
128+ var writer = new StreamWriter ( stream , UTF8EncodingWithoutBom ) ;
129+ JsonWriter < T > . WriteObject ( writer , value ) ;
130+ writer . Flush ( ) ;
131+ }
132+
137133 public static void SerializeToStream ( object value , Type type , Stream stream )
138134 {
139135 var writer = new StreamWriter ( stream , UTF8EncodingWithoutBom ) ;
140136 JsonWriter . GetWriteFn ( type ) ( writer , value ) ;
141137 writer . Flush ( ) ;
142138 }
139+
140+ public static T DeserializeFromStream < T > ( Stream stream )
141+ {
142+ using ( var reader = new StreamReader ( stream , UTF8EncodingWithoutBom ) )
143+ {
144+ return DeserializeFromString < T > ( reader . ReadToEnd ( ) ) ;
145+ }
146+ }
147+
148+ public static object DeserializeFromStream ( Type type , Stream stream )
149+ {
150+ using ( var reader = new StreamReader ( stream , UTF8EncodingWithoutBom ) )
151+ {
152+ return DeserializeFromString ( reader . ReadToEnd ( ) , type ) ;
153+ }
154+ }
143155 }
144156}
0 commit comments