My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

luni, 2 noiembrie 2015

[OmniFaces utilities (2.0)] Encode given object as JSON


[OmniFaces utilities] The toJson() function encodes the given object as JSON.

Function:
See also: Json#encode()
Usage:

Per example, let's suppose that we need to encode to JSON the below list of players (it is pretty easy to intuit how the Player bean looks like):

@Named
@ViewScoped
public class PlayerBean implements Serializable {

 private List<Player> players = new ArrayList<>();

 public PlayerBean() {

  players.add(new Player(2, "NOVAK DJOKOVIC", (byte) 26, "Belgrade, Serbia", "Monte Carlo, Monaco", (short) 188, (byte) 80, "Boris Becker, Marian Vajda", sdf.parse("22.05.1987")));
  players.add(new Player(1, "RAFAEL NADAL", (byte) 27, "Manacor, Mallorca, Spain", "Manacor, Mallorca, Spain", (short) 185, (byte) 85, "Toni Nadal", sdf.parse("03.06.1986")));
  players.add(new Player(7, "TOMAS BERDYCH", (byte) 28, "Valasske Mezirici, Czech", "Monte Carlo, Monaco", (short) 196, (byte) 91, "Tomas Krupa", sdf.parse("17.09.1985")));
  players.add(new Player(8, "STANISLAS WAWRINKA", (byte) 28, "Lausanne, Switzerland", "St. Barthelemy, Switzerland", (short) 183, (byte) 81, "Magnus Norman", sdf.parse("28.03.1985")));
  players.add(new Player(4, "ANDY MURRAY", (byte) 26, "Dunblane, Scotland", "London, England", (short) 190, (byte) 84, "Ivan Lendl", sdf.parse("15.05.1987")));
  players.add(new Player(5, "JUAN MARTIN DEL POTRO", (byte) 25, "Tandil, Argentina", "Tandil, Argentina", (short) 198, (byte) 97, "Franco Davin", sdf.parse("23.09.1988")));
  players.add(new Player(10, "JO-WILFRIED TSONGA", (byte) 28, "Le Mans, France", "Gingins, Switzerland", (short) 188, (byte) 91, "Nicolas Escude & Thierry Ascione", sdf.parse("17.04.1985")));
  players.add(new Player(6, "ROGER FEDERER", (byte) 32, "Basel, Switzerland", "Bottmingen, Switzerland", (short) 185, (byte) 85, "Stefan Edberg, Severin Luthi", sdf.parse("08.08.1981")));
  players.add(new Player(9, "RICHARD GASQUET", (byte) 27, "Beziers, France", "Neuchatel, Switzerland", (short) 185, (byte) 75, "Sergi Bruguera and Sebastien", sdf.parse("18.06.1986")));
  players.add(new Player(3, "DAVID FERRER", (byte) 31, "Javea, Spain", "Valencia, Spain", (short) 175, (byte) 73, "Jose Francisco Altur", sdf.parse("02.04.1982")));
 }

 public List<Player> getPlayers() {
  return players;
 }

 public void setPlayers(List<Player> players) {
  this.players = players;
 }
}

In page we simply call of:toJson(), as below:

<p>
 #{of:toJson(playerBean.players)}
</p>


Outputs:

[{"age":26,"birthplace":"Belgrade, Serbia","born":"Thu, 21 May 1987 21:00:00 GMT","coach":"Boris Becker, Marian Vajda","height":188,"player":"NOVAK DJOKOVIC","ranking":2,"residence":"Monte Carlo, Monaco","weight":80},{"age":27,"birthplace":"Manacor, Mallorca, Spain","born":"Mon, 02 Jun 1986 21:00:00 GMT","coach":"Toni Nadal","height":185,"player":"RAFAEL NADAL","ranking":1,"residence":"Manacor, Mallorca, Spain","weight":85},{"age":28,"birthplace":"Valasske Mezirici, Czech","born":"Mon, 16 Sep 1985 21:00:00 GMT","coach":"Tomas Krupa","height":196,"player":"TOMAS BERDYCH","ranking":7,"residence":"Monte Carlo, Monaco","weight":91},{"age":28,"birthplace":"Lausanne, Switzerland","born":"Wed, 27 Mar 1985 22:00:00 GMT","coach":"Magnus Norman","height":183,"player":"STANISLAS WAWRINKA","ranking":8,"residence":"St. Barthelemy, Switzerland","weight":81},{"age":26,"birthplace":"Dunblane, Scotland","born":"Thu, 14 May 1987 21:00:00 GMT","coach":"Ivan Lendl","height":190,"player":"ANDY MURRAY","ranking":4,"residence":"London, England","weight":84},{"age":25,"birthplace":"Tandil, Argentina","born":"Thu, 22 Sep 1988 21:00:00 GMT","coach":"Franco Davin","height":198,"player":"JUAN MARTIN DEL POTRO","ranking":5,"residence":"Tandil, Argentina","weight":97},{"age":28,"birthplace":"Le Mans, France","born":"Tue, 16 Apr 1985 21:00:00 GMT","coach":"Nicolas Escude & Thierry Ascione","height":188,"player":"JO-WILFRIED TSONGA","ranking":10,"residence":"Gingins, Switzerland","weight":91},{"age":32,"birthplace":"Basel, Switzerland","born":"Fri, 07 Aug 1981 21:00:00 GMT","coach":"Stefan Edberg, Severin Luthi","height":185,"player":"ROGER FEDERER","ranking":6,"residence":"Bottmingen, Switzerland","weight":85},{"age":27,"birthplace":"Beziers, France","born":"Tue, 17 Jun 1986 21:00:00 GMT","coach":"Sergi Bruguera and Sebastien","height":185,"player":"RICHARD GASQUET","ranking":9,"residence":"Neuchatel, Switzerland","weight":75},{"age":31,"birthplace":"Javea, Spain","born":"Thu, 01 Apr 1982 21:00:00 GMT","coach":"Jose Francisco Altur","height":175,"player":"DAVID FERRER","ranking":3,"residence":"Valencia, Spain","weight":73}]

If we pass the result through a JSON beautifer we will obtain this:

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors