문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top