Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newtonsoft JSON Serialization error #86

Open
Daniele-Tentoni opened this issue Jun 1, 2020 · 2 comments
Open

Newtonsoft JSON Serialization error #86

Daniele-Tentoni opened this issue Jun 1, 2020 · 2 comments

Comments

@Daniele-Tentoni
Copy link

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.

@jamesmontemagno
Copy link
Owner

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.

@Daniele-Tentoni
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants