Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate JSON With JSON-Views

I am trying to use JSON-Views in Grails 3.1.

I have the following controller:

package myapp

BasketController {

    def index(ProductFilterCommand cmd) {

        [basketList: service.findAllBaskets()]
    }
}

And the following classes:

package myapp

class Basket {
    List<BasketItem> items
}

class BasketItem  {
    String name 
}

Here are the gson files which I thought would work:

basket/index.gson

import myapp.Basket

model {
    Iterable<Basket> basketList
}

json.baskets(basketList) {
    g.render(template: "basket", model: [basket: it])
}

basket/_basket.gson

import myapp.Basket

model {
    Basket basket
}

json.items(basket.items) { 
    g.render(template: "item", model:[item: it])
}

basket/_item.gson

import myapp.Item

model {
    Item item
}

json g.render(item)

I want to generate json such as:

{
    "baskets": [{
        "items": [{
            "name": "T-shirt"
        }, {
            "name": "Pants"
        }]
    }, {
        "items": [{
            "name": "T-shirt"
        }, {
            "name": "Pants"
        }]
    }]
}

But instead I am getting:

{
  "baskets": [
    {},
    {}
  ]
}
like image 931
Sergio del Amo Avatar asked Feb 08 '16 16:02

Sergio del Amo


People also ask

What is a JSON view?

JSON Viewer is an online web-based tool which helps to view, analyze JSON data simply along with formatting. Just upload JSON file/paste JSON code & view it. Development Tools.

What is JsonView annotation?

The JsonView annotation can be used to include/exclude a property during the serialization and deserialization process dynamically. We need to configure an ObjectMapper class to include the type of view used for writing a JSON from Java object using the writerWithView() method.

What is JsonProperty annotation?

@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.

What is Jackson mixin?

Jackson mixins is a mechanism to add Jackson annotations to classes without modifying the actual class. It was created for those cases where we can't modify a class such as when working with third-party classes. We can use any Jackson annotation but we don't add them directly to the class.


1 Answers

Looks like a bug to me. The only way to achieve what you are looking for is to use the views as shown below. Also note the usage of collection instead of model. I would file a bug with the sample app I used to test below.

Note the usage of template as a fully qualified name basket/item. This is the defect.

//index.gson
import com.example.Basket

model {
    Iterable<Basket> basketItems
}

json {
    baskets g.render(template: 'basket', collection: basketItems, var: 'basket')
}

//_basket.gson
import com.example.Basket

model {
    Basket basket
}

json {
    items g.render(template: "basket/item", collection: basket.items, var: 'item')
}

//_item.gson
import com.example.BasketItem

model {
    BasketItem item
}

json g.render(item)

//or if id is not required in response
/*json {
    name item.name
}*/
like image 171
dmahapatro Avatar answered Sep 18 '22 06:09

dmahapatro