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.xml;
014
015import java.util.*;
016
017import org.apache.juneau.examples.core.pojo.*;
018import org.apache.juneau.xml.*;
019
020/**
021 * Sample class which shows the complex usage of XmlSerializer.
022 *
023 * <ul class='seealso'>
024 *    <li class='extlink'>{@source}
025 * </ul>
026 */
027public class XmlComplexExample {
028
029   /**
030    * Serializing PojoComplex bean into human readable XML
031    * and Deserialize back to PojoComplex instance type.
032    *
033    * @param args Unused.
034    * @throws Exception Unused.
035    */
036   public static void main(String[] args) throws Exception {
037
038      // Fill some data to a PojoComplex bean
039      HashMap<String, List<Pojo>> values = new HashMap<>();
040      ArrayList<Pojo> setOne = new ArrayList<>();
041      setOne.add(new Pojo("1.1", "name1"));
042      setOne.add(new Pojo("1.1", "name2"));
043      ArrayList<Pojo> setTwo = new ArrayList<>();
044      setTwo.add(new Pojo("1.2", "name1"));
045      setTwo.add(new Pojo("1.2", "name2"));
046      values.put("setOne", setOne);
047      values.put("setTwo", setTwo);
048      PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
049
050      // Serialize to human readable XML and print
051      String serial = XmlSerializer.DEFAULT_SQ_READABLE.serialize(pojoc);
052      System.out.println(serial);
053
054      // Deserialize back to PojoComplex instance
055      PojoComplex obj = XmlParser.DEFAULT.parse(serial, PojoComplex.class);
056
057      assert obj.getClass().equals(pojoc.getClass());
058      assert obj.getInnerPojo().getId().equals(pojoc.getInnerPojo().getId());
059
060      // The object above can be parsed thanks to the @Beanc annotation on PojoComplex
061      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
062
063   }
064}