title: MongoDB BSON (C# Driver), how do I deserialize BSON object serialized with BsonDocument.ToJson()?
body: <p>I'm having a hard time trying to deserialize JSON created with BsonDocument.ToJson() back to BsonDocument. 
  I have an ASP.NET Core API server and a Unity C# frontend. I'm using the same version of BSON in both projects. The serverside retrieves an object from a MongoDB, serializes it to JSON by simply calling ToJson() method on the retrieved document, and sends it to the client. 
  Here is the exact output text of this action (it's a test project, so I'm not afraid to reveal anything of this)</p>
  
  <blockquote>
    <p>"{ \"_id\" : ObjectId(\"5bb35fa04ba8ab26287b52ec\"), \"email\" : \"some@yandex.ru\", \"password\" : \"6fa2da5e421daa8cbba5fdb76c88b30b\", \"regDate\" : ISODate(\"2018-10-02T12:08:00.523Z\"), \"invitations\" : [], \"certs\" : [], \"isAdvertizer\" : false, \"name\" : \"\", \"usedEmail\" : \"some@yandex.ru\", \"lastname\" : \"\", \"usedEmails\" : [\"some@yandex.ru\"], \"inviter\" : ObjectId(\"000000000000000000000000\"), \"accountActivationKey\" : \"65D86D12\", \"subscription\" : { \"_id\" : ObjectId(\"5bb35fa04ba8ab26287b52ed\"), \"uniqueCode\" : \"9d19297c8\", \"userId\" : ObjectId(\"5bb35fa04ba8ab26287b52ec\"), \"isDemo\" : true, \"expirationDate\" : \"07.10.2018 19:08\", \"expirationUt\" : NumberLong(1538939280), \"startDate\" : \"02.10.2018 19:08\", \"startUt\" : NumberLong(1538482080) }, \"logs\" : \"02.10.2018 19:08: Получение пробной подписки: 9d19297c8. Подписка истекает: 07.10.2018 19:08\n\", \"sessionKey\" : \"e8c0c26301bdabbcff79e1407e333d16\" }"</p>
  </blockquote>
  
  <p>I'm totally sure it comes to the client side as is, nothing is lost of broken. Then I try to deserialize it like this:</p>
  
  <pre><code>var bson = BsonDocument.Parse(jsonString);
  </code></pre>
  
  <p>And get this Exception all the time</p>
  
  <blockquote>
    <p>System.FormatException: Cannot deserialize a 'BsonDocument' from BsonType 'String'.
      at MongoDB.Bson.Serialization.Serializers.SerializerBase<code>1[TValue].EnsureBsonTypeEquals (MongoDB.Bson.IO.IBsonReader reader, MongoDB.Bson.BsonType bsonType) [0x00015] in &lt;ca055f3819fb41d78b1423b49b25b04b&gt;:0 
      at MongoDB.Bson.Serialization.Serializers.BsonValueSerializerBase</code>1[TBsonValue].Deserialize (MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) [0x00025] in :0 
      at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue] (MongoDB.Bson.Serialization.IBsonSerializer`1[TValue] serializer, MongoDB.Bson.Serialization.BsonDeserializationContext context) [0x00017] in :0 
      at MongoDB.Bson.BsonDocument.Parse (System.String json) [0x00014] in :0 
      at Data.LocalData.SetUserData (System.String jsonString) [0x00005] in F:\UnityProjects\Wagon\Assets\Scripts\Data\LocalData.cs:76 </p>
  </blockquote>
  
  <p>It's kind of weird, because it cannot deserialize its own Json. 
  Any suggestions?</p>
  
  <hr>
  
  <h2>Added later</h2>
  
  <p>Well, seems like ASP server adds something to a string (I suppose the problem is with escape slashes), because this works fine on serverside:</p>
  
  <pre><code>var result = user.ToJson();
  var bson = BsonDocument.Parse(result);
  WriteLine(bson); // output here is as expected
  </code></pre>
  
  <hr>
  
  <h2>More info</h2>
  
  <p>The problem is with 2 double qoutes at the beginning and the end of the string after it comes from ASP backend
  <a href="https://i.stack.imgur.com/Yi1dv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yi1dv.jpg" alt="enter image description here"></a></p>
  
  <p>I don't know how to avoid this behavior, so for now I've just used this crutch to solve it</p>
  
  <pre><code>jsonString = jsonString.Substring(1, jsonString.Length - 2);
  </code></pre>
  
  <p>Not really elegant solution, so I'm not putting it as an answer, but it works</p>