-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableEntity.cs
57 lines (45 loc) · 1.88 KB
/
TableEntity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
namespace Azure.Storage.Table.Test
{
public abstract class TableEntity : ITableEntity
{
private readonly JsonSerializer _json = new JsonSerializer();
public string PartitionKey { get; set; }
public string RowKey { get; set; }
[JsonIgnore]
public string TableName { get; }
protected TableEntity() { }
internal TableEntity(string tableName) => TableName = tableName;
public TableQueryResult<TResult> DeSerialize<TResult>(Stream stream, TablePaginationToken paginationToken) where TResult : class
{
if (stream == null || !stream.CanRead) return null;
using (var sr = new StreamReader(stream, Encoding.UTF8))
{
using (var reader = new JsonTextReader(sr) { DateParseHandling = DateParseHandling.None })
{
var queryResult = _json.Deserialize<TableEntityCollection<TResult>>(reader);
return new TableQueryResult<TResult>(queryResult?.Results, paginationToken);
}
}
}
public TableResult<TResult> DeSerialize<TResult>(Stream stream, HttpStatusCode statusCode) where TResult : class
{
if (stream == null || !stream.CanRead) return null;
using (var reader = new StreamReader(stream))
{
using (var json = new JsonTextReader(reader) { DateParseHandling = DateParseHandling.None })
{
return new TableResult<TResult>(_json.Deserialize<TResult>(json), statusCode);
}
}
}
public HttpContent Serialize(object entity)
{
return new StringContent(JsonConvert.SerializeObject(entity), Encoding.UTF8, "application/json");
}
}
}