Newtonsoft.Json.JsonSerializationException while attempting to fetch records
I'm working on my second Xamarin / ReactiveUI application that uses Akavache. Everything works perfectly fine in my first app yet my new app is returning a JsonSerializationException while attempting to fetch my object from the data store. I have gone over both of my apps several times and have made sure everything is indeed the same.
I've spent two days trying to figure out what I've done wrong here to no avail... Along the way I ran into open issue #95 where Paul states:
"if you can track down a case where we're failing serialization/deserialization and ending up returning 'null', that'd definitely be a bug"
I don't know whether this is an Akavache bug or my own fault, but I do know that I'm completely out of ideas with this exception.
My Class:
[JsonObject]
public class Equity
{
public string Symbol { get; set; }
public DateTime InsertedOn { get; set; }
public decimal Entry { get; set; }
public decimal Exit { get; set; }
public int Quantity { get; set; }
[JsonIgnore]
public decimal Difference
{
get
{
return (Exit - Entry) * Quantity;
}
}
}
Storage / Fetch Procedure:
try
{
var equities = await BlobCache.LocalMachine.GetObject<ReactiveUI.ReactiveList<Equity>>(Global.AkavacheKeys.equityData);
}
catch (KeyNotFoundException)
{
await BlobCache.LocalMachine.InsertObject<ReactiveList<Equity>>(Global.AkavacheKeys.equityData, equities);
}
catch (Exception x)
{
Debug.WriteLine("{0}", x); // hits
}
Stack Trace:
Exception: Newtonsoft.Json.JsonSerializationException: Unexpected token while deserializing object: PropertyName. Path 'Value[0].Symbol'.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x0013c] in <2781d1b198634655944cdefb18b3309b>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x0007a] in <2781d1b198634655944cdefb18b3309b>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3426/d1ee3ba2/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at System.Reactive.PlatformServices.ExceptionServicesImpl.Rethrow (System.Exception exception) [0x00006] in <50e73325239843abb1c421bf5dddc7a9>:0
at System.Reactive.ExceptionHelpers.ThrowIfNotNull (System.Exception exception) [0x0000d] in <ab76b1b1678341d69f8fc2c1f68d0bf5>:0
at System.Reactive.Subjects.AsyncSubject`1[T].GetResult () [0x00039] in <d0067ed104ac455987b6feb85f80156b>:0
at xxx.DashboardViewModel+<RegisterObservables>c__async2.MoveNext () [0x00091] in xxx/ViewModels/DashboardViewModel.cs:103
I've opened up a bounty on an existing question on stack overflow: http://stackoverflow.com/questions/29768550/newtonsoft-json-jsonserializationexception-when-fetching-an-object-from-akavache
Try to read the raw value by calling await BlobCache.LocalMachine.Get(Global.AkavacheKeys.equityData) or wrap it (like I did) into BitConverter.ToString() and analyze it in another editor of your choice, that can show invisible characters.
Compare the outcome to what you would expect. Maybe there is something you see ...
But I guess the reason for the exception here is different from the one, I had on stackoverflow. If it's the same, it would mean, that your instance of Akavache can't de-serialize the value of the property Symbol without any problems - and that would be strange ... it's just a string ...
@codebeaulieu sorry for the delay on this, but my psychic debugger thinks serializing/deserializng ReactiveUI.ReactiveList<Equity> could be the issue here.
Could you try using a plain List<Equity> here, as I think ReactiveList might be doing some crazy things when it's being deserialized...