5

I need to add KnownType to the below code for it to serialize successfully. When I do, the generated JSON is as follows:

JSON form of Adult with 1 child: {"age":42,"name":"John","children":[{"__type":"
Child:#TestJson","age":4,"name":"Jane","fingers":10}]}

How do I have it not include the "__type":"Child:#TestJson"? We return hundreds of these elements on some queries and that extra text will add up.

Full code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace TestJson
{
    class Program
    {
        static void Main(string[] args)
        {
            Adult parent = new Adult {name = "John", age = 42};

            MemoryStream stream1 = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Adult));
            ser.WriteObject(stream1, parent);

            stream1.Position = 0;
            StreamReader sr = new StreamReader(stream1);
            Console.Write("JSON form of Adult with no children: ");
            Console.WriteLine(sr.ReadToEnd());


            Child child = new Child { name = "Jane", age = 4, fingers=10 };

            stream1 = new MemoryStream();
            ser = new DataContractJsonSerializer(typeof(Child));
            ser.WriteObject(stream1, child);

            stream1.Position = 0;
            sr = new StreamReader(stream1);
            Console.Write("JSON form of Child with no parent: ");
            Console.WriteLine(sr.ReadToEnd());


            // now connect the two
            parent.children.Add(child);

            stream1 = new MemoryStream();
            ser = new DataContractJsonSerializer(typeof(Adult));
            ser.WriteObject(stream1, parent);

            stream1.Position = 0;
            sr = new StreamReader(stream1);
            Console.Write("JSON form of Adult with 1 child: ");
            Console.WriteLine(sr.ReadToEnd());
        }
    }

    [DataContract]
    [KnownType(typeof(Adult))]
    [KnownType(typeof(Child))]
    class Person
    {
        [DataMember]
        internal string name;

        [DataMember]
        internal int age;
    }

    [DataContract]
    class Adult : Person
    {
        [DataMember] 
        internal List<Person> children = new List<Person>();
    }

    [DataContract]
    class Child : Person
    {
        [DataMember]
        internal int fingers;
    }
}
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • This is one of the big reasons I use [JSON.Net](http://json.net) – Mike Christensen Jul 23 '13 at 16:27
  • @MikeChristensen: That's really not a reason, because `DataContractJsonSerializer` supports this just as well... – Daniel Hilgarth Jul 23 '13 at 16:35
  • @DanielHilgarth - Yea, I figured there was some way to control this. I just ran into so many problems with the default .NET serializer I switched to the third party library. – Mike Christensen Jul 23 '13 at 16:38
  • @MikeChristensen I take it you're happy with Newtonsoft? Any downsides to switching to it? – David Thielen Jul 23 '13 at 16:45
  • @DavidThielen - It was a complete pain in the butt to get it working with WCF, however I think overall it was worth it. – Mike Christensen Jul 23 '13 at 16:48
  • I think it's a complete PITA to get most anything working in WCF :) The docs all say that "WCF makes things sooo easy." But only if you don't do anything outside of what the WCF designers planned on. – David Thielen Jul 23 '13 at 22:46
  • possible duplicate of [How to not serialize the \_\_type property on JSON objects](http://stackoverflow.com/questions/627356/how-to-not-serialize-the-type-property-on-json-objects) – Ali Gonabadi Jun 09 '15 at 05:32

1 Answers1

14

As I told you in the last question, I don't know, but some research leads me to believe that the following might achieve what you want:

var settings = new DataContractJsonSerializerSettings();
settings.EmitTypeInformation = EmitTypeInformation.Never;

var serializer = new DataContractJsonSerializer(yourType, settings);
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443