I have the following problem which I am unable to solve:
I have different classes which all implement an interface named IProtocol
. The are named, for now, SimpleProtocol
, ParallelProtocol
. I wanted to persist those object so I used JSON.NET and everything works fine. Except when I am trying to deserialize them it works perfectly when I know the type they are supposed to be, for instance:
SimpleProtocol p = JsonConvert.DeserializeObject<SimpleProtocol>(myJsonData);
However, I am now in a situation where I want to load the JSON data and get an IProtocol
back, but that is, understandably, not allowed by JSON; E.g., something like this does not work:
IProtocol p1 = JsonConvert.DeserializeObject<IProtocol>(myJsonData); // does not work
IProtocol p2 = (IProtocol)JsonConvert.DeserializeObject(myJsonData); // also, does not work
So, looking up the API I found this method signature:
public static Object DeserializeObject(
string value,
Type type
)
which looks just like the thing I needed, so trying out by also persisting the type in a string and retrieving it:
// test
Type protocolType = Type.GetType("MyApp.Protocols.SimpleProtocol");
IProtocol p1 = JsonConvert.DeserializeObject(myJsonData, protocolType);
I get an error that it is impossible to cast a Newtonsoft.Json.Linq.JObject
to IProtocol
. This is weird and I don't know how to solve this.
It is impossible to pass the Type object in a generic method, so I am basically stuck here. Is there a method to solve this, preferably without using Reflection? It looks to me that this is a perfectly normal use case.
What I can do, but it seems a bit 'dirty' to me, is to create a simple wrapper class which holds an IProtocol instance in it and serialize / deserialize that?
See Question&Answers more detail:
os