step-up-labs / firebase-database-dotnet

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

Save and Restore Lists not working! #224

Closed c-q-f closed 3 years ago

c-q-f commented 4 years ago

Hi there!

// I can save a list do database: async static void Save() { var myColl = new List(); myColl.Add(new Person { ID = 1, Name = "Peter" }); myColl.Add(new Person { ID = 2, Name = "Paul" }); myColl.Add(new Person { ID = 3, Name = "Mary" });

        FirebaseClient firebase = new FirebaseClient("https://xxxxxxxxxxxx.firebaseio.com/");
        await firebase.Child("MyApp").Child("Clients").PutAsync<List<Person>>(myColl);
    }

// but I can't restore it (an exception rises): async static void Restore() { FirebaseClient firebase = new FirebaseClient("https://xxxxxxxxxxxx.firebaseio.com/"); var myColl = await firebase.Child("MyApp").Child("Clients").OnceAsync<List>(); }

EXCEPTION: Firebase.Database.FirebaseException HResult=0x80131500 Message=Exception occured while processing the request. Url: https://xxxxxxxxxxxxxxxx.firebaseio.com/MyApp/Clients/.json Request Data: Response: [{"ID":1,"Name":"Peter"},{"ID":2,"Name":"Paul"},{"ID":3,"Name":"Mary"}] Source=Firebase StackTrace: at Firebase.Database.Http.HttpClientExtensions.d01.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Firebase.Database.Query.FirebaseQuery.d71.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ConsoleApp1.Program.d2.MoveNext() in D:\XM\Samples\Firebase\ConsoleApp1\ConsoleApp1\Program.cs:line 37 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.Tasks.Task.<>c.b139_1(Object state) at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

This exception was originally thrown at this call stack: Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(Newtonsoft.Json.JsonReader, System.Type, Newtonsoft.Json.Serialization.JsonContract) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(Newtonsoft.Json.JsonReader, System.Type, Newtonsoft.Json.Serialization.JsonContract, Newtonsoft.Json.Serialization.JsonProperty, object, string) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Newtonsoft.Json.JsonReader, System.Type, bool) Newtonsoft.Json.JsonSerializer.DeserializeInternal(Newtonsoft.Json.JsonReader, System.Type) Newtonsoft.Json.JsonSerializer.Deserialize(Newtonsoft.Json.JsonReader, System.Type) Newtonsoft.Json.JsonConvert.DeserializeObject(string, System.Type, Newtonsoft.Json.JsonSerializerSettings) Newtonsoft.Json.JsonConvert.DeserializeObject(string, Newtonsoft.Json.JsonSerializerSettings) Firebase.Database.Http.HttpClientExtensions.GetObjectCollectionAsync(System.Net.Http.HttpClient, string, Newtonsoft.Json.JsonSerializerSettings)

Inner Exception 1: JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List1[ConsoleApp1.Person]]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

bezysoftware commented 4 years ago

If you want to be using arrays instead of standard dictionaries, try OnceSingleAsync

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