Couchbase Lite Mapping Tool problems with complex objects

I just tried using the mapping tool (1.0.7) on github because I have some slightly complex objects to store in couchbase lite (2.6).

The first issue I’ve noticed is that properties using [JsonIgnore] appear to still be serialized. Am I missing something?

Also, it appears that I cannot use this on an object with a nested object. For example…

Public Class Contract
{

public List Products { get; set; }

}

Public Abstract Class Product
{
}

Public Class Roofing : Product
{
}

It says that Roofing is not a valid type when calling .ToMutableDocument on an instance of Contract. Will this tool work on objects like this or should I be doing something else. I’d greatly appreciate and ideas.

Thanks!!

1 Like

Hey @Salvatore_Librizzi!

I have reproduced the issue you’ve reported with properties that use the JsonIgnore attribute still getting saved into the Couchbase Lite database, and have created a new bug for it here. Thank you for that catch! I am hoping to have a fix out very soon.

Regarding your second issue, I have not been able to reproduce what you are encountering. I did, however, need to make some assumptions based on the code sample you provided:

public class Contract
{
    public List<Roofing> Products { get; set; }
}

public abstract class Product
{
    public int ProductId { get; set; }
}

public class Roofing : Product
{
    public string Description { get; set; }

    [JsonIgnore]
    public string DoNotUse { get; set; }
}

And then I created a new contract like this…

string contractId = Guid.NewGuid().ToString();

var products = new List<Roofing>();

for (int i = 0; i < 10; i++)
{
    var product = new Roofing
    {
        ProductId = i,
        Description = $"This is product #{i}",
        DoNotUse = $"Product {i} property that shouldn't show up!"
    };

    products.Add(product);
}

var contract = new Contract
{
    Products = products
};

var document = contract.ToMutableDocument($"contract::{contractId}");

Database.Save(document);

Also, I have attached a sample app, MappingTestApp.zip (3.4 MB), that you can use to test the code above. It uses Couchbase.Lite.Mapping version 1.0.8. If you get a second please take a look as see how it compares to what you are trying to do.

Thanks so much for the feedback on the package!

Hey Robert! Thanks so much for chiming in as I know you’re probably very busy.

Slight correction to the example you used:

public class Contract
{
     public List<Product> Products {get; set; }
}

It’s not a list of type Roofing. It’s of the abstract type Product which can contain Roofing, Siding, Flashing, etc. The problem occurs when doing contract.ToMutableDocument it chokes on an says that type Product cannot be created (obviously because it’s abstract). It loses whether or not its of type Roofing.

I know that when just using newtonsoft you have to specify TypeNameHandling = TypeNameHandling.All in the JsonSerializerSettings to ensure that the type (Roofing in this case) is preserved for deserialization.

Btw, I couldn’t get the demo running as it had an error in MainActivity.cs:
Error CS0234 The type or namespace name ‘Essentials’ does not exist in the namespace ‘Xamarin’

Thanks so much for your help. I greatly appreciate it :slight_smile:

1 Like

Hey @Salvatore_Librizzi!

Ah, yes, I’m understanding your use case much better now. It looks as though you’ve uncovered another bug. I have created a new bug here, and, as you mentioned, the fix will likely involve making changes to the deserialization workflow to accommodate.

Thanks for the detailed information! I am shooting to have the next release (v1.1) published within a few days.