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.uon.UonParser;
017import org.apache.juneau.uon.UonSerializer;
018
019/**
020 * Sample class which shows the simple usage of UONSerializer.
021 *
022 * <h5 class='section'>See Also:</h5><ul>
023 * </ul>
024 */
025public class UonExample {
026
027   /**
028    * Serializing SimplePojo bean into UON type
029    * and Deserialize back to Pojo instance type.
030    *
031    * @param args Unused.
032    * @throws Exception Unused.
033    */
034   public static void main(String[] args) throws Exception {
035
036      // Fill some data to a Pojo bean
037      Pojo pojo = new Pojo("id","name");
038
039      /**
040       * Produces
041       * (name=name,id=id)
042       */
043      String serial = UonSerializer.DEFAULT.serialize(pojo);
044      System.out.println(serial);
045
046      // Deserialize back to Pojo instance
047      Pojo obj = UonParser.DEFAULT.parse(serial, Pojo.class);
048
049      assert obj.getId().equals(pojo.getId());
050      assert obj.getName().equals(pojo.getName());
051
052      // The object above can be parsed thanks to the @Beanc annotation on PojoComplex
053      // Using this approach, you can keep your POJOs immutable, and still serialize and deserialize them.
054
055   }
056}