0

I m trying to serialize a RealmObject in my uwp project and I would like to know if there is any way to to ignore ObjectSchema and Ream properties when serializing a realmObject ? I tried to create a constuctor to avoid copying these two properties but It doesn't worked for me.

This is my object :

public class Sector : RealmObject, NodeBase
{
    [PrimaryKey]
    public int id { get; set; }
    public string key { get; set; }
    [Ignored]
    public Dictionary<string, string> title { get; set; }
    [JsonIgnore]
    public Dictionary Title { get; set; }
    public int position { get; set; }
    public bool template_is_carousel { get; set; }
    public bool isVisible { get; set; }
    public IList<string> filter { get; }
    [Ignored]
    public DisplayType display_type { get; set; }
    [JsonIgnore]
    public string DisplayType { get; set; }
    public int parent_id { get; set; }
    public string parent_type { get; set; }
    public string image_url { get; set; }
    public string banner_color { get; set; }
    public int template_rows { get; set; } = 3;
    public int template_columns { get; set; } = 2;

    public Sector(Sector sector)
    {
        this.id = sector.id;
        this.key = sector.key;
        this.Title = sector.Title;
        this.title = sector.title;
        this.position = sector.position;
        this.template_is_carousel = sector.template_is_carousel;
        this.isVisible = sector.isVisible;
        this.filter = sector.filter;
        this.display_type = sector.display_type;
        this.DisplayType = sector.DisplayType;
        this.parent_id = sector.parent_id;
        this.parent_type = sector.parent_type;
        this.image_url = sector.image_url;
        this.banner_color = sector.banner_color;
        this.template_rows = sector.template_rows;
        this.template_columns = sector.template_columns;
    }

    public Sector()
    {
    }
}

And this is how I m serializing my object :

private string serializeParameter(object parameter)
    {
         return JsonConvert.SerializeObject(parameter); 
    }

1 Answers1

0

Serialization in Json.NET will serialize base class properties by default. The Sector class inherit from RealmObject class, then when serializing Sector object, all the base properties in the parent class RealmObject will be serialized.

To resolve this, you could try to use [JsonObject(MemberSerialization.OptIn)] attribute, in that case, properties without specify [JsonProperty] will not be serialized. For example, the following code will only serialize parent_type property,

[JsonObject(MemberSerialization.OptIn)]
public class Sector : RealmObject
{
    [JsonProperty]
    public string parent_type { get; set; } 
}

Actually the simply way is to add [JsonIgnore] attribute for the properties in the parent class. But it looks like you are using a third package that you could not change the parent class RealmObject.

More details please reference this thread.

To use which way is based on your requirements, you could also try to serialize it by Json relative APIs in UWP app without using the Json.Net. For example:

 var jsonstring= Stringify();

 public string Stringify()
 { 
     JsonObject jsonObject = new JsonObject();
     jsonObject["parent_type "] = JsonValue.CreateStringValue("test"); 
     return jsonObject.Stringify();
 }
Sunteen Wu
  • 10,309
  • 1
  • 9
  • 20