Practical ASP.NET

Returning Raw JSON Data in Web API with Marten

Here's a trick to have Marten automatically deserialize JSON data only when you want it to.

Marten is an open source .NET document database library that allows the storing, loading, updating and deleting of objects as documents in an underlying PostgreSQL database.

Marten stores .NET objects as JSON data in the underlying PostgreSQL database. When these documents are retrieved from the database, Marten deserializes the JSON data back into .NET objects to be used in the application.

Normally this automatic deserialization is exactly what's required -- once you get the document back as an object you can manipulate it just like any other object in the application. However, in some circumstances this automatic deserialization can introduce an unnecessary processing overhead. One such circumstance is the use of Web API to retrieve documents and return them to the client.

Creating a Basic Web API Project That Uses Marten
After creating a new ASP.NET Web API project in Visual Studio, the first thing to do is to define a document class to represent a Player in a fictitious Role Playing Game:

public class Player
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int CurrentHealth { get; set; }
}

Next, the API controller can be created that allows the POSTing of new Players to be added to the database and GET methods to retrieve Players. For simplicity in the example in Listing 1, additional methods such as PUT, validation and logging have not been included.

Listing 1: Initial PlayersController
using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using Marten;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
  public class PlayersController : ApiController
  {
    private readonly IDocumentSession _session;

    public PlayersController(IDocumentSession session)
    {
      _session = session;
    }

    // GET api/players
    public IEnumerable<Player> Get()
    {
      return _session.Query<Player>();
    }

    // GET api/players/1
    public Player Get(int id)
    {
      var player = _session.Query<Player>().Where(x => x.Id == id).FirstOrDefault();

      if (player != null)
      {
        return player;
      }

      throw new HttpResponseException(HttpStatusCode.NotFound);
    }

    // POST api/players
    public void Post([FromBody] Player player)
    {
      // Validation, etc.

      _session.Store(player);
      _session.SaveChanges();
    }
  }
}

Note in Listing 1 the Marten IDocumentSession is being injected into the controller via its constructor. This assumes that IoC has been configured for the application and that the correct Marten types have been registered with the container. For example, to use StructureMap, the StructureMap.WebApi2 NuGet package can be installed and the Marten types registered, as shown in Listing 2.

Listing 2: Registering Marten Types in the StructreMap DefaultRegistry
ForSingletonOf<IDocumentStore>().Use("Create DocumentStore", () =>
{
  return DocumentStore.For(configure =>
  {
    // Connection would normally be stored in configuration somewhere
    configure.Connection("host = localhost; database = RPGDatabase; 
      password = g7qo84nck22i; username = postgres"); 
    configure.AutoCreateSchemaObjects = AutoCreate.All;
  });
});

For<IDocumentSession>()
  .LifecycleIs<ContainerLifecycle>()
  .Use("Lightweight Marten Document Session", x => x.GetInstance<IDocumentStore>().LightweightSession());

At this point, new Players can be POSTed (as JSON) to /api/players, all players can be retrieved via a GET to /api/players and a single player can be retrieved via a GET to /api/players/{id}.

Reducing Deserialization/Serialization Overhead
In the implementation of the GETs in Listing 1 Marten is deserializing the JSON into Player objects, only for the object to be serialized back to JSON to be returned via Web API. This overhead can be reduced by instructing Marten to not perform deserialization and instead just return the JSON string as stored in PostgreSQL. We can then just return this as a string result from the controller action.

In the case of multiple results, the Marten .ToJsonArray extension method can be included in the LINQ code. This method returns multiple documents inside a JSON array. The following code shows an updated GET method that uses .ToJsonArray to return the JSON stored in the database directly to the clien:

// GET api/players
public HttpResponseMessage Get()
{
  string rawJsonFromDb = _session.Query<Player>().ToJsonArray();

  var response = Request.CreateResponse(HttpStatusCode.OK);
  response.Content = new StringContent(rawJsonFromDb, Encoding.UTF8, "application/json");
  return response;
}

The "find single Player" GET method can also be modified, this time using the Marten AsJson extension method as shown in Listing 3.

Listing 3: Returning a Single Document as Raw JSON
// GET api/players/1
public HttpResponseMessage Get(int id)
{
  var rawJsonFromDb = 
    _session.Query<Player>().Where(x => x.Id == id).AsJson().FirstOrDefault();

  if (!string.IsNullOrEmpty(rawJsonFromDb))
  {
    var response = Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(rawJsonFromDb, Encoding.UTF8, "application/json");
    return response;
  }

  throw new HttpResponseException(HttpStatusCode.NotFound);
}

To learn more about Marten, check out the documentation and the series of articles on the Don't Code Tired blog.

About the Author

Jason Roberts is a Microsoft C# MVP with over 15 years experience. He writes a blog at http://dontcodetired.com, has produced numerous Pluralsight courses, and can be found on Twitter as @robertsjason.

comments powered by Disqus

Featured

Subscribe on YouTube