jamesmontemagno/monkey-cache

Newtonsoft JSON Serialization error

Daniele-Tentoni opened this issue · 2 comments

Hi all,
I've a problem when trying to retreive some data from barrel. In this code

try 
{
    if(barrel.Exists("profile")) 
{
        var cache = FileSystem.CacheDirectory;
        var data = FileSystem.AppDataDirectory;
        var profile = this.barrel.Get<Profile>("profile");
        return profile ?? new Profile();
    }
} 
catch(Exception e) 
{
    Debug.WriteLine(e.StackTrace);
}

I get the error

Type specified in JSON 'System.Reflection.RuntimeMethodInfo, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' is not compatible with 'System.IntPtr, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. Path 'SaveProfileAction.Method.$type', line 1, position 177.

I've saved a profile before read with

barrel.Add("profile", profile, TimeSpan.FromDays(365));

In my Profile class I've

public class Profile : ObservableObject
{
    [JsonIgnore]
    public Action SaveProfileAction { get; set; }

    private string dream = string.Empty;
    public string Dream
    {
        get => dream;
        set => SetProperty(ref dream, value, onChanged: SaveProfileAction);
    }

    private string name = string.Empty;
    public string Name
    {
        get => name;
        set
        {
            var force = false;
            if (value.Length > 15)
            {
                value = value.Substring(0, 15);
                force = true;
            }

            SetProperty(ref name, value, onChanged: SaveProfileAction);
            if (force)
                OnPropertyChanged();
        }
    }

    private string preferredLocation = string.Empty;
    public string PreferredLocation
    {
        get => preferredLocation;
        set
        {
            var force = false;
            if(value.Length > 30)
            {
                value = value.Substring(0, 30);
                force = true;
            }

            SetProperty(ref preferredLocation, value, onChanged: SaveProfileAction);
            if (force)
                OnPropertyChanged();
        }
    }
}

What I understand from the error is that he try to retreive the SaveActionProfile property in the json string but he can't do that. Any idea?

Sorry for my poor English.

That code looks familiar :)

So I am thinking that somehow SaveProfileAction got saved in your json and that isn't compatible. Try to delete the file first and re-save it.

I've tried to make a "MockStore" to test the behaviour

public class MockBarrel 
{
    readonly IBarrel barrel;
    readonly object locker = new object();
    public MockBarrel()
    {
        Barrel.ApplicationId = AppInfo.PackageName;
        barrel = Barrel.Create(FileSystem.AppDataDirectory);
    }

    public MockItem GetMockItem()
    {
        lock (locker)
        {
            var item = barrel.Get<MockItem>("item");
            return item ?? new MockItem();
        }
    }

    public void SaveMockItem(MockItem item)
    {
        lock (locker)
        {
            barrel.Add("item", item, TimeSpan.FromDays(365));
        }
    }

The model is

public class MockItem
{
    public string Id { get; set; }
    public string Text { get; set; }
}

This this code all work good

var mock = new MockBarrel();
mock.SaveMockItem(new Models.MockItem { Id = "1", Text = "Some text" });
var item = mock.GetMockItem();

Now my question is: can the problem be made by calling the Get from an instance of my service different from the instance that do the Add? In the mock example I make all this step in only one shot.