concept JValue in category scalatra

appears as: JValue, JValue, A JValue
Scalatra in Action

This is an excerpt from Manning's book Scalatra in Action.

Let’s take a closer look at what happens in this listing. The JacksonJsonSupport trait is responsible for parsing an incoming JSON request to a JValue as well as for writing a result of type JValue as JSON text to a response. This is accomplished by hooking into Scalatra’s request-response cycle.

The GET route returns a JValue as a result, which is written as JSON text to the response. Generally, if you intend to output JSON using Scalatra’s JSON support, your JSON routes should always return a value of type JValue. When the route result is of type JValue, it’s set implicitly to application/json by JacksonJsonSupport.

There are three methods of creating a JValue in the Json4s library:

Listing 5.6. Producing a JValue with the DSL
val recipeJson = ("title" -> "Penne with cocktail tomatoes, Rucola and Goat cheese") ~ ("details" -> detailsJson) ~ ("ingredients" -> List( ("label" -> "Penne") ~ ( "quantity" -> "250g"), ("label" -> "Cocktail tomatoes") ~ ( "quantity" -> "300g"), ("label" -> "Rucola") ~ ( "quantity" -> "2 handful"), ("label" -> "Goat cheese") ~ ( "quantity" -> "250g"), ("label" -> "Garlic cloves") ~ ( "quantity" -> "250g"))) ~ ("steps" -> List( "Cook noodles until aldente.", "Quarter the tomatoes, wash the rucola, dice the goat's cheese ...", "Heat olive oil in a pan, add the garlic and the tomatoes and ...", "Shortly before the noodles are ready add the rucola to the ...", "Drain the noodles and mix with the tomatoes, finally add the ..."))