简体   繁体   English

ServiceStack.Text在.Net 4.0上解析JSON

[英]ServiceStack.Text JSON parsing on .Net 4.0

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). H chaps,我正在尝试使用ServiceStack.Text进行JSON解析(在我看到的各种基准测试中,它似乎比JSON.Net表现得更好)。 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. IdMethodParams都为空。 Am I using this API correctly? 我正确使用此API吗?

It seems that ServiceStack does not support public fields, only public properties. 似乎ServiceStack不支持公共字段,只支持公共属性。 So if I change my model object to the following it all works. 因此,如果我将模型对象更改为以下内容,则一切正常。

[DataContract]
public class RpcRequest<T>
{
    [JsonProperty("id")]
    [DataMember(Name="id")]
    public String Id { get; set; }

    [JsonProperty("method")]
    [DataMember(Name="method")]
    public String Method { get; set; }

    [JsonProperty("params")]
    [DataMember(Name="params")]
    public T Params { get; set; }

    [JsonIgnore]
    [IgnoreDataMember]
    public Policy Policy { get; set; }
}

Note the addition of getters and setters to each property. 请注意,为每个属性添加getter和setter。

I think you want JsonSerializer instead of TypeSerializer . 我想你想要JsonSerializer而不是TypeSerializer

TypeSerializer is a new-fangled JSV format that Mr Mythz details on his blog here: http://www.servicestack.net/mythz_blog/?p=176 TypeSerializer是一种新奇的JSV格式,Mythz先生在他的博客上详细介绍了这些格式: http//www.servicestack.net/mythz_blog/? p = 176

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM