Get JSON string from Akavache

219 views Asked by At

My Xamarin app has some cached objects with Akavache. For testing purposes, I'm trying to get the cached data in raw JSON string format. I've tried the following, but I'm getting a weird string:

protected readonly IBlobCache cache;
private void ViewCachedData(string key) => this.cache.Get(key).Subscribe(OnDataLoaded);
private void OnDataLoaded(byte[] data) => Debug.WriteLine(Encoding.ASCII.GetString(data));

The Debug.WriteLine will only print a question mark, but checking the value I can see the JSON partially:

"?\0\0Value\0?\0\0EmployeeNumber\0\b\0\0\0848\0FirstName\0\0\0\0aaron\0LastName\0\0\0\0jones\0\nPreferredFirstName\0CellPhoneNumber\0\v\0\0\05555555555\0\n\0\0\0"
2

There are 2 answers

0
Ana Betts On

Akavache doesn't store things in JSON, it stores them in BSON

0
Elton Santana On

I was able to solve that by using the following code:

private void OnDataLoaded(byte[] data)
{
    using (var reader = new BsonReader(new MemoryStream(data)))
    {
        var serializer = JsonSerializer.Create(new JsonSerializerSettings());
        var json = serializer.Deserialize<JObjectWrapper>(reader).Value;
        Debug.WriteLine(json);
    }
}

private class JObjectWrapper
{
    public JObject Value { get; set; }
}