【发布时间】:2013-02-17 05:06:54
【问题描述】:
我目前正在使用一个控制台应用程序,我正在使用 HttpClient 与 Apache CouchDB 数据库进行交互。我正在使用这篇文章:http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client
当我通过 PostAsJsonSync 序列化文档并将文档发送到我的数据库时,我想忽略我的类中的 null 属性,但我不确定如何:
public static HttpResponseMessage InsertDocument(object doc, string name, string db)
{
HttpResponseMessage result;
if (String.IsNullOrWhiteSpace(name)) result = clientSetup().PostAsJsonAsync(db, doc).Result;
else result = clientSetup().PutAsJsonAsync(db + String.Format("/{0}", name), doc).Result;
return result;
}
static HttpClient clientSetup()
{
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential("**************", "**************");
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("*********************");
//needed as otherwise returns plain text
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
这是我要序列化的类....
class TestDocument
{
public string Schema { get; set; }
public long Timestamp { get; set; }
public int Count { get; set; }
public string _rev { get; set; }
public string _id { get; set; } - would like this to be ignored if null
}
非常感谢任何帮助。
【问题讨论】:
-
在幕后,您可能正在使用 Json.NET。 stackoverflow.com/questions/9819640/…
-
知道如何引用它正在使用的序列化程序吗?
-
在将文档放入 CouchDB 时看起来和我的问题一模一样