001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.examples.core.xml;
018
019import java.util.*;
020
021import org.apache.juneau.examples.core.pojo.*;
022import org.apache.juneau.xml.*;
023
024/**
025 * Sample class which shows the complex usage of XmlSerializer.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 * </ul>
029 */
030public class XmlComplexExample {
031
032   /**
033    * Serializing PojoComplex bean into human readable XML
034    * and Deserialize back to PojoComplex instance type.
035    *
036    * @param args Unused.
037    * @throws Exception Unused.
038    */
039   public static void main(String[] args) throws Exception {
040
041      // Fill some data to a PojoComplex bean
042      HashMap<String, List<Pojo>> values = new HashMap<>();
043      ArrayList<Pojo> setOne = new ArrayList<>();
044      setOne.add(new Pojo("1.1", "name1"));
045      setOne.add(new Pojo("1.1", "name2"));
046      ArrayList<Pojo> setTwo = new ArrayList<>();
047      setTwo.add(new Pojo("1.2", "name1"));
048      setTwo.add(new Pojo("1.2", "name2"));
049      values.put("setOne", setOne);
050      values.put("setTwo", setTwo);
051      PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
052
053      // Serialize to human readable XML and print
054      String serial = XmlSerializer.DEFAULT_SQ_READABLE.serialize(pojoc);
055      System.out.println(serial);
056
057      // Deserialize back to PojoComplex instance
058      PojoComplex obj = XmlParser.DEFAULT.parse(serial, PojoComplex.class);
059
060      assert obj.getClass().equals(pojoc.getClass());
061      assert obj.getInnerPojo().getId().equals(pojoc.getInnerPojo().getId());
062
063      // The object above can be parsed thanks to the @Beanc annotation on PojoComplex
064      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
065
066   }
067}