Storing json docs properties with PascalCase

Hey,

I am using the Couchbase .Net sdk StoreJson method this way:

 _client.ExecuteStoreJson(StoreMode.Set, itemKey, value);

The result document`s properties in couchbase are camelCase and i want them to be PascalCase.

What is the best way of doing it?

Chenos -

The CouchbaseClientExtensions class has a public static JsonSerializerSettings instance: https://github.com/couchbase/couchbase-net-client/blob/release13/src/Couchbase/Extensions/CouchbaseClientExtensions.cs#L19

You can set the JsonSerializerSettings.ContractResolver to a custom Pascal cased converter. Alternatively, you could annotate your POCO’s (if your using them) with [JsonProperty(“YourPascalName”)] to force Pascal casing.

-Jeff

Hey Jeff,

I used :

CouchbaseClientExtensions.JsonSerializerSettings.ContractResolver = new PascalCasePropertyNamesContractResolver();

It serialized the json properties with pascalcase and stored them as i wanted in couchbase.
New thing that happens now is that i get “Error converting value to type ‘System.Int64’. Path ‘id’…”

My Id is a long type fields that its name is also pascalcase as the other properties.
Can you explain this error?

By the way, when i use [JsonProperty(“id”)] attribute above the “Id” property, it works, but i want to understand why it happens and if there is a good way i can solve it without using JsonProperty attribute.

Thanks,
Chen.

I replied above and waiting to hear if you have an answer to my issue.

Tnx,
Chen

Chenos -

There is some magic going on with respect to the Id:

That is because it becomes the primary key for the document and becomes part of the metadata instead. Since it’s case sensitive, using [JsonProperty(“id”)] skips this and ensures that it’s stored with document. It’s re-added here: https://github.com/couchbase/couchbase-net-client/blob/release13/src/Couchbase/Helpers/DocHelper.cs#L10

I am pretty sure this was done to accommodate changes on the server where the key and other info were moved to a separate metadata document.

-Jeff

1 Like

My recollection is that this was added because the ASP.NET 1.x client depended on id being in the document for some of it’s auto-mapping. When this changed in 2.0, the quick fix was to make the automatic object handling in .NET inject the ID.

@jmorris this is really your area, but it would be ideal from my perspective if we can find a way to not modify the doc from what we’ve gotten from the app. You’d know enough about how to do this correctly.

@Chenos @ingenthr I believe an easy work-around to this would be to the handle the JSON serialization on the application side and then use one of the non-JSON overloads: ExecuteStore(key, value) or ExecuteGet(key) on the client for storing a retrieving the document. This could be encapsulated into separate methods on the CouchbaseClientExtensions class, for example: GetJsonWithoutEmbeddedId(key). Not pretty, but would work.

Thanks for the answers.

After what you wrote here, i think using JsonProperty[“id”] combined with the PascalCaseContractResolver is cleaner than the other options you suggested.

Hope that in 2.x .net client version i will have even more cleaner solution for this issue.