C#C
C#8mo ago
eduardoA

✅ Unsafe code

Can somebody explain why this needs UNSAFE

I am getting a json back from firebase firestore

   public async Task<List<T>> ReadAllAsync<T>(string collectionName) {
       var token = await firebaseAuth.User!.GetIdTokenAsync(forceRefresh: true);
       _httpClient!.DefaultRequestHeaders.Authorization =
           new AuthenticationHeaderValue("Bearer", token);

       var requestUrl = $"{_firebaseBaseUrl}/{collectionName}";
       var response = await _httpClient.GetAsync(requestUrl);

       var usersList = new List<T>();

       if(response.IsSuccessStatusCode) {
           var jsonResponse = await response.Content.ReadAsStringAsync();
           var jsonDoc = JsonSerializer.Deserialize<JsonElement>(jsonResponse);

           if(jsonDoc.TryGetProperty("documents", out var documents)) {
               foreach(var document in documents.EnumerateArray()) {
                   var jsonString = document.GetRawText(); // Get the raw JSON string of the document
                   var mappedObject = JsonSerializer.Deserialize<T>(jsonString); // Deserialize directly to T

                   if(mappedObject != null) {
                       usersList.Add(mappedObject);
                   }
               }
           }
       }
       return usersList;
   }
image.png
Was this page helpful?