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