ServiceStack.Text可以将JSON反序列化为自定义的通用类型吗? [英] Can ServiceStack.Text deserialize JSON to a custom generic type?

查看:47
本文介绍了ServiceStack.Text可以将JSON反序列化为自定义的通用类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是示例,其中T是一些DTO,我希望得到1 ... n返回与resultCount相匹配的内容.使用Jayrock JsonConvert可以很好地进行加载,但是在尝试反序列化与Jayrock相同的json文本时,它只是向我返回了一个新的JsonResult.

Example is the following, where T is some DTO that I expect to get 1...n back matching the resultCount. This loaded up fine using Jayrock JsonConvert, however is just returning a new JsonResult to me when trying to deserialize the same json text that worked in Jayrock.

[Serializable]
public class JsonResult<T>
{
  public int resultCount = 0;
  public T[] results;
} 

我认为这可能是我在 ServiceStack.Text中询问的问题.JsonSerializer.DeserializeFromString< T>()无法在字符串包含\ n的情况下反序列化,但是即使去掉\ n仍然不允许反序列化成功.

I thought this might be an issue that I asked about at ServiceStack.Text.JsonSerializer.DeserializeFromString<T>() fails to deserialize if string contains \n's, however even stripping out those \n's still doesn't allow deserialization to succeed.

推荐答案

ServiceStack的JSON序列化器可以支持泛型,但仅序列化公共属性,这是这里的问题.

ServiceStack's JSON Serializer does support generics but only serializes public properties which is the problem here.

[Serializable] 属性在.NET之外的大多数(所有)序列化程序中都没有意义,包括ServiceStack的所有序列化程序.

Also the [Serializable] attribute has no significance in most (all?) serailizers outside of .NET, including all of ServiceStack's serializers.

这篇关于ServiceStack.Text可以将JSON反序列化为自定义的通用类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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