2017-04-19 127 views
2

,因爲我讀了最新RestSharp readme.txt文件:「RESTClient實現」不包含「JsonSerializer」的定義,並沒有擴展方法「JsonSerializer」

*** IMPORTANT CHANGE IN RESTSHARP VERSION 103 *** 

In 103.0, JSON.NET was removed as a dependency. 

If this is still installed in your project and no other libraries depend on 
it you may remove it from your installed packages. 

There is one breaking change: the default Json*Serializer* is no longer 
compatible with Json.NET. To use Json.NET for serialization, copy the code 
from https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs 
and register it with your client: 

var client = new RestClient(); 
client.JsonSerializer = new YourCustomSerializer(); 

The default Json*Deserializer* is mostly compatible, but it does not support 
all features which Json.NET has (like the ability to support a custom [JsonConverter] 
by decorating a certain property with an attribute). If you need these features, you 
must take care of the deserialization yourself to get it working. 

林已經與Nu包管理器安裝Newtonsoft.Json,並試圖註冊Json.NET客戶端變量,但沒有奏效。這裏是我的代碼:

private void Form1_Load(object sender, EventArgs e) 
     {   
      var client = new RestClient("http://homestead.app/vendor"); 
      client.JsonSerializer = new JsonSerializer(); <-- HERE IS THE ERROR 
      var request = new RestRequest("", Method.GET); 
      IRestResponse response = client.Execute(request); 
      var content = response.Content; // raw content as string 
      textBox1.Text = content; 
     } 

client.JsonSerializer屬性不可用。

請幫助我。感謝

回答

2

爲RestSharp該串行器必須實現兩個接口:

  • RestSharp.Serializers.ISerializer
  • RestSharp.Serializers.IDeserializer

必須從Newtonsoft包裹串行這些輸入接口。

有從一個項目我從事的工作代碼:

/// <summary> 
/// Default JSON serializer for request bodies 
/// Doesn't currently use the SerializeAs attribute, defers to Newtonsoft's attributes 
/// </summary> 
/// <remarks> 
/// Based on http://blog.patrickmriley.net/2014/02/restsharp-using-jsonnet-serializer.html 
/// </remarks> 
public class RestSharpJsonNetSerializer : RestSharp.Serializers.ISerializer, RestSharp.Deserializers.IDeserializer 
{ 
    private readonly JsonSerializer serializer; 

    /// <summary> 
    /// Default serializer 
    /// </summary> 
    public RestSharpJsonNetSerializer() 
    { 
     this.ContentType = "application/json"; 
     this.serializer = new JsonSerializer 
     { 
      MissingMemberHandling = MissingMemberHandling.Ignore, 
      NullValueHandling = NullValueHandling.Include, 
      DefaultValueHandling = DefaultValueHandling.Include 
     }; 
    } 

    /// <summary> 
    /// Default serializer with overload for allowing custom Json.NET settings 
    /// </summary> 
    public RestSharpJsonNetSerializer(JsonSerializer serializer) 
    { 
     this.ContentType = "application/json"; 
     this.serializer = serializer; 
    } 

    /// <summary> 
    /// Unused for JSON Serialization 
    /// </summary> 
    public string DateFormat { get; set; } 

    /// <summary> 
    /// Unused for JSON Serialization 
    /// </summary> 
    public string RootElement { get; set; } 

    /// <summary> 
    /// Unused for JSON Serialization 
    /// </summary> 
    public string Namespace { get; set; } 

    /// <summary> 
    /// Content type for serialized content 
    /// </summary> 
    public string ContentType { get; set; } 

    /// <summary> 
    /// Serialize the object as JSON 
    /// </summary> 
    /// <param name="obj">Object to serialize></param> 
    /// <returns>JSON as String</returns> 
    public string Serialize(object obj) 
    { 
     using (var stringWriter = new StringWriter()) 
     { 
      using (var jsonTextWriter = new JsonTextWriter(stringWriter)) 
      { 
       jsonTextWriter.Formatting = Formatting.Indented; 
       jsonTextWriter.QuoteChar = '"'; 

       this.serializer.Serialize(jsonTextWriter, obj); 

       var result = stringWriter.ToString(); 
       return result; 
      } 
     } 
    } 

    public T Deserialize<T>(RestSharp.IRestResponse response) 
    { 
     using (var strReader = new StringReader(response.Content)) 
     { 
      using (var jsonReader = new JsonTextReader(strReader)) 
      { 
       var data = this.serializer.Deserialize<T>(jsonReader); 
       return data; 
      } 
     } 
    } 
} 
+0

你好,我仍然得到同樣的確切的錯誤。並且client.JsonSerializer屬性仍然不可用。 – tonywei

+2

'JsonSerializer'屬於'RestRequest'屬性而不屬於'RestClient'。 – TcKs

0

readme.txt更新至少在GitHub上,但包裝仍然包含舊代碼。

所以@TcKs的答案是正確的:

var request = new RestRequest(); 
request.JsonSerializer = new Shared.JsonSerializer(); 
var client = new RestClient(); 
client.Post(request); 
相關問題