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

Commit ead7eaa

Browse files
committed
Merge pull request #363 from flq/polymorphic_dto
Proper serialization of an object implementing IEnumerable<T>
2 parents 6562428 + 1678410 commit ead7eaa

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/ServiceStack.Text/Common/JsWriter.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,14 @@ private WriteObjectDelegate GetCoreWriteFn<T>()
403403

404404
return (w, x) => writeFn(w, x, keyWriteFn, valueWriteFn);
405405
}
406+
}
406407

407-
var enumerableInterface = typeof(T).GetTypeWithGenericTypeDefinitionOf(typeof(IEnumerable<>));
408-
if (enumerableInterface != null)
409-
{
410-
var elementType = enumerableInterface.GenericTypeArguments()[0];
411-
var writeFn = WriteListsOfElements<TSerializer>.GetGenericWriteEnumerable(elementType);
412-
return writeFn;
413-
}
408+
var enumerableInterface = typeof(T).GetTypeWithGenericTypeDefinitionOf(typeof(IEnumerable<>));
409+
if (enumerableInterface != null)
410+
{
411+
var elementType = enumerableInterface.GenericTypeArguments()[0];
412+
var writeFn = WriteListsOfElements<TSerializer>.GetGenericWriteEnumerable(elementType);
413+
return writeFn;
414414
}
415415

416416
var isDictionary = typeof(T) != typeof(IEnumerable) && typeof(T) != typeof(ICollection)

tests/ServiceStack.Text.Tests/JsonTests/PolymorphicListTests.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.IO;
45
#if !MONOTOUCH
56
using System.Runtime.Serialization.Json;
67
#endif
7-
using System.Text;
88
using System.Text.RegularExpressions;
99
using NUnit.Framework;
10-
using ServiceStack.Text.Common;
10+
using System.Linq;
1111

1212
namespace ServiceStack.Text.Tests.JsonTests
1313
{
@@ -75,6 +75,41 @@ public string Name
7575
}
7676
}
7777

78+
public interface ITerm { }
79+
80+
public class FooTerm : ITerm { }
81+
82+
public class Terms : IEnumerable<ITerm>
83+
{
84+
private readonly List<ITerm> _list = new List<ITerm>();
85+
86+
public Terms()
87+
: this(Enumerable.Empty<ITerm>())
88+
{
89+
90+
}
91+
92+
public Terms(IEnumerable<ITerm> terms)
93+
{
94+
_list.AddRange(terms);
95+
}
96+
97+
public IEnumerator<ITerm> GetEnumerator()
98+
{
99+
return _list.GetEnumerator();
100+
}
101+
102+
IEnumerator IEnumerable.GetEnumerator()
103+
{
104+
return GetEnumerator();
105+
}
106+
107+
public void Add(ITerm term)
108+
{
109+
_list.Add(term);
110+
}
111+
}
112+
78113
[TestFixture]
79114
public class PolymorphicListTests : TestBase
80115
{
@@ -456,5 +491,16 @@ public void Can_serialize_and_deserialize_an_entity_containing_a_polymorphic_ite
456491
Assert.That(((Collie)deserialized.Dog).IsLassie, Is.True);
457492
}
458493

494+
[Test]
495+
public void polymorphic_serialization_of_class_implementing_generic_ienumerable_works_correctly()
496+
{
497+
var terms = new Terms {new FooTerm()};
498+
var output = JsonSerializer.SerializeToString(terms);
499+
Log(output);
500+
Assert.IsTrue(output.Contains("__type"));
501+
var terms2 = JsonSerializer.DeserializeFromString<Terms>(output);
502+
Assert.IsAssignableFrom<FooTerm>(terms2.First());
503+
}
504+
459505
}
460506
}

0 commit comments

Comments
 (0)