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