TypeNameHandling in DefaultTranscoder


#1

Currently, in DefaultTranscoder, the incoming and outgoing serialzation contracts are passed in from ClientConfiguration, but I can’t see where other serialization options can be set, such as TypeNameHandling. Without access to those settings, it is currently not possible to deserialize a collection of sub-typed objects, such as

public class Garage
{
    public List<Car> Cars { get; set; }
}

public class Ford : Car
{
    public string FordSpecificThing { get; set; }
}

public class Honda : Car
{
    public string HondaSpecificThing { get; set; }
}

var garage = new Garage();
garage.Cars.Add(new Ford { FordSpecificThing = "TestFord" });
garage.Cars.Add(new Honda { HondaSpecificThing = "TestHonda" });

That gives you something like this document:

{  Cars: [
    { FordSpecificThing: "TestFord" },
    { HondaSpecificThing: "TestHonda"}
    ]
}

However, because you cannot set TypeNameHandling in the deserializer, this object does not properly deserialize, because the subtypes cannot be determined without TypeNameHandling.Objects (with the corresponding $type elements in the JSON) or the like. This option can be set via the IContractResolver, but it does not take effect with top level objects, like these are (I’ve tried).

My question is, is there another way to use a different ITypeTranscoder that I can build to account for this, or are there other ways to deserialize besides the normal Get and GetDocument calls? It looks like ITypeTranscoder was built to be injected, but I can’t see where it could be, since DefaultTranscoder is instantiated directly.

I have altered this in my personal fork to take JsonSerializerSettings instead of IContractResolver. I could put out a Pull Request if you want to see it, though.

-Matt


#2

Hi Matt -

We are actually going to refactor that portion of the code so that the serialization mechanism can be customized via your own implementation. Here is the ticket: http://www.couchbase.com/issues/browse/NCBC-676

Once this is in place, there will be a default serializer that ships with the client based off of NewtonSoft that allows the JsonSerilizationSettings to be modified.

This is slated for 2.0.0, but may happen in 2.0.X.

Also, yes fill free to send a pull request :smile:

-Jeff