C# - Servicestack MongoDB JSON Objects Unescape -
i have got 2 problems.
i have local mongodb several collections.
a database object looks this:
my configuration looks this:
using funq; using servicestack; using servicestack.text; namespace protocol.server.api { public class apihost : appselfhostbase { public apihost() : base("api rest service", typeof(apihost).assembly) { } public override void configure(container container) { plugins.add(new corsfeature()); setconfig(new hostconfig { defaultcontenttype = mimetypes.json, enablefeatures = feature.all.remove(feature.html) }); jsconfig.convertobjecttypesintostringdictionary = true; } } }
my request definition this: public async task get(getobjects request) { var collection = connect.database.getcollection("cylinders"); var aggregate = collection.find( => true); var results = await aggregate.tolistasync(); return results; }
using servicestack; namespace protocol.server.api.clients { [route("/objects")] public class getobjects : ireturn<string> { } } using mongodb.bson; using mongodb.driver; using mongodbtest; using servicestack; using system; using system.threading.tasks; namespace protocol.server.api.clients { public class clientservice : servicestack.service { public async task<object> get(getobjects request) { var collection = connect._database.getcollection<bsondocument>("cylinders"); var aggregate = collection.find(_ => true); var results = await aggregate.tolistasync(); return results; } } }
problem 1: json objects not escaped , quite strange (backslahes , on).
problem 2 (aggregation): want aggregate objects in specific format.
i wrote this:
var collection = connect._database.getcollection<bsondocument>("cylinders"); var aggregate = collection.aggregate().group(new bsondocument { { "_id", "$_id" }, }); var results = await aggregate.tolistasync();
this works perfectly, if want aggregate more fields, not work:
var aggregate = collection.aggregate().group(new bsondocument { { "_id", "$_id" }, { "availableat", "$description.availableat" } });
how solve problems?
it's not recommended return json string in servicestack service prohibits being able access response in other format , prevents many other of servicestack's surrounding metadata features around services.
your services should returning clean poco's can serialized in registered format, returning bsondocument
poor response serialize , explains why you're getting escaped json you're serializing object contains json in string instead of clean dto.
so should change services return clean poco's like:
public async task<list<cylinder>> get(getobjects request) { var collection = connect._database.getcollection<cylinder>(); var aggregate = collection.find(_ => true); var results = await aggregate.tolistasync(); return results; }
Comments
Post a Comment