Monday, November 19, 2018

Functions binding Cosmos DB, C# -- 2

Sample Code
4. Extract specific json properties
5. Read multiple documents


4. Extract specific json properties
I was wondering how serialization works. From 4-1 to 4-6 below, all the samples use the document, code, and request query.

Target document in Cosmos DB
GetDocument.cs

Request query

4-1. Basic class
This is not involved in serialization. The point is that the class has only values "id", "maker", "name", while the Cosmos DB document has "id", "maker", "name", "class", "type", "engine", "transMission", "horsePower", "price". The return only includes "id", "maker", "name".

VehicleList.cs
Return

4-2. Basic class with Capital value name

The class has values with capital letter, "Maker", "Name", but the Cosmos DB document includes values with small letter, "maker", "name", and the return includes values with small letter, which is the original Cosmos DB document.

VehicleList.cs
Return

4-3. Basic class with different value name
Next one is very interesting. The class includes "id", "Maker", "Name2", the Cosmos DB document includes "id", "maker", "name", and the return includes "id", "maker", "name2":null. The Functions and class does not care upper/lower case. It always return the class values with lowercase.

VehicleList.cs
Return

4-4. Serialization

Serialization is used here. In this way, the return is the same as 4-1

VehicleList.cs
Return
4-5. Serialization with Capital value name
Next one is also interesting. The class includes "id", "maker", "name", Jason properties include "id", "Maker", "name", the Cosmos DB document includes "id", "maker", "name", and the return includes "id", "Maker", "Name". Whether the return includes upper/lower case depends on Jason properties, even though the Cosmos DB document includes lower case.

VehicleList.cs
Return

4-6. Serialization with different value name
When you set the different class value name from the Cosmos DB document name, you can get correct ones with correct Jason properties.

VehicleList.cs
Return

5. Read multiple documents
If you use DocumentClient, you can extract multiple documents. Contains cannot handle GUID, so this time id type is string. In this example, search query is defined as searchterm, searchterm2, searchterm3, but you can create complex query by utilizing this.

VehicleList.cs
GetDocumentClient.cs

Request query
Return