step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
671 stars 170 forks source link

OnceSingleAsync<T> never maps the object #234

Closed mastertheef closed 3 years ago

mastertheef commented 4 years ago

OnceSingleAsync method always returns an object with empty fields.

public class Model { public Id { get; set; } public string Name { get; set; } }

will return an object like

{Id: Empty.Guid; Name: ''}

hieucodecraft commented 4 years ago

Did the problem was solved? If not, please show your code when you call the OnceSingleAsync()

claybrooks commented 4 years ago

When I do .OnceSingleAsync<object>, I notice that the object returned is not flat. It's in a format like { "unique_id": {...}}, where {...} is the type I want OnceSingleAsync<T> to deserialize to and "unique_id" is unknown at the time of querying (it's generated when the record was added to the real time database). Since we only care about the data underneath the unique_id, and the unique_id will be different from record to record, there's no way to write a class hierarchy for the JSON deserializer.

The easiest way to overcome this is to apply a [JsonConverter(typeof(ConverterClassName))] to the class you are trying to deserialize to. Then, in the converter, you can handle the nested data properly.

Here's a link to the converter documentation: Custom Converter

Here's a link to a good solution for writing converters: Templated Converter

There may be a way to structure the query better to only return the data underneath the unique_id, but I couldn't find a way through the API.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 3 years ago

Closing the issue due to inactivity. Feel free to re-open