1717using System . Text ;
1818using ServiceStack . Text . Common ;
1919using ServiceStack . Text . Json ;
20+ using ServiceStack . Text . Pools ;
2021
2122namespace ServiceStack . Text
2223{
@@ -54,57 +55,7 @@ public static object DeserializeFromReader(TextReader reader, Type type)
5455 {
5556 return DeserializeFromString ( reader . ReadToEnd ( ) , type ) ;
5657 }
57-
58- [ ThreadStatic ] //Reuse the thread static StringBuilder when serializing to strings
59- private static StringBuilderWriter LastWriter ;
60-
61- internal class StringBuilderWriter : IDisposable
62- {
63- protected StringBuilder sb ;
64- protected StringWriter writer ;
65-
66- public StringWriter Writer
67- {
68- get { return writer ; }
69- }
70-
71- public StringBuilderWriter ( )
72- {
73- this . sb = new StringBuilder ( ) ;
74- this . writer = new StringWriter ( sb , CultureInfo . InvariantCulture ) ;
75- }
76-
77- public static StringBuilderWriter Create ( )
78- {
79- var ret = LastWriter ;
80- if ( JsConfig . ReuseStringBuffer && ret != null )
81- {
82- LastWriter = null ;
83- ret . sb . Clear ( ) ;
84- return ret ;
85- }
86-
87- return new StringBuilderWriter ( ) ;
88- }
89-
90- public override string ToString ( )
91- {
92- return sb . ToString ( ) ;
93- }
94-
95- public void Dispose ( )
96- {
97- if ( JsConfig . ReuseStringBuffer )
98- {
99- LastWriter = this ;
100- }
101- else
102- {
103- Writer . Dispose ( ) ;
104- }
105- }
106- }
107-
58+
10859 public static string SerializeToString < T > ( T value )
10960 {
11061 if ( value == null || value is Delegate ) return null ;
@@ -120,36 +71,32 @@ public static string SerializeToString<T>(T value)
12071 return result ;
12172 }
12273
123- using ( var sb = StringBuilderWriter . Create ( ) )
74+ var writer = StringWriterThreadStatic . Allocate ( ) ;
75+ if ( typeof ( T ) == typeof ( string ) )
12476 {
125- if ( typeof ( T ) == typeof ( string ) )
126- {
127- JsonUtils . WriteString ( sb . Writer , value as string ) ;
128- }
129- else
130- {
131- JsonWriter < T > . WriteRootObject ( sb . Writer , value ) ;
132- }
133- return sb . ToString ( ) ;
77+ JsonUtils . WriteString ( writer , value as string ) ;
13478 }
79+ else
80+ {
81+ JsonWriter < T > . WriteRootObject ( writer , value ) ;
82+ }
83+ return StringWriterThreadStatic . ReturnAndFree ( writer ) ;
13584 }
13685
13786 public static string SerializeToString ( object value , Type type )
13887 {
13988 if ( value == null ) return null ;
14089
141- using ( var sb = StringBuilderWriter . Create ( ) )
90+ var writer = StringWriterThreadStatic . Allocate ( ) ;
91+ if ( type == typeof ( string ) )
14292 {
143- if ( type == typeof ( string ) )
144- {
145- JsonUtils . WriteString ( sb . Writer , value as string ) ;
146- }
147- else
148- {
149- JsonWriter . GetWriteFn ( type ) ( sb . Writer , value ) ;
150- }
151- return sb . ToString ( ) ;
93+ JsonUtils . WriteString ( writer , value as string ) ;
94+ }
95+ else
96+ {
97+ JsonWriter . GetWriteFn ( type ) ( writer , value ) ;
15298 }
99+ return StringWriterThreadStatic . ReturnAndFree ( writer ) ;
153100 }
154101
155102 public static void SerializeToWriter < T > ( T value , TextWriter writer )
0 commit comments