Smarter TypeNameHandling.Auto

Sep 25, 2011 at 5:32 AM

I have a type with property defined as ICollection<T>

Which is serialized as:

"Categories": {
   "$type":
"System.Collections.Generic.List`1[[ReRSS.Data.Models.Category,
ReRSS.Data]], mscorlib",
   "$values": [
     {
       "Name": "News",
       "Slugified": "news"
     }
   ]
 }

TypeNameHandling is set to Auto, obviously.

My suggestion is that if JSON.Net is already going to be using this collection type by default, it can omit the type declaration for that.

I looked at the code, and it seems like it is only possible to do so by directly modifying the SerializeList() method in JsonSerializerInternalWriter

This is a pretty common thing to happen, and it will produce much nicer JSON for the common case. Is it possible?

Dec 28, 2011 at 6:34 PM

I have the same problem.  For collections (List<> and Dictionary<>), I want it to generate JSON without the $type, but I would like $type for other objects.  I am also using Boo.Lang.List and Boo.Lang.Hash (which are coming from DSL generated data).  How can I get my collections from having $type?

Dec 29, 2011 at 9:00 AM
What we did in RavenDB is to post process the json to remove $type definition if the values are known.

On Wed, Dec 28, 2011 at 9:34 PM, chrisvire <notifications@codeplex.com> wrote:

From: chrisvire

I have the same problem. For collections (List<> and Dictionary<>), I want it to generate JSON without the $type, but I would like $type for other objects. I am also using Boo.Lang.List and Boo.Lang.Hash (which are coming from DSL generated data). How can I get my collections from having $type?

Read the full discussion online.

To add a post to this discussion, reply to this email (Json@discussions.codeplex.com)

To start a new discussion for this project, email Json@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Dec 30, 2011 at 2:05 AM

FYI Json.NET no longer includes the type name for List<T> and Dictionary<K, V> values on properties that use their equivalent interfaces.

Jan 3, 2012 at 5:13 PM
Edited Jan 3, 2012 at 5:19 PM

What I really what to do is white-list specific classes that I want $type to be included.  There are a series of classes that subclass from an abstract class.  I have a container class of those subclasses.

 

public class EntryDetail
{
}

public abstract class Entry
{
	public string Id { get; set; }
	public IList<EntryDetail> Details { get; set; }
}

public class Container
{
	Public IList<Entry> Entries { get; set; }
}

 

So I want the serialized Entry objects to include $type, but only those instances and not its contained objects (i.e. not EntryDetail).  How would I do that?

I currently got this working with a series of very fragile tweaks (basically black-listing): one request using TypeNameHandling.Auto, another using TypeNameHandling.Objects, and introducing a BooHashConverter which doesn't serialize out the $type.  It isn't maintainable at all.

Coordinator
Jan 3, 2012 at 10:16 PM

You can specify type name handling on an individual property using the JsonProperty attribute. Does that not do what you want?

Jan 4, 2012 at 10:47 AM

I tried using JsonProperty, but I couldn't figure out how to get the attribute to apply to the item in the collection, not the collection itself.

public class Container
{
    [JsonProperty(TypeNameHandling = TypeNameHandling.Auto)]
    public IList<Entry> Entries { get; set; }
}
This has no affect on the Entry class (or the IList for that matter).

Jan 7, 2014 at 1:23 AM
This seems to still be a problem, anyone got a solution ?