mycouch icon indicating copy to clipboard operation
mycouch copied to clipboard

Query with string as Value

Open jlami opened this issue 5 years ago • 1 comments

Hi,

Thanks for this library, it is a live safer. But I stumbled onto this bug:

I have the following pseudo view:

function (doc) {
  emit(doc._id, "test");
}

And query it like this:

var query = new QueryViewRequest("name", "view");
var result = await couchdb.Views.QueryAsync<string>(query);

result.rows[0].Value is now a JSON string with extra " before and after, where I would like a plain c# string.

I saw that this is because you treat string as special here: https://github.com/danielwertheim/mycouch/blob/714ff85d6149b3e2127f01b9325be424843853c2/source/projects/MyCouch/Serialization/Converters/MultiTypeDeserializationJsonConverter.cs#L20-L26

Maybe you could designate another type as the trigger for this JSON result. Maybe object? Because now this is quite ambiguous. I understand that this is a breaking change, but I don't really see another solution.

Thanks.

jlami avatar Jul 09 '19 14:07 jlami

I'm seeing the same result (using the latest version 7.6.0) when receiving a string array via:

var result = await couchdb.Views.QueryAsync<string[]>(query);

Each string in the array contains escaped double quotes, e.g. "\"foo\"".

The problem appears to be the two 'special case' functions - ReadJsonAsString(JsonReader) and ReadJsonAsStringArray(JsonReader). Both functions are reading the value(s) but then outputting them as JSON tokens, rather than string values, hence the strings have extra double quotes.

For now, the simplest workaround seems to be to declare the types as object and then convert the values back to strings:

var result = await couchdb.Views.QueryAsync<object[]>(query);
...
string value = row.Value[0].ToString();

davidgeary avatar May 19 '22 12:05 davidgeary