Skip to content

Commit 13138f2

Browse files
committed
Don't add type info for top-level Types that are objects
1 parent f58dfd0 commit 13138f2

7 files changed

Lines changed: 104 additions & 44 deletions

File tree

512 Bytes
Binary file not shown.

src/ServiceStack.Text/Env.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static Env()
2323
+ (IsMonoTouch ? " MonoTouch" : "");
2424
}
2525

26-
public static decimal ServiceStackVersion = 2.29m;
26+
public static decimal ServiceStackVersion = 3.01m;
2727

2828
public static bool IsUnix { get; set; }
2929

src/ServiceStack.Text/JsonSerializer.cs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ServiceStack.Text/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2.2.9.*")]
35+
[assembly: AssemblyVersion("3.0.1.*")]
3636
//[assembly: AssemblyFileVersion("1.0.0.0")]

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Globalization;
1616
using System.IO;
1717
using System.Text;
18+
using ServiceStack.Text.Json;
1819
using ServiceStack.Text.Jsv;
1920

2021
namespace ServiceStack.Text
@@ -78,6 +79,7 @@ public static string SerializeToString<T>(T value)
7879
{
7980
if (value == null) return null;
8081
if (typeof(T) == typeof(string)) return value as string;
82+
if (typeof(T) == typeof(object)) return SerializeToString(value, value.GetType());
8183

8284
var sb = new StringBuilder(4096);
8385
using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
@@ -87,6 +89,19 @@ public static string SerializeToString<T>(T value)
8789
return sb.ToString();
8890
}
8991

92+
public static string SerializeToString(object value, Type type)
93+
{
94+
if (value == null) return null;
95+
if (type == typeof(string)) return value as string;
96+
97+
var sb = new StringBuilder(4096);
98+
using (var writer = new StringWriter(sb, CultureInfo.InvariantCulture))
99+
{
100+
JsvWriter.GetWriteFn(type)(writer, value);
101+
}
102+
return sb.ToString();
103+
}
104+
90105
public static void SerializeToWriter<T>(T value, TextWriter writer)
91106
{
92107
if (value == null) return;
@@ -95,24 +110,54 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
95110
writer.Write(value);
96111
return;
97112
}
113+
if (typeof(T) == typeof(object))
114+
{
115+
SerializeToWriter(value, value.GetType(), writer);
116+
return;
117+
}
98118

99119
JsvWriter<T>.WriteObject(writer, value);
100120
}
101121

102-
public static T Clone<T>(T value)
122+
public static void SerializeToWriter(object value, Type type, TextWriter writer)
103123
{
104-
var serializedValue = SerializeToString(value);
105-
var cloneObj = DeserializeFromString<T>(serializedValue);
106-
return cloneObj;
124+
if (value == null) return;
125+
if (type == typeof(string))
126+
{
127+
writer.Write(value);
128+
return;
129+
}
130+
131+
JsvWriter.GetWriteFn(type)(writer, value);
107132
}
108133

109134
public static void SerializeToStream<T>(T value, Stream stream)
110135
{
136+
if (typeof(T) == typeof(object))
137+
{
138+
SerializeToStream(value, value.GetType(), stream);
139+
return;
140+
}
141+
111142
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
112143
JsvWriter<T>.WriteObject(writer, value);
113144
writer.Flush();
114145
}
115146

147+
public static void SerializeToStream(object value, Type type, Stream stream)
148+
{
149+
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
150+
JsonWriter.GetWriteFn(type)(writer, value);
151+
writer.Flush();
152+
}
153+
154+
public static T Clone<T>(T value)
155+
{
156+
var serializedValue = SerializeToString(value);
157+
var cloneObj = DeserializeFromString<T>(serializedValue);
158+
return cloneObj;
159+
}
160+
116161
public static T DeserializeFromStream<T>(Stream stream)
117162
{
118163
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))

tests/ServiceStack.Text.Tests/ReportedIssues.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ public void Does_not_serialize_typeinfo_for_concrete_types()
188188
{
189189
var json = GetBook().ToJson();
190190
Console.WriteLine(json);
191-
Assert.That(json.IndexOf("__"), Is.EqualTo(-1));
192-
}
191+
Assert.That(json.IndexOf("__"), Is.EqualTo(-1));
192+
193+
var jsv = GetBook().ToJsv();
194+
Assert.That(jsv.IndexOf("__"), Is.EqualTo(-1));
195+
}
193196
}
194197
}

tests/ServiceStack.Text.Tests/Utils/JsvFormatterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void Can_PrettyFormat_object()
2525

2626
Console.WriteLine(modelStr);
2727

28-
Assert.That(modelStr, Is.EqualTo("{\r\n\t__type: \"ServiceStack.Common.Tests.Models.ModelWithIdAndName,\r\n\t ServiceStack.Common.Tests\",\r\n\tId: 1,\r\n\tName: Name\r\n}"));
28+
Assert.That(modelStr, Is.EqualTo("{\r\n\tId: 1,\r\n\tName: Name\r\n}"));
2929
}
3030

3131
internal class TestModel

0 commit comments

Comments
 (0)