2014-02-28 87 views
0

我正在寫一個Wp8/C#庫來查詢MongoLab的REST Api。 我有一個abtract對象是這樣的:如何序列化/反序列化對象

[DataContract] 
public abstract class Entity 
{ 
    [DataMember(Name = "_id")] 
    public string _id { get; set; } 
} 

字段_id是自動生成由蒙戈作爲的ObjectId。但隨着WP8,我沒有MongoDB的C#驅動程序...序列化和反序列化不工作....

這是我已經試過:

var str = url; 
var response = await _httpClient.GetAsync(str); 
var rep = await response.Content.ReadAsStringAsync(); 
return JsonConvert.DeserializeObject<T>(rep); 

我已經也試用Datacontractjsonserializer。

我該怎麼做?

謝謝

+0

您不需要在標題中放置標籤信息,也就是標籤的用途。 – crashmstr

+0

你有什麼嘗試過 - 你說序列化等不起作用 - 你可以分享不起作用的代碼嗎? – ScruffyDuck

+1

你不能序列化/反序列化抽象類。 –

回答

1

這是一類,我寫了處理JSON序列化和反序列化在.net 3.5 不要忘了添加引用System.ServiceModel.Web.dll

你可以使用JsonTools.ObjectToJsonString(rep);

using System; 
using System.Text; 
using System.Runtime.Serialization.Json; 
using System.IO; 

namespace Utilities 
{ 
    /// <summary> 
    /// Group of static methods for dealing with JSON. 
    /// </summary> 
    public static class JsonTools 
    { 
     /// <summary> 
     /// Serializes an object to JSON string. 
     /// </summary> 
     /// <param name="obj">The object to serialize. </param> 
     /// <returns></returns> 
     /// <exception cref="System.Runtime.Serialization.InvalidDataContractException"></exception> 
     /// <exception cref="System.Runtime.Serialization.SerializationException"></exception> 
     /// <exception cref="System.ServiceModel.QuotaExceededExceptionn"></exception>   
     public static string ObjectToJsonString(object obj) 
     { 
      try 
      { 
       MemoryStream jsonStream = new MemoryStream(); 
       DataContractJsonSerializer js = new DataContractJsonSerializer(obj.GetType()); 
       js.WriteObject(jsonStream, obj); 
       jsonStream.Position = 0; 

       StreamReader sr = new StreamReader(jsonStream); 
       return sr.ReadToEnd(); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 

     /// <summary> 
     /// Serializes an object to JSON byte array. 
     /// </summary> 
     /// <param name="obj">The object to serialize. </param> 
     /// <returns></returns> 
     /// <exception cref="System.Runtime.Serialization.InvalidDataContractException"></exception> 
     /// <exception cref="System.Runtime.Serialization.SerializationException"></exception> 
     /// <exception cref="System.ServiceModel.QuotaExceededExceptionn"></exception> 
     public static byte[] ObjectToJsonByteArray(object obj) 
     { 
      try 
      { 
       MemoryStream jsonStream = new MemoryStream(); 
       DataContractJsonSerializer js = new DataContractJsonSerializer(obj.GetType()); 
       js.WriteObject(jsonStream, obj); 
       jsonStream.Position = 0; 

       return jsonStream.ToArray(); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 


     /// <summary> 
     /// Deserializes a JSON formatted string to an object of the defined type 
     /// </summary> 
     /// <param name="jsonString">JSON formatted string</param> 
     /// <param name="objType">The type of the object which the jsonString is to be Deserialized to.</param> 
     /// <returns>Deserialized object</returns> 
     /// <exception cref="System.Runtime.Serialization.SerializationException"></exception> 
     public static object JsonStringToObject(string jsonString, Type objType) 
     { 
      try 
      { 
       DataContractJsonSerializer js = new DataContractJsonSerializer(objType); 
       byte[] jsonBytes = Encoding.Default.GetBytes(jsonString); 
       MemoryStream jsonStream = new MemoryStream(jsonBytes); 

       return js.ReadObject(jsonStream); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 

     /// <summary> 
     /// Deserializes a JSON formatted byte array to an object of the defined type 
     /// </summary> 
     /// <param name="jsonBytes">JSON formatted byte array</param> 
     /// <param name="objType">The type of the object which the jsonString is to be Deserialized to.</param> 
     /// <returns>Deserialized object</returns> 
     /// <exception cref="System.Runtime.Serialization.SerializationException"></exception> 
     public static object JsonByteArrayToObject(byte[] jsonBytes, Type objType) 
     { 
      try 
      { 
       DataContractJsonSerializer js = new DataContractJsonSerializer(objType); 
       MemoryStream jsonStream = new MemoryStream(jsonBytes); 

       return js.ReadObject(jsonStream); 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 


    } 
} 
+0

它的工作,但我的_id是一個字典[字符串,對象],它不適用於這個類... – mcdave

+0

我不認爲你可以做到這一點與通用類型的對象 – CathalMF