json - Web API controller for CRUD operations using mongoDB -
i'm trying create api controller crud operations using mongodb. keep in mind not know structure of these collections , whole idea of using mongodb take advantage of document storage option along ability query without having worry designing models. here had before:
// api/mongo public ienumerable<bsondocument> get(string database, string collection) { db = server.getdatabase(database); return db.getcollection(collection).findall().asenumerable(); }
but above getting error:
{ "message": "an error has occurred.", "exceptionmessage": "the 'objectcontent`1' type failed serialize response body content type 'application/json; charset=utf-8'.", "exceptiontype": "system.invalidoperationexception", "stacktrace": null, "innerexception": { "message": "an error has occurred.", "exceptionmessage": "error getting value '__emptyinstance' on 'mongodb.bson.objectid'.", "exceptiontype": "newtonsoft.json.jsonserializationexception", "stacktrace": " @ newtonsoft.json.serialization.dynamicvalueprovider.getvalue(object target)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.calculatepropertyvalues(jsonwriter writer, object value, jsoncontainercontract contract, jsonproperty member, jsonproperty property, jsoncontract& membercontract, object& membervalue)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializelist(jsonwriter writer, iwrappedcollection values, jsonarraycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializelist(jsonwriter writer, iwrappedcollection values, jsonarraycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serialize(jsonwriter jsonwriter, object value)\r\n @ newtonsoft.json.jsonserializer.serializeinternal(jsonwriter jsonwriter, object value)\r\n @ newtonsoft.json.jsonserializer.serialize(jsonwriter jsonwriter, object value)\r\n @ system.net.http.formatting.jsonmediatypeformatter.<>c__displayclassd.<writetostreamasync>b__c()\r\n @ system.threading.tasks.taskhelpers.runsynchronously(action action, cancellationtoken token)", "innerexception": { "message": "an error has occurred.", "exceptionmessage": "common language runtime detected invalid program.", "exceptiontype": "system.invalidprogramexception", "stacktrace": " @ get__emptyinstance(object )\r\n @ newtonsoft.json.serialization.dynamicvalueprovider.getvalue(object target)" } } }
mongocusror has method tojson() using able this:
[ { "_id": objectid("520d4776a9a3f31f54ebdba6"), "name": "john" }, { "_id": objectid("520d4b77a9a3f31f54ebdba7"), "name": "chi", "nickname": "cdawg" }, { "_id": objectid("520d4c13a9a3f31f54ebdba8"), "name": "ak", "nickname": "afro", "address": { "state": "ny" } } ]
so can see web api pipeline having trouble using available formatters serialize type objectid not simple type. case xml formatter.
i have re-written method follows:
// api/mongo public jarray get(string database, string collection) { db = server.getdatabase(database); return jarray.parse(db.getcollection(collection).findall().tojson(new jsonwritersettings { outputmode = jsonoutputmode.strict })); }
this has worked me , returns following:
[ { "_id": { "$oid": "520d4776a9a3f31f54ebdba6" }, "name": "john" }, { "_id": { "$oid": "520d4b77a9a3f31f54ebdba7" }, "name": "chi", "nickname": "cdawg" }, { "_id": { "$oid": "520d4c13a9a3f31f54ebdba8" }, "name": "akash", "nickname": "ak", "address": { "state": "ny" } } ]
but not method number of reasons:
- setting accept header to'text/xml' not work kind of kills web api's awesomeness
- it seems such waste serialize collection json , in-turn parse again return it
- i really don't , there must better way accomplish this
i've read using [bsonid]
or [jsonignore]
attributes etc, but, not have typed model apply these against. , way can me leverage web api's content negotiation.
your suggestions appreciated.
Comments
Post a Comment