.Net 4.0 上的 ServiceStack.Text JSON 解析
H 伙计们, 我正在尝试使用 ServiceStack.Text 进行 JSON 解析(在我见过的各种基准测试中,它的性能似乎比 JSON.Net 更好)。但我没有得到我期望的结果。我试图反序列化的类看起来像这样:
[DataContract]
public class RpcRequest<T>
{
[JsonProperty("id")]
[DataMember(Name="id")]
public String Id;
[JsonProperty("method")]
[DataMember(Name="method")]
public String Method;
[JsonProperty("params")]
[DataMember(Name="params")]
public T Params;
[JsonIgnore]
[IgnoreDataMember]
public Policy Policy;
}
我正在像这样调用解析器
public static class Json
{
public static T Deserialize<T>(string serialized)
{
return TypeSerializer.DeserializeFromString<T>(serialized);
}
}
...
RpcRequest<Params> myRequeset = Json.Deserialize(packet);
但是我从该调用中返回一个没有设置任何值的实例。即Id
、Method
和Params
都为null。我是否正确使用了这个 API?
H chaps,
I am trying to use ServiceStack.Text for JSON parsing (it seems to be performing better than JSON.Net in various benchmarks I have seen). But I am not getting the results I am expecting. The class I am attempting to deserialize looks like this:
[DataContract]
public class RpcRequest<T>
{
[JsonProperty("id")]
[DataMember(Name="id")]
public String Id;
[JsonProperty("method")]
[DataMember(Name="method")]
public String Method;
[JsonProperty("params")]
[DataMember(Name="params")]
public T Params;
[JsonIgnore]
[IgnoreDataMember]
public Policy Policy;
}
And I am invoking the parser like this
public static class Json
{
public static T Deserialize<T>(string serialized)
{
return TypeSerializer.DeserializeFromString<T>(serialized);
}
}
...
RpcRequest<Params> myRequeset = Json.Deserialize(packet);
However I am getting an instance back from that call which has none of the values set. ie Id
, Method
and Params
are all null. Am I using this API correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来ServiceStack不支持公共字段,只支持公共属性。因此,如果我将模型对象更改为以下内容,一切都会正常。
请注意为每个属性添加了 getter 和 setter。
It seems that ServiceStack does not support public fields, only public properties. So if I change my model object to the following it all works.
Note the addition of getters and setters to each property.
我认为您需要
JsonSerializer
而不是TypeSerializer
。TypeSerializer
是一种新奇的 JSV 格式,Mythz 先生在他的博客上对此进行了详细介绍:http://www.servicestack.net/mythz_blog/?p=176I think you want
JsonSerializer
instead ofTypeSerializer
.TypeSerializer
is a new-fangled JSV format that Mr Mythz details on his blog here: http://www.servicestack.net/mythz_blog/?p=176