| 

.NET C# Java Javascript Exception

9

Base Class & Implementations

public abstract class BaseFoo
{
 public string FooBarBuzz { get; set; }
}

public class AFoo : BaseFoo
{
 public string A { get; set; }
}

public class BFoo : BaseFoo
{
 public string B { get; set; }
}

When we deserilize it we need somehow a “marker”-property and I use the “FooBarBuzz” property from the BaseFoo-class as my marker. If it contains “A”, then it is implementation “AFoo” and “B” for “BFoo”.

Serialize

The serialization part works as expected:

AFoo a = new AFoo();
a.FooBarBuzz = "A";
a.A = "Hello World";

BFoo b = new BFoo();
b.FooBarBuzz = "B";
b.B = "Hello World";

List<BaseFoo> allFoos = new List<BaseFoo>();
allFoos.Add(a);
allFoos.Add(b);

var result = JsonConvert.SerializeObject(allFoos);

Resulting Json:

x

Deserialize and boom…

If we continue now and just use the typical code we get a nice exception:

var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result);

Exception:

Additional information: Could not create an instance of type ConsoleApplication6.BaseFoo. Type is an interface or abstract class and cannot be instantiated. Path ‘[0].A’, line 1, position 6.

Introducing a simple JsonConverter

The exception is pretty clear, because JSON.NET has no knowledge about our convention. The way to go* is to write a JsonConverter. I found a couple of implementations on StackOverflow, but this one seems to be the easiest and best option.

* Small Update based on the comment from Michael: JSON.NET can also include the TypeName in the JSON as a “$type” property. If this is not an issue for you, then just add the “TypeNameHandling.Auto” to the JsonSerializerSettings and it will work. In my actual scenario I want a clean JSON output, without JSON.NET magic. That’s the reason for the converter.

public class FooConverter : JsonConverter
{
 public override bool CanConvert(Type objectType)
 {
 return (objectType == typeof(BaseFoo));
 }

 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
 JObject jo = JObject.Load(reader);
 if (jo["FooBarBuzz"].Value<string>() == "A")
 return jo.ToObject<AFoo>(serializer);

 if (jo["FooBarBuzz"].Value<string>() == "B")
 return jo.ToObject<BFoo>(serializer);

 return null;
 }

 public override bool CanWrite
 {
 get { return false; }
 }

 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
 throw new NotImplementedException();
 }
}

When we read the JSON we look for our “FooBarBuzz”-property and serialize it to the correct implementation.

Let’s use our new JsonConverter

Just create a new instance of the Converter and apply it to the DeserializeObject method and you are done.

JsonConverter[] converters = { new FooConverter()};
var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result, new JsonSerializerSettings() { Converters = converters });

Easy, right?

The full sample can be viewed on GitHub

.net web csharp asp.net javascript html5 asp.net-mvc jquery css iis
5 Meinungen
0
free online euchre games https://myeuchre.com eurchre is perfect past time right after a hard day at work, especially for those that do not want to use TV or radio to unwind.
0
I am a big fan of online nba games, So i trust the portal nba locker codes for getting all hack and cheats for this game.
0
Thank you for sharing your details and experience. I think it very good for me. bloons tower defense 5
super smash flash 2
0
For instance, if you planned to revise for two subjects each day and you have set 4 hours for revision. You can spend 45 minutes studying one subject and give yourself 15 minutes break before starting once again. This will give you 4 slots of 1 hour to revise any of the two with respect to the material you have. Can someone to write my University essay now
0
Much gratitude to you such an incredible sum for sharing! I will form a post soon about continuing with your direction top advertising agencies in pakistan.
Schreibe einen Kommentar:
Themen:
iis css jquery asp.net-mvc html5 javascript asp.net csharp web .net
Entweder einloggen... ...oder ohne Wartezeit registrieren
Benutzername
Passwort
Passwort wiederholen
E-Mail
TOP TECHNOLOGIES CONSULTING GmbH