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