如何在JsonFX中反序列化多态集合? [英] How to deserialize polymorphic collections in JsonFX?

查看:123
本文介绍了如何在JsonFX中反序列化多态集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JsonFX序列化代码可以工作,但是我要序列化的对象包含一个多态实体列表,并且它们都被反序列化为它们的基本类型,而不是它们的实际类型.

My JsonFX serialization code works, but the object that I'm serializing contains a list of polymorphic entities, and they're all deserialized as their base type and not their actual type.

这是我的序列化代码:

public static string Serialize(System.Object obj)
{
    StringBuilder builder = new StringBuilder();
    using (TextWriter textWriter = new StringWriter(builder))
    {
        JsonWriter writer = new JsonWriter(textWriter);
        writer.Write(obj);

        return builder.ToString();
    }
}

public static T Deserialize<T>(string json)
{
    using (TextReader textReader = new StringReader(json))
    {
        var jsonReader = new JsonReader(textReader);
        return jsonReader.Deserialize<T>();
    }
}

如您所见,它非常简单.我也没有用任何属性或使它们可序列化的特殊修饰来修饰我的类.除了多态性问题之外,这一切似乎都可以正常工作.

As you can see it's pretty straightforward. I'm also not decorating my classes with any attributes or anything special to make them serializable. Besides the polymorphic problem, it all just seems to be working properly.

那我怎样才能使我的多态类型正确反序列化?.

So how can I get my polymorphic types to deserialize properly?.

谢谢.

推荐答案

您需要打开类型提示.这是示例代码(与JsonFx v1.4相关,可能与您的版本兼容或不兼容):

You need to turn on type hinting. Here's example code (this is relevant to JsonFx v1.4, may or may not work with your version):

    StringBuilder result = new StringBuilder(string.Empty);

    JsonWriterSettings settings = JsonDataWriter.CreateSettings(true);
    settings.TypeHintName = "__type";

    JsonWriter writer = new JsonWriter(result, settings);

    writer.Write(obj);

    return result.ToString();

这会将额外的数据添加到您的JSON字符串中,如下所示:

This will add extra data to your JSON string which looks something like:

"__type": "MyNamespace.MyClass, MyAssembly",

这意味着它将根据类名称找出其派生类型.如果您更改类名称或名称空间名称,它将不再起作用.更糟糕的是,您无法再从旧的JSON文本数据中反序列化,除非您大规模替换所有出现的旧类名并将其替换为新的类名.

Which means it finds out what derived type it is based on the class name. If you change your class name or namespace name, it won't work anymore. Worse, you can't deserialize from your old JSON text data anymore, unless you mass replace all occurrences of the old class name and replace it with the new.

所以你必须要小心.

忘记提及您必须编辑JsonFx的源代码才能起作用. 在JsonReader.cs中,找到ReadArray方法:

Forgot to mention that you have to edit the source code of JsonFx for this to work. In JsonReader.cs, find the ReadArray method:

更改:

object value = this.Read(arrayItemType, isArrayTypeAHint);

收件人:

object value = this.Read(null, false);

这将确保JsonFx始终尝试找出数组/列表中每个元素的类型.如果您希望此方法仅适用于单个变量,那么您需要在适当的代码上进行更改(没有尝试过).

This will ensure that JsonFx will always attempt to figure out the type of each element in an array/list. If you want this to work for just single variables, well you'd need to do the changes on the appropriate code (haven't tried that).

这篇关于如何在JsonFX中反序列化多态集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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