This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +52
-8
lines changed
benchmarks/ServiceStack.Text.Benchmarks
tests/ServiceStack.Text.Tests Expand file tree Collapse file tree 3 files changed +52
-8
lines changed Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ public class JsonSerializationBenchmarks
67
67
{
68
68
static ModelWithAllTypes allTypesModel = ModelWithAllTypes . Create ( 3 ) ;
69
69
static ModelWithCommonTypes commonTypesModel = ModelWithCommonTypes . Create ( 3 ) ;
70
- static MemoryStream stream = new MemoryStream ( 16384 ) ;
70
+ static MemoryStream stream = new MemoryStream ( 32768 ) ;
71
71
const string serializedString = "this is the test string" ;
72
72
readonly string serializedString256 = new string ( 't' , 256 ) ;
73
73
readonly string serializedString512 = new string ( 't' , 512 ) ;
Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ public class DirectStreamWriter : TextWriter
11
11
12
12
private Stream stream ;
13
13
private StreamWriter writer = null ;
14
- private char [ ] curChar = new char [ 1 ] ;
14
+ private byte [ ] curChar = new byte [ 1 ] ;
15
+ private bool needFlush = false ;
15
16
16
17
private Encoding encoding ;
17
18
public override Encoding Encoding => encoding ;
@@ -26,25 +27,44 @@ public override void Write(string s)
26
27
{
27
28
if ( s . Length <= optimizedBufferLength )
28
29
{
30
+ if ( needFlush )
31
+ {
32
+ writer . Flush ( ) ;
33
+ needFlush = false ;
34
+ }
35
+
29
36
byte [ ] buffer = Encoding . GetBytes ( s ) ;
30
37
stream . Write ( buffer , 0 , buffer . Length ) ;
31
- }
32
- else
38
+ } else
33
39
{
34
40
if ( writer == null )
35
41
writer = new StreamWriter ( stream , Encoding , s . Length < maxBufferLength ? s . Length : maxBufferLength ) ;
36
42
37
43
writer . Write ( s ) ;
38
- writer . Flush ( ) ;
44
+ needFlush = true ;
39
45
}
40
46
}
41
47
42
48
public override void Write ( char c )
43
49
{
44
- curChar [ 0 ] = c ;
50
+ if ( ( int ) c < 128 )
51
+ {
52
+ if ( needFlush )
53
+ {
54
+ writer . Flush ( ) ;
55
+ needFlush = false ;
56
+ }
45
57
46
- byte [ ] buffer = Encoding . GetBytes ( curChar ) ;
47
- stream . Write ( buffer , 0 , buffer . Length ) ;
58
+ curChar [ 0 ] = ( byte ) c ;
59
+ stream . Write ( curChar , 0 , 1 ) ;
60
+ } else
61
+ {
62
+ if ( writer == null )
63
+ writer = new StreamWriter ( stream , Encoding , optimizedBufferLength ) ;
64
+
65
+ writer . Write ( c ) ;
66
+ needFlush = true ;
67
+ }
48
68
}
49
69
50
70
public override void Flush ( )
Original file line number Diff line number Diff line change @@ -66,6 +66,30 @@ public void Does_encode_unicode()
66
66
}
67
67
}
68
68
69
+ [ Test ]
70
+ public void Does_encode_large_strings ( )
71
+ {
72
+ char [ ] testChars = new char [ 32769 ] ;
73
+ for ( int i = 0 ; i < testChars . Length ; i ++ )
74
+ testChars [ i ] = ( char ) i ;
75
+
76
+ string test = new string ( testChars ) ;
77
+
78
+ var obj = new { test } ;
79
+ using ( var mem = new System . IO . MemoryStream ( ) )
80
+ {
81
+ ServiceStack . Text . JsonSerializer . SerializeToStream ( obj , obj . GetType ( ) , mem ) ;
82
+
83
+ var encoded = System . Text . Encoding . UTF8 . GetString ( mem . ToArray ( ) ) ;
84
+
85
+ var copy1 = JsonObject . Parse ( encoded ) ;
86
+
87
+ Assert . That ( test , Is . EqualTo ( copy1 [ "test" ] ) ) ;
88
+
89
+ System . Diagnostics . Debug . WriteLine ( copy1 [ "test" ] ) ;
90
+ }
91
+ }
92
+
69
93
[ Test ]
70
94
public void Can_parse_Twitter_response ( )
71
95
{
You can’t perform that action at this time.
0 commit comments