I want to query the DocumentDb and return the raw json with out any deserialization (I don't have any business logic to run, so deserialization would be an unnecessary overhead). Is this something we can do with current SDK?
2 Answers
Good question. At this time, DocumentDB does not offer raw JSON through the SDK. You will always either get a Document object or if you are using a generic call, your own object type.
In most cases it makes sense to deserialize into a POCO object so it can be validated and used in business code prior to returning to the client. But it would be nice to have access to the raw JSON where there isn't any processing needed before returning it to the client.
Hope this helps.
3 Comments
DocumentDB into my POCO even. How are you doing that?In .NET SDK 1.9.5 you can access ResourceResponse.ResponseStream:
[HttpGet]
public async Task<HttpResponseMessage> GetDocumentById(string id)
{
var documentUri = UriFactory.CreateDocumentUri(database.Id, collection.Id, id);
var resourceResponse = await _client.ReadDocumentAsync(documentUri);
resourceResponse.ResponseStream.Position = 0;
using (StreamReader reader = new StreamReader(resourceResponse.ResponseStream, Encoding.UTF8))
{
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(reader.ReadToEnd(), Encoding.UTF8, "application/json");
return response;
}
}
Unfortunately, FeedResponse class does not offer similar functionality yet. There is an UserVoice request here, but no official response so far.
Edit: in my tests I am seeing roughly 20..50ms faster results for standard "Person" style class with ResponseStream, compared to deserialization-serialization.