开发者

In Play, when rendering JSON can I use anonymous types and/or object initializers?

开发者 https://www.devze.com 2023-04-11 13:13 出处:网络
I\'m writing a Play application that will have dozens of controller actions that return JSON. The format of each JSON result is slightly different, built up from a few primitives.

I'm writing a Play application that will have dozens of controller actions that return JSON. The format of each JSON result is slightly different, built up from a few primitives.

I would like to avoid creating a Java class to hold the return type for each action method, so currently I am using a HashMap, like this:

// used to populate the filters for an iphone app
public static void filters()
{
    // get four lists from the database
    List<Chef> chefs= Chef.find("order by name asc").fetch();
    List<Cuisine> cuisin开发者_StackOverflow社区es = Cuisine.find("order by name asc").fetch(); 
    List<Meal> meals  = Meal.find("order by name asc").fetch(); 
    List<Ingredient> ingredients = Ingredient.find("order by name asc").fetch(); 
    // return them as JSON map
    Map<String,Object> json = new HashMap<String,Object>();
    json.put("chefs", chefs);
    json.put("cuisines", cuisines);
    json.put("meals", meals);
    json.put("ingredients", ingredients);
    renderJSON(json);
}

This returns JSON that looks like this, which is what I want:

{ 
  "chefs": [{},{},...{}],
  "cuisines": [{},{},...{}],
  "meals": [{},{},...{}],
  "ingredients": [{},{},...{}]
}

I feel like the syntax to construct the HashMap is redundant. I don't have a ton of Java experience, so I'm comparing to C# which lets me use an anonymous type with an object initializer, to cut down on the code like so:

return Json(new
{
    chefs = chefs,
    cuisines = cuisines,
    meals = meals,
    ingredients = ingredients
});

Is there anything in the Java/Play world that let's me write this kind of code more compactly?


There is no exact equivalent to the C# construct in Java, however you can create an anonymous object and initialize it using the idiom illustrated below:

public static void filters()
{
  renderJSON(new HashMap<String,Object>(){{

    // get four lists from the database
    List<Chef> chefs= Chef.find("order by name asc").fetch();
    List<Cuisine> cuisines = Cuisine.find("order by name asc").fetch(); 
    List<Meal> meals  = Meal.find("order by name asc").fetch(); 
    List<Ingredient> ingredients = Ingredient.find("order by name asc").fetch(); 

    // return them as JSON map
    put("chefs", chefs);
    put("cuisines", cuisines);
    put("meals", meals);
    put("ingredients", ingredients);

  }});
}

(You could put the four lists declarations outside the anonymous type initializer, but then you would need to declare them final.)


Java does have anonymous types, which you should be able to use just fine:

// these would be accessed from DB or such; must be final so anonymous inner class can access
final String _name = "Bob";
final int _iq = 125;
renderJSON(new Object() {
  public String name = nameValue; // or define 'getName()'
  public int iq = iqValue; // ditto here
});

this creates anonymous inner class, and then JSON data binder should be able introspect it, serialize stuff and so on.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号