Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit e5e81f3

Browse files
committed
Merge branch 'master' of github.com:ServiceStack/ServiceStack.Text
2 parents 8b6a91e + 53c5293 commit e5e81f3

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

src/ServiceStack.Text/Common/JsWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ static JsWriter()
4747

4848
public static void WriteDynamic(Action callback)
4949
{
50+
var prevState = JsState.IsWritingDynamic;
5051
JsState.IsWritingDynamic = true;
5152
try
5253
{
5354
callback();
5455
}
5556
finally
5657
{
57-
JsState.IsWritingDynamic = false;
58+
JsState.IsWritingDynamic = prevState;
5859
}
5960
}
6061

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,10 @@ public static void WriteProperties(TextWriter writer, object instance)
466466
private static void WriteLateboundProperties(TextWriter writer, object value, Type valueType)
467467
{
468468
var writeFn = Serializer.GetWriteFn(valueType);
469+
var prevState = JsState.IsWritingDynamic;
469470
if (!JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault()) JsState.IsWritingDynamic = true;
470471
writeFn(writer, value);
471-
if (!JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault()) JsState.IsWritingDynamic = false;
472+
if (!JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault()) JsState.IsWritingDynamic = prevState;
472473
}
473474

474475
internal static string GetPropertyName(string propertyName, Config config)

src/ServiceStack.Text/JsonSerializer.Generic.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ public string SerializeToString(T value)
4848
if (value == null) return null;
4949
if (typeof(T) == typeof(object) || typeof(T).IsAbstract || typeof(T).IsInterface)
5050
{
51+
var prevState = JsState.IsWritingDynamic;
5152
if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = true;
5253
var result = JsonSerializer.SerializeToString(value, value.GetType());
53-
if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = false;
54+
if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = prevState;
5455
return result;
5556
}
5657

@@ -64,9 +65,10 @@ public void SerializeToWriter(T value, TextWriter writer)
6465
if (value == null) return;
6566
if (typeof(T) == typeof(object) || typeof(T).IsAbstract || typeof(T).IsInterface)
6667
{
68+
var prevState = JsState.IsWritingDynamic;
6769
if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = true;
6870
JsonSerializer.SerializeToWriter(value, value.GetType(), writer);
69-
if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = false;
71+
if (typeof(T).IsAbstract || typeof(T).IsInterface) JsState.IsWritingDynamic = prevState;
7072
return;
7173
}
7274

src/ServiceStack.Text/JsonSerializer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ public static string SerializeToString<T>(T value)
8585
}
8686
if (typeof(T).IsAbstract || typeof(T).IsInterface)
8787
{
88+
var prevState = JsState.IsWritingDynamic;
8889
JsState.IsWritingDynamic = true;
8990
var result = SerializeToString(value, value.GetType());
90-
JsState.IsWritingDynamic = false;
91+
JsState.IsWritingDynamic = prevState;
9192
return result;
9293
}
9394

@@ -133,9 +134,10 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
133134
}
134135
else if (typeof(T).IsAbstract || typeof(T).IsInterface)
135136
{
137+
var prevState = JsState.IsWritingDynamic;
136138
JsState.IsWritingDynamic = false;
137139
SerializeToWriter(value, value.GetType(), writer);
138-
JsState.IsWritingDynamic = true;
140+
JsState.IsWritingDynamic = prevState;
139141
}
140142
else
141143
{
@@ -165,9 +167,10 @@ public static void SerializeToStream<T>(T value, Stream stream)
165167
}
166168
else if (typeof(T).IsAbstract || typeof(T).IsInterface)
167169
{
170+
var prevState = JsState.IsWritingDynamic;
168171
JsState.IsWritingDynamic = false;
169172
SerializeToStream(value, value.GetType(), stream);
170-
JsState.IsWritingDynamic = true;
173+
JsState.IsWritingDynamic = prevState;
171174
}
172175
else
173176
{

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ public static string SerializeToString<T>(T value)
114114
}
115115
if (typeof(T).IsAbstract || typeof(T).IsInterface)
116116
{
117+
var prevState = JsState.IsWritingDynamic;
117118
JsState.IsWritingDynamic = true;
118119
var result = SerializeToString(value, value.GetType());
119-
JsState.IsWritingDynamic = false;
120+
JsState.IsWritingDynamic = prevState;
120121
return result;
121122
}
122123

@@ -150,9 +151,10 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
150151
}
151152
else if (typeof(T).IsAbstract || typeof(T).IsInterface)
152153
{
154+
var prevState = JsState.IsWritingDynamic;
153155
JsState.IsWritingDynamic = false;
154156
SerializeToWriter(value, value.GetType(), writer);
155-
JsState.IsWritingDynamic = true;
157+
JsState.IsWritingDynamic = prevState;
156158
}
157159
else
158160
{
@@ -182,9 +184,10 @@ public static void SerializeToStream<T>(T value, Stream stream)
182184
}
183185
else if (typeof(T).IsAbstract || typeof(T).IsInterface)
184186
{
187+
var prevState = JsState.IsWritingDynamic;
185188
JsState.IsWritingDynamic = false;
186189
SerializeToStream(value, value.GetType(), stream);
187-
JsState.IsWritingDynamic = true;
190+
JsState.IsWritingDynamic = prevState;
188191
}
189192
else
190193
{

tests/ServiceStack.Text.Tests/JsonObjectTests.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
24
using System.Linq;
5+
using System.Text;
36
using NUnit.Framework;
47

58
namespace ServiceStack.Text.Tests
@@ -89,6 +92,45 @@ public void Does_encode_large_strings()
8992
System.Diagnostics.Debug.WriteLine(copy1["test"]);
9093
}
9194
}
95+
96+
public interface IFoo
97+
{
98+
string Property { get; }
99+
}
100+
101+
public class FooA : IFoo
102+
{
103+
public string Property { get; set; } = "A";
104+
}
105+
106+
[Test]
107+
public void Can_consistently_serialize_stream()
108+
{
109+
var item = new FooA();
110+
var result1 = SerializeToStream1(item);
111+
var result2 = SerializeToStream2(item);
112+
var result3 = SerializeToStream1(item);
113+
var result4 = SerializeToStream2(item);
114+
115+
Assert.That(result1, Is.EqualTo(result2));
116+
Assert.That(result3, Is.EqualTo(result4));
117+
}
118+
119+
// Serialize using TypeSerializer.SerializeToStream<T>(T, Stream)
120+
public static string SerializeToStream1(IFoo item)
121+
{
122+
using var stream = new MemoryStream();
123+
TypeSerializer.SerializeToStream(item, stream);
124+
return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
125+
}
126+
127+
// Serialize using TypeSerializer.SerializeToStream(object, Type, Stream)
128+
public static string SerializeToStream2(IFoo item)
129+
{
130+
using var stream = new MemoryStream();
131+
TypeSerializer.SerializeToStream(item, item.GetType(), stream);
132+
return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
133+
}
92134

93135
[Test]
94136
public void Can_parse_Twitter_response()

0 commit comments

Comments
 (0)