Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON data from RavenDB

I have an Asp.Net MVC3 application that use embedded RavenDB to store data. The view needs json data that is now created by the controller in this way:

    public ContentResult Data()
    {
        var res = JsonConvert.SerializeObject(DocumentSession.Query<DataObject>());
        return new ContentResult { Content = res, ContentType = "application/json" };
    }

Everything works fine but to me it seems inefficient because data that are stored in DB in JSON format is serialized in POCO and then deserialized again.

Is there a more direct way to get json data directly from the embedded db?

like image 870
Roberto Avatar asked Mar 01 '26 06:03

Roberto


1 Answers

It's not inefficient at all. Keep in mind that internally, raven actually uses BSON - so you would have to translate it anyway. Also there are metadata fields. If you were to return it directly through your controller, you would have no opportunity to shape the response of the data and strip off the unwanted fields.

If you must continue with this line of thinking, you have two options:

  1. You could use the DocumentStore.DatabaseCommands.Get() and related operations to return RavenJObjects that you could then translate JSON from.

  2. You could talk directly to the Raven database over HTTP without using the raven client.

Neither of these are straightforward, and you are throwing away a lot of goodness of the Raven Client API. IMHO, any performance gain you were to achieve would be unnoticeable. I would stick with your current approach.

Also - If you are just trying to avoid having to serialize here, consider returning a JsonResult instead of a ContentResult. If you want to use Json.Net instead (per your other recent post), Here is a cleaner way to do it: http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx

like image 112
Matt Johnson-Pint Avatar answered Mar 04 '26 17:03

Matt Johnson-Pint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!