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