6

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 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the quick reply. Json to Deserialized object back to json (for client), seems some unnecessary work (especially when we do not need Deserialization). Did you find a way to solve this or is this something that Microsoft has to come up with a solution? Thank you -Soma.
No, we are deserializing into POCO objects. We met with the DocDB team back in February and asked the same question and they didn't have a way around it at the time. I think I remember that they might be working on this feature for a future release. The platform is still young and they are constantly releasing new features.
I can't figure out how to deserialize the DocumentDB into my POCO even. How are you doing that?
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.