开发者

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). 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.DeserializeFro开发者_JS百科mString<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?


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.

[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.


I think you want JsonSerializer instead of TypeSerializer.

TypeSerializer is a new-fangled JSV format that Mr Mythz details on his blog here: http://www.servicestack.net/mythz_blog/?p=176

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜