How to insert raw json in Couchbase with the .NET client?

I’m working on a REST API for CRUD operations on Couchbase.

The Idea is to send directly to the backend the RAW Json coming from the request body.

There should be no serialization at all done by the Couchbase client. You can actually get a document back from the store in a simple string (no deserialization).

As far as I can tell, this is not supported yet.

Am I wrong ?

I am doing something similar and I think what you are missing is the document “container” around the content.

IBucket bucket = ClusterHelper.GetBucket(bucketName);
Document doc = new Document { Id = docId, Content = content };
bucket.Upsert(docId, doc);

The Document object gives Couchbase someplace to hang metadata (like ID). Inside that your raw text will be encoded for string safety (" becomes " for example), which is undone upon retrieval. There is still probably a serialization/deserialization step going on in the SDK since Document is generic though. I think they’d need some sort of StringDocument or ByteArrayDocument that skips serialization and takes the value as is to make this optimal.

I think I may have found a solution.

With the latest SDK (2.1) I’ve written a custom converter.
On serialization (POST/PUT), it simply call writer.WriteRaw(rawContent) to solve my “no serialization” need.
On deserialization (GET), I work at the string level, calling bucket.GetDocumentAsync(id) and it seems to work, I’ve checked the string lengths and they match.

Bu I agree with you, raw operations would be a plus.

@omatrot -

Glad to hear you have taken advantage of one of the “new” extension points (ITypeTranscoder) we’ve added to the client!

If your JSON is a string, you can just make the type parameter a string the DefaultTranscoder will skip serialization/deserialization and just insert the string (of course converting it to bytes for IO) the Couchbase server will treat the string as JSON. This is assumes the string is a valid JSON string, if not the server will store it as binary content.

You can also store the raw bytes if you make the type parameter a byte array and the default transcoder will bypass serialization/deserialization, however the server will treat the value as binary content.

-Jeff

@jmorris I’ll need to double check because when using the overloads that takes an IDocument , it seems to me that the content, even if it is a simple string, was going through serialization. Is it by any chance visible in the logs ?

@omatrot -

The DefaultTranscoder will use UTF8 encoding; the ITypeSerializer will not be used.

-Jeff

@jmorris Just double checked, it works. No serialization.