Some properties in my document was read like float instead of integer

Hello,

I migrate my CouchBaseLite from 2.7 to 2.8 et Async Gateway to version 3.0.

my problem is : some properties of a document are interpreted as float instead of integer.
I have lot of types of documents but it’s just with one there is a problem.

when i read data from Couchbase Lite, the dictionary returned by the querybuilder indicates that my property is of type float and not integer. the value is however in integer format.

Below is the code that reads the data in couchBaseLite and turns the document into a .NET class.
The JSON serialization writes the properties in decimal format and the deserialization in my class crashes because I expect integers and not floats

    public IEnumerable<T> Query(IExpression expression)
    {
        List<T> convertedResult = new List<T>();

        using (var query = QueryBuilder.Select(SelectResult.All())
                        .From(DataSource.Database(_data.Db))
                        .Where(Expression.Property("type").EqualTo(Expression.String(_documentType))
                        .And(Expression.Property("isDeleted").IsNot(Expression.Boolean(true)))
                        .And(expression)))
        {
            foreach (var result in query.Execute())
            {
                var dict = result.GetDictionary(_data.Db.Name);
                var data = dict.ToDictionary();
                var json = JsonConvert.SerializeObject(data, _settings);

                var value = JsonConvert.DeserializeObject<T>(json, _settings);
                convertedResult.Add(value));
            }
        }

        return convertedResult;
    }

thanks for you help

@silman, just to clarify, do you mean that, JsonConvert.SerializeObject(data, _settings) writes a value in “data” in float format but it’s supposed to be integer?

yes, Json serialization write the value for example “120.0” instead of “120”.

I don’t understand why queryBuilder’s dictionnary result consider my property “surfaceMaison” like a float and not like a integer

Internally, we do have types to distinguish between integers and floats. Do you know how the document is created? Or how are those fields populated?

I think programmer is the decision maker.
It depends on various aspects to prepopulate or display on system.

in my case, the documents come from a CouchBaseServer 6.6.5 enterprise database, with a 3.0 gateway.
The database was loaded by the XDCR service from a Couchbase Server Community Edition 6.6.0 database.

otherwise the documents are created with the Front Office using the JSON serialization of the .NET class.

public class ReleveEnergieMaison
{
    [MailMerge(MailMergeTypeEnum.Text, "MaisonSurface")]
    public int? SurfaceMaison { get; set; }
    public int? AnneeConstructionMaison { get; set; }
    public DateTimeOffset? DateConstructionMaison { get; set; }

    public decimal? ConsoElecAnnuelle { get; set; }
    public decimal? MontantFacturekWh { get; set; }
 ...
}

    public void Save(T entity)
    {
        if (entity == null)
            throw new ArgumentNullException($"Object to save into DB is null : {typeof(T).Name}");
        entity.DateUpdated = DateTime.UtcNow;
        var documentToSave = ObjectAsMutableDocument(entity);

        // Try to get a mutableDocument with the same ID (to see if exists) :
        var existingDoc = _data.Db.GetDocument(entity.Id.ToString())?.ToMutable();
        if (existingDoc != null)
        {
            ManageProperties(existingDoc, documentToSave);
        }

        _data.Db.Save(documentToSave);
    }


    protected MutableDocument ObjectAsMutableDocument(T entity)
    {
        // Convert our entity into Dictionary<string, dynamic> :
        var json = JsonConvert.SerializeObject(entity, _settings);
        var values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json, _settings);

        // Construct the doc Id :
        dynamic docId = String.Empty;
        values.TryGetValue("id", out docId);
        if (docId == null || docId == String.Empty)
        {
            throw new ArgumentNullException($"Can't create MutableDocument without ID, docId is null");
        }

        return new MutableDocument(docId, values);
    }

In ObjectAsMutableDocument(T entity), there are:

  1. json = JsonConvert.SerializeObject(entity, _settings);
  2. var values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json, _settings);

By "yes, Json serialization write the value for example “120.0” instead of “120”, did you mean “json” in above (1) shows the number 120.0 instead of 120. Have you inspected “entity”, the argument of the function? Are you sure the property in “entity” is of type integer?