001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.examples.core.html; 014 015import org.apache.juneau.examples.core.pojo.Pojo; 016import org.apache.juneau.examples.core.pojo.PojoComplex; 017import org.apache.juneau.html.HtmlSerializer; 018import org.apache.juneau.html.HtmlParser; 019 020import java.util.ArrayList; 021import java.util.HashMap; 022import java.util.List; 023 024/** 025 * Sample class which shows the complex usage of HtmlSerializer and HtmlParser. 026 */ 027public class HtmlComplexExample { 028 029 /** 030 * Serializing PojoComplex bean into Html type 031 * and Deserialize back to PojoComplex instance type. 032 * 033 * @param args 034 * @throws Exception 035 */ 036 public static void main(String[] args) throws Exception { 037 // Juneau provides static constants with the most commonly used configurations 038 // Get a reference to a serializer - converting POJO to flat format 039 /** 040 * Produces 041 * <table><tr><td>innerPojo</td><td><table><tr><td>name</td><td>name0</td></tr> 042 * <tr><td>id</td><td>1.0</td></tr></table></td></tr><tr><td>values</td><td><table> 043 * <tr><td>setOne</td><td><table _type="array"><tr><th>name</th><th>id</th></tr> 044 * <tr><td>name1</td><td>1.1</td></tr><tr><td>name2</td><td>1.1</td></tr> 045 * </table></td></tr><tr><td>setTwo</td><td><table _type="array"><tr><th>name 046 * </th><th>id</th></tr><tr><td>name1</td><td>1.2</td></tr><tr><td>name2</td><td>1.2 047 * </td></tr></table></td></tr></table></td></tr><tr><td>id</td><td>pojo</td></tr></table> 048 */ 049 HtmlSerializer htmlSerializer = HtmlSerializer.DEFAULT; 050 // Get a reference to a parser - converts that flat format back into the POJO 051 HtmlParser htmlParser = HtmlParser.DEFAULT; 052 053 // Fill some data to a PojoComplex bean 054 HashMap<String, List<Pojo>> values = new HashMap<>(); 055 ArrayList<Pojo> setOne = new ArrayList<>(); 056 setOne.add(new Pojo("1.1", "name1")); 057 setOne.add(new Pojo("1.1", "name2")); 058 ArrayList<Pojo> setTwo = new ArrayList<>(); 059 setTwo.add(new Pojo("1.2", "name1")); 060 setTwo.add(new Pojo("1.2", "name2")); 061 values.put("setOne", setOne); 062 values.put("setTwo", setTwo); 063 PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values); 064 065 String flat = htmlSerializer.serialize(pojoc); 066 067 // Print out the created POJO in JSON format. 068 System.out.println(flat); 069 070 PojoComplex parse = htmlParser.parse(flat, PojoComplex.class); 071 072 assert parse.getId().equals(pojoc.getId()); 073 assert parse.getInnerPojo().getName().equals(pojoc.getInnerPojo().getName()); 074 assert parse.getInnerPojo().getId().equals(pojoc.getInnerPojo().getId()); 075 076 // The object above can be parsed thanks to the @BeanConstructor(properties = id,name) annotation on Pojo 077 // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them. 078 } 079}