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