mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.52k stars 1.24k forks source link

How to store and read System.Numerics.BigInteger ? #2209

Closed pedefe closed 2 years ago

pedefe commented 2 years ago

Hello,

I manage class fields based on System.Numerics.BigInteger values (mainly for year...). With previous version, it was working. Now (release 5.0.12), BigInteger are no more understand and all values are 0. How to allow the LiteDb to work correctly with such a type ? Best regards.

pedefe commented 2 years ago

Solution seems to be with Mapper:

/*
 *  */
public Func<BigInteger, BsonValue> serialize_BigInteger = (bi) => {
    BsonValue bv;
    String s = bi.ToString();
    bv = new BsonValue(bi.ToString());
    return bv;
    };

/*
 *  */
public Func<BsonValue, BigInteger> deserialize_BigInteger = (bv) => {
    BigInteger bi;
    String s = bv.AsString;
    bi = BigInteger.Parse( s);
    return bi;
    };

ldb = new LiteDatabase(...);
ldb.Mapper.RegisterType<BigInteger>(
    serialize: (bi) => serialize_BigInteger( bi),
    deserialize: (bsonv) => deserialize_BigInteger(bsonv)
    );