Hyper9 Common provides several interfaces and utility classes in order to simplify working with Java beans, specifically with regards to serialization. Classes implementing the Hyper9 Common Bean interface are immediately able to serialize themselves to JSON and XML (RSS/ATOM is coming soon).

A Hyper9 Common Bean

So what does a Hyper9 Common Bean look like? Well, it's pretty simple:

/*
 * Copyright (c) 2009, Hyper9 All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. Neither the name of Hyper9 nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission. THIS SOFTWARE IS PROVIDED
 * BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.hyper9.common.beans;

import java.io.Serializable;

/**
 * The root interface for all types that can returned to a client via a RESTful
 * query.
 * 
 * @author akutz
 * 
 */
public interface Bean extends Serializable
{
    /**
     * Serializes the bean to a JSON string, excluding properties that have a
     * null value and transient properties. This method may throw an exception,
     * but due to Object.toString()'s erasure the exception will be trapped and
     * not re-thrown. Overloads of this method re-throw the exception.
     * 
     * @return A JSON string, excluding properties that have a null value and
     *         transient properties.
     */
    @Override
    public String toString();

    /**
     * Serialize the bean to a string, excluding properties that have a null
     * value and transient properties.
     * 
     * @param format The serialization format to use.
     * @return The serialized bean, excluding properties that have a null value
     *         and transient properties.
     * @throws Exception When an error occurs.
     */
    public String toString(BeanSerializationFormats format) throws Exception;

    /**
     * Serialize the bean to a string.
     * 
     * @param format The serialization format to use.
     * @param includeNulls A value indicating whether or not to include
     *        properties that have a null value.
     * @param includeTransients A value indicating whether or not to include
     *        transient properties.
     * @return The serialized bean.
     * @throws Exception When an error occurs.
     */
    public String toString(
        BeanSerializationFormats format,
        boolean includeNulls,
        boolean includeTransients) throws Exception;

    /**
     * Serialize the bean to a string.
     * 
     * @param format The serialization format to use.
     * @param includeNulls A value indicating whether or not to include
     *        properties that have a null value.
     * @param includeTransients A value indicating whether or not to include
     *        transient properties.
     * @param callback A callback used to influence the beans in the list (as
     *        well as any child beans they may point to) before they are
     *        serialized into JSON strings.
     * @return The serialized bean.
     * @throws Exception When an error occurs.
     */
    public String toString(
        BeanSerializationFormats format,
        boolean includeNulls,
        boolean includeTransients,
        BeanSerializationCallback<Bean> callback) throws Exception;
}

Essentially a Hyper9 Common Bean requires implementing classes to provide several methods to assist in serializing itself to a string. It would seem logical then for Hyper9 Common to provide a base implementation of the Hyper9 Common Bean, but no such base implementation exists. Why? Many popular Java frameworks, including Spring for example, require all beans known to them to extend their own bean base class. Since Java classes may only have one super class, it cannot be assumed that beans wanting to make use of a Hyper9 Common bean's methods may not already have their own super class.

That's okay, because Hyper9 Common provides BeanSerializer.

BeanSerializer

The Hyper9 Common BeanSerializer is a utility class which provides implementations of all of the Bean interface's methods. Classes that implement a Hyper9 Common Bean may use the BeanSerializer to provide the necessary method implementations required by the Bean interface.

UniqueBean

A UniqueBean is simply a Hyper9 Common bean that provides an ID property:

/*
 * Copyright (c) 2009, Hyper9 All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. Neither the name of Hyper9 nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission. THIS SOFTWARE IS PROVIDED
 * BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.hyper9.common.beans;

/**
 * This bean describes an object that can be uniquely identified by an
 * identifier.
 * 
 * @author akutz
 * @param <T> The type of the bean's identifier.
 * 
 */
public interface UniqueBean<T> extends Bean
{
    /**
     * Get this bean's unique identifier.
     * 
     * @return This bean's unique identifier.
     */
    public T getID();

    /**
     * Set this bean's unique identifier.
     * 
     * @param toSet This bean's unique identifier.
     */
    public void setID(T toSet);
}

Etc

The Hyper9 Common beans package also provides several other classes, such as:

  • BeanUtils - A utility class for working with beans.
  • BeanSerializationFormats - An enumeration that specifies which format to serialize a bean as.