How to tell DocumentDB SDK to use camelCase during linq query?
Asked Answered
B

4

20

Considering the document { "userName": "user1" } stored in the User collection, and the following User class:

public class User
{
        public string Id { get; set; }
        public string UserName { get; set; }
}

With the following JSON.net settings:

JsonConvert.DefaultSettings = () =>
{
    return new JsonSerializerSettings
    {
       ContractResolver = new CamelCasePropertyNamesContractResolver(),
    };
};

When I query with Linq as such:

var t = _client.CreateDocumentQuery<User>(_collection.SelfLink)
            .Where(u => u.UserName == "user1").AsDocumentQuery().ExecuteNextAsync();

t.Wait();

var users = t.Result;
var user = users.FirstOrDefault();

user is null. Changing the Document to have a pascal casing or the POCO to use a camel casing solves the issue. Of course I do not want any of those as I want my JSON objects and C# objects to be "standarized".

How can I tell the DocumentDB SDK to map my object's property names using camel casing, similar as JSON.net?

Bonesetter answered 27/5, 2016 at 18:2 Comment(1)
Does this answer solve your issue? It's not exactly a duplicate question, but I believe the published answer covers what you're looking for.Chinaman
L
28

The DocumentDB LINQ provider does not pick up the JsonConvert.DefaultSettings. In general you can use the DefaultSettings to control camelCase, but for those properties you wish to use in a LINQ Where clause must have the name explicitly set using JsonProperty attribute on your DTO.

public class User
{
    public string Id { get; set; }

    [JsonProperty("userName")]
    public string UserName { get; set; }
}

Although a bit tedious and a good source for bugs, it seems to be your only option for now.

Limon answered 27/5, 2016 at 18:39 Comment(2)
thanks for the answer! this was killing my queries too. For now i'm switching away from LINQ expressions to SQL.Mandle
A GitHub issue for this: github.com/Azure/azure-documentdb-dotnet/issues/317Menzies
V
5

In a similar case with Cosmos DB, I was able to set all properties to Camel case for my objects at the class declaration level, as in:

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class User
{
    public string Id { get; set; }

    public string UserName { get; set; }
}

This is how you tell NewtonSoft.Json to use Camel case for serializing.

Venial answered 12/2, 2020 at 16:33 Comment(2)
This does not work for the CosmosDB DocumentClient. Only controlling the casing per property with JsonProperty("lowerCaseName").Distraught
I have tried this solution as well and it doesn't fix the linq queries.Cinderella
D
5

In newer SDK's you can control the linq serialization in the following way:

container.GetItemLinqQueryable<T>(
    linqSerializerOptions: new CosmosLinqSerializerOptions
    {
        PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
    });

Where container is a Microsoft.Azure.Cosmos.Container.

Dobbins answered 21/2, 2023 at 15:4 Comment(0)
N
0

According to CosmosDB SDK sources if you inherit from CosmosLinqSerializer you can implement SerializeMemberName https://github.com/Azure/azure-cosmos-dotnet-v3/blob/b9b35bb92d5b0c075259a4d78287bff0f66c9861/Microsoft.Azure.Cosmos.Samples/Usage/SystemTextJson/CosmosSystemTextJsonSerializer.cs#L55

And for System.Text.Json you can copy the code above as is. Then you just set PropertyNamingPolicy in your JsonSerializerOptions passed to your Cosmos serializer.

Negris answered 31/5 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.