Open MichaelRumpler opened 3 years ago
@MichaelRumpler The BsonValue
constructor should never be used for arrays (and documents). You should use the BsonArray
constructor instead. BsonArray
inherits from BsonValue
, and if you call the BsonValue
constructor directly, you'll end up with an instance that is not a BsonArray
but which has Type == BsonType.Array
. Try using something like this:
var bsonArray = new BsonArray(new BsonValue[] { "a" });
I ended up using BsonMapper.Global.Serialize
. This seems to work too so far.
Apparently I did something wrong, but it still worked in v4 and breaks in v5. I'll leave this issue open. If you decide that it is ok to throw in v5, you should at least throw a better exception. The NRE tells you nothing.
@MichaelRumpler This worked in LiteDB v4 because the BsonArray
class didn't exist yet, so everything was a BsonValue
.
Calling BsonMapper.Global.Serialize(...)
is probably the best solution right now, as it should work for every class in every situation.
I'll leave this issue open for now, while I think of a way to solve or mitigate this issue.
Version 5.0.10
Describe the bug I want to write a
string[]
asBsonValue
. The code which worked with LiteDB 4.1.4 throws aNullReferenceException
in 5.0.10.Code to Reproduce
Expected behavior The
string[]
would be written to the database.Stacktrace
Additional context In
BsonValue.GetBytesCount
this.Type
isBsonType.Array
butthis.AsArray
returnsnull
.Maybe I'm doing this wrong. If you tell me how I should convert arrays to
BsonValue
s in a different way, I'd also be fine. Especially if I could write/read different kinds of arrays. Currently I'm limited to string arrays.