简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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