Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

c# - JSON.NET deserialize to object with Type parameter

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...