ServiceStack.Text JSON Parsing on .Net 4.0

H, I'm trying to use ServiceStack.Text to parse JSON (it seems to work better than JSON.Net in the various tests I've seen). But I do not get the expected results. The class I'm trying to deserialize is as follows:

[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 call a 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 returning an instance from this call that does not have any of the set values. those. Id, Methodand Params- all values ​​are equal to zero. Am I using this API correctly?

+3
source share
2 answers

It seems that ServiceStack does not support public fields, but only public properties. Therefore, if I change my model object to the next, it will all work.

[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; }
}

.

+9

, JsonSerializer TypeSerializer.

TypeSerializer - JSV, - : http://www.servicestack.net/mythz_blog/?p=176

+2

Source: https://habr.com/ru/post/1784940/


All Articles