JSON.NET反序列化为具有Type参数的对象 [英] JSON.NET deserialize to object with Type parameter

查看:400
本文介绍了JSON.NET反序列化为具有Type参数的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题,我无法解决:



我有不同的类,它们都实现一个名为 IProtocol 。现在,命名为 SimpleProtocol ParallelProtocol 。我想坚持那些对象,所以我使用JSON.NET和一切工作正常。除非我试图反序列化它们,当我知道它们应该是什么类型时,它工作完美,例如:

  SimpleProtocol p = JsonConvert.DeserializeObject< SimpleProtocol>(myJsonData); 

然而,我现在处于一种情况,我想加载JSON数据,并获得 IProtocol 回来,但这是可以理解的,JSON不允许;例如,类似这样的东西工作:

  IProtocol p1 = JsonConvert.DeserializeObject< IProtocol> myJsonData); //不工作
IProtocol p2 =(IProtocol)JsonConvert.DeserializeObject(myJsonData); //也不工作

因此,查找 API 我发现此方法签名:

  public static Object DeserializeObject(
string value,
Type type

看起来就像我需要的东西,所以尝试通过在字符串中持久化类型并检索它:

  // test 
类型protocolType = Type.GetType(MyApp.Protocols.SimpleProtocol);
IProtocol p1 = JsonConvert.DeserializeObject(myJsonData,protocolType);

我得到一个错误,不能投射 Newtonsoft.Json。 Linq.JObject IProtocol 。这是奇怪的,我不知道如何解决这个。



在一个通用的方法是不可能传递Type对象,所以我基本上被困在这里。有没有办法解决这个问题,最好不使用反射?它看起来对我来说是一个完全正常的用例。



我能做什么,但它似乎有点脏我是,创建一个简单的包装器类中包含一个IP协议实例,并序列化/反序列化?

解决方案

似乎我的初始方法是使用这种方法正确的:

  public static Object DeserializeObject(
字符串值,
类型类型

问题是,我坚持我的对象类型为 MyProtocol.GetType (PersistedTypeString);

$ <$>

/ code>



为具有 null 值的类型。但是使用 MyProtocol.GetType()。AssemblyQualifiedName 一切都很好(ps这也包括在Type.GetType()的文档)



这是我的代码示例:

 类型ProtocolType = Type.GetType(MetaData [ProtocolType ]); 
var Protocol = JsonConvert.DeserializeObject(Data [Protocol],ProtocolType,JsonProtocolPersister.DefaultSettings);
return(IProtocol)Protocol;


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?

解决方案

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;

这篇关于JSON.NET反序列化为具有Type参数的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆