Search code examples
c#.netmongodbmongodb-csharp-2.0

JSON reader was expecting a value but found 'function'


I have a collection that contains BsonJavascript object.

{ 
    "Name" : "HourlyMP", 
    "MapFunction" : function(){
        var _id = this.srcip + " - " + this.hour
        var valueData = {
            ip: this.srcip,
            session: 1
        }
        emit(_id, valueData);
    } 
}

As you see the value of "MapFunction" field is BsonJavascript. I can Export and Import this data successfully. But when I Deserialize exported json into BsonDocument i get this error:

JSON reader was expecting a value but found 'function'

By the way i am using official C# 2.2 driver and my deserialize code is below:

BsonSerializer.Deserialize<BsonDocument>(myjsonstring)

Edit

I defined function as string then converted it to BsonJavascriptFunction inside my code. This solved my problem. Thanks for reply


Solution

  • According to the documentation, BsonSerializer.Deserialize<TNominalType>(String) deserializes a JSON string.

    Functions aren't valid in JSON.