ServiceStack/ServiceStack.Text — Issue #66: serializing descendant type stored in a base type property doesn't render the descendant type's properties
Author: hyun007
Created: 2012-02-06 08:11:37+00:00
State: open

serializing descendant type stored in a base type property doesn't render the descendant type's properties.

the code below renders the json string : {"Continent":"South America", "Species":{"Name":"Cat"}}
it should render instead: {"Continent":"South America", "Species":{"Name":"Tiger","Color":"Orange"}}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using ServiceStack.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var jungle = new Habitat();
jungle.Continent = "South America";
jungle.Species = new Cat {Name = "Tiger", Color = "Orange"};

Console.WriteLine(jungle.ToJson());
var input = Console.ReadLine();
}
}

[DataContract]
public class Habitat where T : Animal
{
[DataMember]
public string Continent { get; set; }
[DataMember]
public T Species { get; set; }
}

[DataContract]
public class Animal
{
[DataMember]
public string Name { get; set; }
}

[DataContract]
public class Cat : Animal
{
[DataMember]
public string Color { get; set; }
}
}