C# DocumentDb client return raw json 可以不反序列化吗?

Can C# DocumentDb client return raw json with out deserialization?

我想在不进行任何反序列化的情况下查询 DocumentDb 和 return 原始 json(我没有 运行 的任何业务逻辑,因此反序列化是不必要的高架)。这是我们可以使用当前 SDK 做的事情吗?

好问题。目前,DocumentDB 不通过 SDK 提供原始 JSON。您将始终获得一个 Document 对象,或者如果您使用的是通用调用,则将获得您自己的对象类型。

在大多数情况下,反序列化为 POCO 对象是有意义的,这样它就可以在返回给客户端之前在业务代码中进行验证和使用。但是如果能够访问原始的 JSON 就更好了,因为在将其返回给客户端之前不需要任何处理。

希望对您有所帮助。

在 .NET SDK 1.9.5 中您可以访问 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;
    }
}

不幸的是,FeedResponse class 还没有提供类似的功能。有一个 UserVoice 请求 here,但目前还没有官方回应。

编辑:在我的测试中,与反序列化-序列化相比,使用 ResponseStream 的标准 "Person" 样式 class 结果快大约 20..50 毫秒。