How to query a view and retrieve raw json

I’m using the .NET SDK 2.1.1 and would like to query a view and simply retrieve the raw json result. Is it possible ?
I’m using IBucket.QueryAsync but IViewResult seems to be limited to returning a collection of rows of T.
Should I switch to the REST Api instead ?

If you ask for a String type, don’t you get the raw JSON ?

No, I get a collection of Couchbase.Views.ViewRow<System.String>

The ViewRow should have a String Value property.

I agree, but I have to reconstruct the Json by hand and there is a penalty with unneeded deserialization.
I ended up using the REST Api.

That seems weird. Maybe @simonbasle and @jmorris can help.

well unless you want to deal with the JSON array of rows (ie the full raw response), there’s no unneeded deserialization that I can think of?
The SDK splits the response into ViewRow<String>, each row contains the raw JSON item emitted by the view plus metada, doesn’t it?
If you want to map the rows data to a POCO, just use QueryAsync<POCO>

Unless I’m doing something wrong, the logs clearly shows deserialization (this is log4net logs):
2015-07-01 13:46:04,389 [119] [DESDEV04] Newtonsoft.JSON INFO Started deserializing Couchbase.Views.ViewResult1[System.String]. Path 'total_rows', line 1, position 14. 2015-07-01 13:46:04,392 [119] [DESDEV04] Newtonsoft.JSON INFO Started deserializing System.Collections.Generic.List1[Couchbase.Views.ViewRow1[System.String]]. Path 'rows', line 1, position 24. 2015-07-01 13:46:04,393 [119] [DESDEV04] Newtonsoft.JSON INFO Started deserializing Couchbase.Views.ViewRow1[System.String]. Path ‘rows[0].id’, line 2, position 7.
2015-07-01 13:46:04,400 [119] [DESDEV04] Newtonsoft.JSON INFO Finished deserializing Couchbase.Views.ViewRow1[System.String]. Path 'rows[0]', line 2, position 87. 2015-07-01 13:46:04,400 [119] [DESDEV04] Newtonsoft.JSON INFO Started deserializing Couchbase.Views.ViewRow1[System.String]. Path ‘rows[1].id’, line 3, position 7.

and so on.

well you are correct, I was unclear: there is some deserialization, but it’s for unwrapping each row in the result list from the envelope of the response.

Each row item is composed of the value emitted by the view, the document’s ID that generated the value and the key emitted by the view.

If the type T is a POCO, the value part would also be unmarshalled into the POCO. Otherwise with String this is still raw JSON.

If you want to do all this processing yourself or somehow deserialize the whole row differently, then you’d have to use the REST api directly I guess… You could always construct the request through the ViewQuery object, get the URL by calling RawUri if you want…

Would you care to explain your use case in a little bit more detail though?

My context is a n tier architecture. I’m trying to have the best performance possible. To achieve this, I would send back raw json through the various layers until I reach the one that would deserialize it because it knows what to do about it. As I said previously I’m now using the REST API and this is working fine.

@omatrot -

If you want to completely avoid deserialization, then you’ll have to go raw REST API (like you did). In doing so, you give up:

  • Automatic retries
  • Load balancing of query requests
  • The QueryRequest API
  • etc…

However, I believe you could create a custom ITypeSerializer which does no serialization (just passes through) and you would get the benefits of using the SDK. Here is an example: http://blog.couchbase.com/2015/june/using-jil-for-custom-json-serialization-in-the-couchbase-.net-sdk

-Jeff

@jmorris

I’ll try this approach and let you know. Automatic retries and Load balancing are indeed very important.