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?
NET objects (deserialize) A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.
It returns JSON data in string format. In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data.
SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. Namespace: Newtonsoft.Json.
Provides methods for converting between . NET types and JSON types.
It seemed that my initial approach by using this method was correct after all:
public static Object DeserializeObject( string value, Type type )
The problem was that I persisted my object type as using MyProtocol.GetType().FullName
which resulted in a value following from
Type protocolType = Type.GetType(PersistedTypeString);
to be a Type with null
values. However by using MyProtocol.GetType().AssemblyQualifiedName
everything works just fine (p.s. this is also included in the docs of Type.GetType())
Here is my code sample:
Type ProtocolType = Type.GetType(MetaData["ProtocolType"]); var Protocol = JsonConvert.DeserializeObject(Data["Protocol"], ProtocolType, JsonProtocolPersister.DefaultSettings); return (IProtocol)Protocol;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With