Home > Back-end >  Azure Blob list keys JsonSerializer.Deserialize returning null C#
Azure Blob list keys JsonSerializer.Deserialize returning null C#

Time:12-20

I have a very simple deserialization to be made. Basically all I do is to retrieve the following json and deserialize as StorageAccountListKeysResult of Microsoft.Azure.Management.Storage.Models:

{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key1\",\"value\":\"qzyyzfNe1bkhBeOVYNXiQ6BjVlDnLsnDWhVqhTrD6whPE78kf/1A/jNeQsUjMvQVnl3dtdKxZmsuMjlx3ru7Q==\",\"permissions\":\"FULL\"},{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key2\",\"value\":\"t 4ao7yMyWvqoiSjrNCH5gPtDC7 Qnow2SdPoFXzOXkPyBrOex/BVUPXtGFW78XAXrQ7ITDZnC8RIyXe5jVaQ==\",\"permissions\":\"FULL\"}

I doubled checked the json result several times and compared with the model and it seems all fine to me but I get null as result from deserialization.

Here is the code:

var serializerOptions = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

var json = "{\"Keys\":[{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key1\",\"value\":\"qzyyzfNe1bkhBeOVYNXiQ6BjVlDnLsnDWhVqhTrD6whPE78kf/1A/jNeQsUjMvQVnl3dtdKxZmsuMjlx3ru7Q==\",\"permissions\":\"FULL\"},{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key2\",\"value\":\"t 4a7oyMyWvqoiSjrNCH5gPtDC7 Qnow2SdPoFXzOXkPyBrOex/BVUPXtGFW78XAXrQ7ITDZnC8RIyXe5jVaQ==\",\"permissions\":\"FULL\"}]}";
var listKeys = JsonSerializer.Deserialize<StorageAccountListKeysResult>(json, serializerOptions); // returns null

What am I missing?

EDIT:

I just realized that the properties of StorageAccountListKeysResult have no setters.

CodePudding user response:

Please try by changing your Keys attribute in the JSON to keys. Your json should look like:

var json = "{\"keys\":[{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key1\",\"value\":\"qzyyzfNe1bkhBeOVYNXiQ6BjVlDnLsnDWhVqhTrD6whPE78kf/1A/jNeQsUjMvQVnl3dtdKxZmsuMjlx3ru7Q==\",\"permissions\":\"FULL\"},{\"creationTime\":\"2021-12-17T12:26:19.5708372Z\",\"keyName\":\"key2\",\"value\":\"t 4a7oyMyWvqoiSjrNCH5gPtDC7 Qnow2SdPoFXzOXkPyBrOex/BVUPXtGFW78XAXrQ7ITDZnC8RIyXe5jVaQ==\",\"permissions\":\"FULL\"}]}";

Reference: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.storage.models.storageaccountlistkeysresult.keys?view=azure-dotnet#Microsoft_Azure_Management_Storage_Models_StorageAccountListKeysResult_Keys

[Newtonsoft.Json.JsonProperty(PropertyName="keys")]
public System.Collections.Generic.IList<Microsoft.Azure.Management.Storage.Models.StorageAccountKey> Keys { get; }

CodePudding user response:

The problem was solved by using Newtonsoft. Microsoft uses Newtonsoft for StorageAccountListKeysResult, so I guess this was the issue.

  • Related