将JSON对象反序列化为Silverlight中的POCO对象

时间:2012-08-01 15:40:19

标签: silverlight json.net signalr

我正在尝试反序列化Silverlight客户端中的一些JSON对象,这些对象通过SignalR发送到客户端。

使用TypeNameHandling = TypeNameHandling.All设置序列化了这些对象。

第一个问题是您无法将类库项目引用添加到Silverlight项目中,因此我使用了创建包含指向POCO类的链接的Silverlight类库的技巧。

但是反序列化时出现错误,因为JSON字符串包含完整类型,包括程序集(不同)。如果我操纵JSON字符串并替换程序集名称,仍会得到相同的错误。

这些是POCO:

public abstract class MessageBase
{
    public Exception Exception { get; set; }
}    

public class AccessRCImported : MessageBase
{
    public DateTime Cob { get; set; }
}

这是JSON字符串:

{"$type":"CS.RAR.ICE.Common.Messages.AccessRCImported, CS.RAR.ICE","Cob":"2012-08-01T00:00:00","Exception":null}

已由

序列化
var output = JsonConvert.SerializeObject(message,
                                                 new JsonSerializerSettings
                                                     {TypeNameHandling = TypeNameHandling.All});
// Replace the assembly name
var result = output.Replace(", CS.RAR.ICE.Common", ", CS.RAR.ICE");
var ctx = ConnectionManager.GetHubContext<ServerHub>();
ctx.Clients.Notify(result);

然后在Silverlight客户端上,我正在做

var message = JsonConvert.DeserializeObject< MessageBase>(onData, _serializerSettings);

我得到以下错误。 Silverlight应用程序和Silverlight客户端库都针对Silverlight 5。

有什么想法吗?

感谢。

{Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON 'CS.RAR.ICE.Common.Messages.AccessRCImported, CS.RAR.ICE'. Path '$type', line 1, position 66. ---> System.IO.FileLoadException: Could not load file or assembly 'CS.RAR.ICE, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The requested assembly version conflicts with what is already bound in the app domain or specified in the manifest. (Exception from HRESULT: 0x80131053) at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.Load(String assemblyString) at Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey(TypeNameKey typeNameKey) at Newtonsoft.Json.Utilities.ThreadSafeStore 2.AddValue(TKey键)    在Newtonsoft.Json.Utilities.ThreadSafeStore 2.Get(TKey key) at Newtonsoft.Json.Serialization.DefaultSerializationBinder.BindToType(String assemblyName, String typeName) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at CS.RAR.ICE.ClientHub.Handle(String onData)}

0 个答案:

没有答案