Json Format question

Good afternoon. I am new to couchbase and I am writing a couple of small test applications – 1) Xamarin app & 2) ASP.NET app – test the syncing features.

When I save a document in the Xamarin app it saves the data at the “root” level (see below).

{

"description": “Description”,

"doctype": “task”,

"taskid": “1”,

"title": “Task 1”,

"_rev": “5-c6328f34ada7b5401a6eef382a4385b7f6843f04”,

"_id": "-vC6DlmhGE0G41qCABaKrMg"

}


However, when I save the same document in the ASP.NET app the data is stored inside a “Content” attribute. See below:

{

"cas": 0,

"content": {

"description": “Description”,

"docid": null,

"doctype": “task”,

"taskid": “2”,

"title": "Task 2"

},

"expiry": 0,

"id": null,

"token": null,

"_rev": “1-892a7902f3f7195ad695b83647100c0f”,

"_id": "2"

}


I also tried doing a N1Q1 insert but it gave the exact same results.

Would have to write 2 different sets of code in each application to do any operations on these different documents but containing the same data it seems like.

I tried to do as much reading and testing as I could before posting but I just could not find any readily available answers. I know I am missing something and I would really appreciate any help.

Thank you,

Brian

Brian,

Would you mind posting a couple snippets of code where you are saving/retrieving the document? Also, would you mind sharing what versions your are using (of Couchbase Server, Couchbase Lite, Sync Gateway, and the .NET SDK)? I’ll tag @borrrden as well, he might have some additional questions or information.

Xamarin code:

using (var mutableDoc = new MutableDocument())
{
mutableDoc.SetString(“taskid”, txtTaskId.Text)
.SetString(“title”, txtTitle.Text)
.SetString(“description”, txtDescription.Text)
.SetString(“doctype”, “task”);

            database.Save(mutableDoc);  
        }

ASP.NET code:

Task nTask = new Task();
nTask.taskid = txtTaskId.Text;
nTask.title = txtTitle.Text;
nTask.description = txtDescription.Text;
nTask.doctype = “task”;

        var bucket = cluster.OpenBucket("TestSync");
        Couchbase.Document<dynamic> doc = new Couchbase.Document<dynamic>();
        doc.Content = nTask;

        bucket.Upsert(nTask.taskid, doc);

Xamarin results:

{
“description”: “Description 22”,
“doctype”: “task”,
“taskid”: “2”,
“title”: “Task 22”,
“_rev”: “2-2b7ad977e9253b7bd084bbec786c9f0d”,
“_id”: “-MZb9V50cBkWXcTunPM-DJg”
}


ASP.NET results:

{
“cas”: 0,
“content”: {
“description”: “Description 88”,
“docid”: null,
“doctype”: “task”,
“taskid”: “88”,
“title”: “Task 88”
},
“expiry”: 0,
“id”: null,
“token”: null,
“_rev”: “1-f9a1cb8793722d5fa1cc6156e629bec9”,
“_id”: “88”
}

Thank you very much for your help!

@brianjolive -

The reason you are seeing the “content” field in the ASP.NET code is because you are using an overload for POCO based persistance and passing the Document reference as the body, so the SDK will serialize the Document object as if its a POCO. Instead, either pass the nTask reference directly as the body (do not create the Document reference) or modify your code to use the correct overload which takes a Document:

    var bucket = cluster.OpenBucket("TestSync");
    Couchbase.Document<dynamic> doc = new Couchbase.Document<dynamic>();
    doc.Content = nTask;
    doc.Id = nTask.taskid

    bucket.Upsert(nTask);

Hopefully that helps,

-Jeff

Thank you! Works like a champ!

1 Like