F# Deserialize Object on Bucket.Query<'t>


#1

Hi,

I just started working with Couchbase, using Couchbase SDK 2.0. I can successfully save and query documents, using Bucket.Upsert and Bucket.Query<obj>.

However, I’m completely unable to figure out how to get a strong typed, deserialized POCO from Bucket.Query. So, what I want to do is Bucket.Query<Team>, and get a List<Team> back.

I’ve gone through all the examples, and they either use CouchbaseClient, which I’m unable to locate, or they use Query<dynamic>, which doesn’t work nicely with F#…

Any advice or pointers would be greatly appreciated.

Thanks


#2

@JDuPreez -

Are you referring to a View or N1Ql query? With views there is some additional work that needs to be done because the record returned by the view engine doesn’t match perfectly: it’s encapsulated in a view row. This will be changing soon.

Here is another post discussing: 2.0 DP3 Question on Views / JsonDataMapper.cs Deserialization

LMK if this is what you are encountering.

Thanks,

Jeff


#3

Hi,

Thank you for the help. I managed to solve this:

  1. I had to change my View to:

function (doc) {
emit(doc.id, { “id”: doc.id, “name”: doc.name, “description”: doc.description });
}

  1. Reference FSharp.Dynamic and

open EkonBenefits.FSharp.Dynamic
open Newtonsoft.Json

  1. Then to get strong typed POCOs out:

let rslt = bckt.Query<obj> q

    [ for row in rslt.Rows -> 
        JsonConvert.DeserializeObject<Team> ((row?value).ToString()) ]

#4

Just like to add. It would be great if Couchbase can add a clear example like this somewhere. I found it a bit confusing that the example apps use Query<dynamic> (like the beer brewery) and didn’t really explain how to deserialize a strong typed object.


#5

@JDuPreez

Glad to hear you got this resolved and thanks for the feedback! I will be updating the docs and sample apps in the next couple of weeks and will make the suggestions you mentioned.

-Jeff