Permalink
Browse files

Merge pull request #250 from ShamsulAmry/master

Non-interface / non-abstract-typed property of a derived class should not have type info
  • Loading branch information...
2 parents 6ca3b35 + 067ce1f commit c4b69e8a075d1842d28640e581d644c8d7fd7a44 @mythz mythz committed Dec 9, 2012
View
24 src/ServiceStack.Text/Common/WriteType.cs
@@ -39,9 +39,13 @@ private static bool IsExcluded
static WriteType()
{
- CacheFn = Init() ? GetWriteFn() : WriteEmptyType;
+ if (typeof(T) == typeof(Object)) {
+ CacheFn = WriteObjectType;
+ } else {
+ CacheFn = Init() ? GetWriteFn() : WriteEmptyType;
+ }
- if (IsIncluded)
+ if (IsIncluded)
{
WriteTypeInfo = TypeInfoWriter;
}
@@ -181,9 +185,22 @@ internal string PropertyName
}
}
+ public static void WriteObjectType(TextWriter writer, object value)
+ {
+ writer.Write(JsWriter.EmptyMap);
+ }
+
public static void WriteEmptyType(TextWriter writer, object value)
{
- writer.Write(JsWriter.EmptyMap);
+ if (WriteTypeInfo != null || JsState.IsWritingDynamic) {
+ writer.Write(JsWriter.MapStartChar);
+ if (!(JsConfig.PreferInterfaces && TryWriteSelfType(writer))) {
+ TryWriteTypeInfo(writer, value);
+ }
+ writer.Write(JsWriter.MapEndChar);
+ return;
+ }
+ writer.Write(JsWriter.EmptyMap);
}
public static void WriteAbstractProperties(TextWriter writer, object value)
@@ -218,6 +235,7 @@ public static void WriteProperties(TextWriter writer, object value)
{
if (JsConfig.PreferInterfaces && TryWriteSelfType(writer)) i++;
else if (TryWriteTypeInfo(writer, value)) i++;
+ JsState.IsWritingDynamic = false;
}
if (PropertyWriters != null)
View
2 src/ServiceStack.Text/Json/JsonWriter.Generic.cs
@@ -99,7 +99,7 @@ public static void WriteLateBoundObject(TextWriter writer, object value)
var type = value.GetType();
var writeFn = type == typeof(object)
- ? WriteType<object, JsonTypeSerializer>.WriteEmptyType
+ ? WriteType<object, JsonTypeSerializer>.WriteObjectType
: GetWriteFn(type);
var prevState = JsState.IsWritingDynamic;
View
2 src/ServiceStack.Text/Jsv/JsvWriter.Generic.cs
@@ -61,7 +61,7 @@ public static void WriteLateBoundObject(TextWriter writer, object value)
if (value == null) return;
var type = value.GetType();
var writeFn = type == typeof(object)
- ? WriteType<object, JsvTypeSerializer>.WriteEmptyType
+ ? WriteType<object, JsvTypeSerializer>.WriteObjectType
: GetWriteFn(type);
var prevState = JsState.IsWritingDynamic;
View
50 tests/ServiceStack.Text.Tests/JsvTests/GeneratedJsvTests.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Linq;
+using NUnit.Framework;
+
+namespace ServiceStack.Text.Tests.JsvTests
+{
+ [TestFixture]
+ public class GeneratedJsvTests
+ {
+ public interface ITest
+ {}
+
+ public class TestObject : ITest
+ {
+ public TestParameter Parameter { get; set; }
+ public ITest InterfaceParameter { get; set; }
+ }
+
+ public class TestParameter : ITest
+ {
+ public string Value { get; set; }
+ }
+
+ [Test]
+ public void Interface_typed_property_in_derived_class_should_have_type_info()
+ {
+ const string expected =
+ "{__type:\"ServiceStack.Text.Tests.JsvTests.GeneratedJsvTests+TestObject, " +
+ "ServiceStack.Text.Tests\",InterfaceParameter:{__type:" +
+ "\"ServiceStack.Text.Tests.JsvTests.GeneratedJsvTests+TestParameter, " +
+ "ServiceStack.Text.Tests\",Value:Some Value}}";
+
+ ITest test = new TestObject {InterfaceParameter = new TestParameter {Value = "Some Value"}};
+ var jsv = test.ToJsv();
+ Assert.That(jsv, Is.EqualTo(expected));
+ }
+
+ [Test]
+ public void Property_in_derived_class_should_not_have_type_info()
+ {
+ const string expected =
+ "{__type:\"ServiceStack.Text.Tests.JsvTests.GeneratedJsvTests+TestObject, " +
+ "ServiceStack.Text.Tests\",Parameter:{Value:Some Value}}";
+
+ ITest test = new TestObject {Parameter = new TestParameter {Value = "Some Value"}};
+ var jsv = test.ToJsv();
+ Assert.That(jsv, Is.EqualTo(expected));
+ }
+ }
+}
View
1 tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj
@@ -182,6 +182,7 @@
<Compile Include="CsvTests\CustomHeaderTests.cs" />
<Compile Include="JsonTests\ContractByInterfaceTests.cs" />
<Compile Include="JsonTests\DictionaryTests.cs" />
+ <Compile Include="JsvTests\GeneratedJsvTests.cs" />
<Compile Include="JsvTests\InheritanceTests.cs" />
<Compile Include="JsonTests\JsonArrayObjectTests.cs" />
<Compile Include="JsonTests\JsonDateTimeTests.cs" />

0 comments on commit c4b69e8

Please sign in to comment.