Couchbase:使用 QueryAsync<string>(query) 失败 "Error reading string. Unexpected token"

Couchbase: using QueryAsync<string>(query) fails with "Error reading string. Unexpected token"

使用 Couchbase 通用 QueryAsync 时,我希望得到一个正常的、未解析的字符串,而不是 JSON 已解析的字符串。

但是,在进行此调用时,似乎 Couchbase SDK for .NET 确实使用了 Newtonsoft,并试图将其解释为 JSON(顺便说一句,它是一个有效的 JSON 字符串!):

string query = $"SELECT * FROM myCollection WHERE id = {r.Next(1, 100000)}";
await scope.QueryAsync<string>(query); // <-- fails here

Newtonsoft.Json.JsonReaderException: 'Error reading string. Unexpected token: StartObject. Path '', line 6, position 1.'

如果我改为这样做,它运行良好并且 JSON 被正确解析(来自 myCollection 的所有文档都是有效的 JSON 字符串):

string query = $"SELECT * FROM myCollection WHERE id = {r.Next(1, 100000)}";
await scope.QueryAsync<JObject>(query); // <-- No problem

-- 编辑--

我也尝试了他们的 Keu Value 操作:

var result = await collection.GetAsync(key);
var content = result.ContentAs<string>(); // <-- fails here with same error

同样使用这种方法,我只是 copied from here,失败了。

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '', line 1, position 1.'

不过 JSON 是正确的,我已经在不同的解析器中进行了验证。

是否可以从 Couchbase 检索原始的、未解析的数据?

我刚找到 this on Couchbase forums:

When using SDK 3.x when trying to get a JSON document from Couchbase and using var result = data.ContentAs(); It always return this error message: “Unexpected character encountered while parsing value: {. Path ‘’, line 1, position 1.”

那里提出的解决方案非常复杂,让我觉得有悖常理:

JMorris 写道:

Hi @Brian_Davis -

The Transcoder API was redesigned in SDK3; its now much more specialized and this type of conversion doesn’t work with the default JsonTranscoder. Fortunately, we do have another transcoder which mimics the sdk2 behavior called the LegacyTranscoder which should be able to handle this for you.

复杂的解决方案似乎是将代码转换器传递到每个调用的选项中:

var result = await collection.GetAsync(key, options: new GetOptions().Transcoder(new LegacyTranscoder()));
var content = result.ContentAs<string>();