0

I have a custom class to be cached using Akavache. This class contains one non-serializable property (it's a ParseFile object). When I try to cache/get a list of this custom class using GetAndFetchLatest, it doesn't work. I figure it may be caused by one property that is non-serializable. Tried JsonIgnore attribute, but didn't help.

Question is, how can I tell Akavache to cache everything in the list of custom class, except the non-serializable property? Sample of the custom class is here, I want it to ignore the Photo property:

[ParseClassName("User")]
public class User : ParseObject
{
    [ParseFieldName("name")]
    public string Name
    { 
        get { return GetProperty<string>(); }
        set { SetProperty<string>(value); }
    }

    [ParseFieldName("photo")]
    public ParseFile Photo
    { 
        get { return GetProperty<ParseFile>(); }
        set { SetProperty<ParseFile>(value); }
    }
}
Nik A.
  • 454
  • 6
  • 15

2 Answers2

2

Use [DataContract] and [IgnoreDataMember] to prevent fields to be serialized

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • It still doesn't work :( Probably because Akavache isn't using System.Runtime.Serialization to serialize it. – Nik A. Jan 06 '15 at 16:13
  • JSON.NET respects System.Runtime.Serialization attributes, are you sure you marked your class with `DataContract` *and* all the members? – Ana Betts Jan 07 '15 at 00:48
  • I did, this below code is what I did. But it doesn't look like the User class is cached even after this modification. `[DataContract] [ParseClassName("User")] public class User { [DataMember] [ParseFieldName("name")] public string Name { get { return GetProperty(); } set { SetProperty(value); } } [IgnoreDataMember] [ParseFieldName("photo")] public ParseFile Photo { get { return GetProperty(); } set { SetProperty(value); } } }` – Nik A. Jan 07 '15 at 15:30
  • By the way, Akavache uses Sqlite3 as its backend. – Nik A. Jan 07 '15 at 18:33
  • What is this "GetProperty" and "SetProperty" business? – Ana Betts Jan 07 '15 at 20:07
  • It's the format used to obtain data from Parse database. See https://parse.com/docs/dotnet_guide#subclasses-properties. Maybe it has something to do with the serialization too? I don't know, but all other Parse subclasses that does not have ParseFile property is able to be cached properly. – Nik A. Jan 08 '15 at 05:39
  • Your answer is probably correct. The reason why it didn't work in my case is because ParseObject itself is non-serializable (I just found out!), meaning I can't serialize the whole thing, not only the 'Photo' property. Thanks for your time :) – Nik A. Jan 10 '15 at 17:35
  • It should be simply [Ignore] , have you tried it? – Emil Jul 04 '17 at 01:39
1

The default Json.Net behavior is to serialize all public fields and properties except those marked with the [JsonIgnoreAttribute] attribute. ref: http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size

Mathieu DSTP
  • 117
  • 3
  • 4