|
1 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
2 | 4 | using System.Linq; |
| 5 | +using System.Text; |
3 | 6 | using NUnit.Framework; |
4 | 7 |
|
5 | 8 | namespace ServiceStack.Text.Tests |
@@ -89,6 +92,45 @@ public void Does_encode_large_strings() |
89 | 92 | System.Diagnostics.Debug.WriteLine(copy1["test"]); |
90 | 93 | } |
91 | 94 | } |
| 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 | + } |
92 | 134 |
|
93 | 135 | [Test] |
94 | 136 | public void Can_parse_Twitter_response() |
|
0 commit comments