In a project, you need to get an instance of a certain class from the JSON string using the LiteDB library.
Json:

 { "field1": "string", "field2": 1 } 

Classes are organized as follows:

 class Class1 { public string field1; public int field2; } 

The following method is defined in the successor class LiteDatabase :

 private T Read<T>(Stream inStream) { var ms = new MemoryStream(); inStream.CopyTo(ms); var bytes = ms.ToArray(); ms.Dispose(); var doc = BsonSerializer.Deserialize(bytes); return Mapper.ToObject<T>(doc); } 

The problem is that when running BsonSerializer.Deserialize , an exception is thrown:

 System.IndexOutOfRangeException не обработано пользовательским кодом HResult=-2146233080 Message=Индекс находился вне границ массива. Source=LiteDB StackTrace: в LiteDB.BsonReader.ReadCString(ByteReader reader) в LiteDB.BsonReader.ReadElement(ByteReader reader, String& name) в LiteDB.BsonReader.ReadDocument(ByteReader reader) в LiteDB.BsonSerializer.Deserialize(Byte[] bson) 
  • 2
    Well, you need to look at it in the LiteDB.BsonSerializer documentation, this is a non-standard class. I would venture to suggest that your text is not BSON, but JSON (this is not the same thing). - VladD
  • Check if there is something in the bytes array? Maybe it's empty, and an exception arises because of this - Donil
  • @Donil, the bytes array contains elements. - tryphosa

0