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.uon;
014
015import org.apache.juneau.examples.core.pojo.Pojo;
016import org.apache.juneau.examples.core.pojo.PojoComplex;
017import org.apache.juneau.uon.UonParser;
018import org.apache.juneau.uon.UonSerializer;
019
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023
024/**
025 * UON complex example.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 * </ul>
029 */
030public class UonComplexExample {
031   /**
032    * Serializing PojoComplex bean into UON format.
033    *
034    * @param args Unused.
035    * @throws Exception Unused.
036    */
037   public static void main(String[] args) throws Exception {
038
039      // Fill some data to a PojoComplex bean
040      HashMap<String, List<Pojo>> values = new HashMap<>();
041      ArrayList<Pojo> setOne = new ArrayList<>();
042      setOne.add(new Pojo("1.1", "name1"));
043      setOne.add(new Pojo("1.1", "name2"));
044      ArrayList<Pojo> setTwo = new ArrayList<>();
045      setTwo.add(new Pojo("1.2", "name1"));
046      setTwo.add(new Pojo("1.2", "name2"));
047      values.put("setOne", setOne);
048      values.put("setTwo", setTwo);
049      PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
050
051      // this creates an RDF serializer with the default XML structure
052      /**Produces
053       * (innerPojo=(name=name0,id='1.0'),
054       * values=(setOne=@((name=name1,id='1.1'),(name=name2,id='1.1')),
055       * setTwo=@((name=name1,id='1.2'),(name=name2,id='1.2'))),id=pojo)
056       */
057      UonSerializer uonSerializer = UonSerializer.DEFAULT;
058      // This will show the final output from the bean
059      System.out.println(uonSerializer.serialize(pojoc));
060
061      PojoComplex obj = UonParser.DEFAULT.parse(uonSerializer.serialize(pojoc), PojoComplex.class);
062
063      assert obj.getId().equals(pojoc.getId());
064
065   }
066}