Package de.netseeker.ejoe.adapter.json

Source Code of de.netseeker.ejoe.adapter.json.JsonLibAdapter

/*********************************************************************
* JsonLibAdapter.java
* created on 05.11.2006 by netseeker
* $Id: JsonLibAdapter.java,v 1.4 2007/11/17 10:59:17 netseeker Exp $
* $Log: JsonLibAdapter.java,v $
* Revision 1.4  2007/11/17 10:59:17  netseeker
* *** empty log message ***
*
* Revision 1.3  2007/03/25 15:03:23  netseeker
* *** empty log message ***
*
* Revision 1.2  2007/03/22 21:01:34  netseeker
* *** empty log message ***
*
* Revision 1.1  2007/02/11 15:42:20  netseeker
* *** empty log message ***
*
* Revision 1.3  2006/11/06 11:02:44  netseeker
* *** empty log message ***
*
* Revision 1.2  2006/11/05 17:13:09  netseeker
* added source documentation
*
* Revision 1.1  2006/11/05 16:33:37  netseeker
* changed adapter connection handling
* added support for JSON with different JSON-adapters (XStream,JSON-lib,MyJSON)
*
*
* ====================================================================
*
*  Copyright 2005-2006 netseeker aka Michael Manske
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.adapter.json;

import java.io.InputStream;
import java.io.OutputStream;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.util.JSONUtils;
import de.netseeker.ejoe.adapter.UTF8StringAdapter;

/**
* Multimode SerializeAdapter supporting JSON via the JSON-lib. By default this adapter is write-only: It is able to
* serialize Objects to JSON but does just return JSON strings on read operations. The read mode can be switched but
* then Objects according to what JSON-lib creates will be returned.
*
* @author netseeker
* @since 0.3.9.1
* @see <a href="http://json-lib.sourceforge.net/">JSON-lib</a>
*/
public class JsonLibAdapter extends UTF8StringAdapter
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private boolean           _writeOnly;

    /**
     * Creates a write-only instance of this adapter. It will write Object instances to JSON but will just return JSON
     * string representations on read operations.
     */
    public JsonLibAdapter()
    {
        this( true );
    }

    /**
     * Creates either a write-only or a mixed-mode instance of this adapter. In write-only mode it will write Object
     * instances to JSON but will just return JSON string representations on read operations. In mixed-mode Objects
     * according to what JSON-lib creates will be returned on read operations.
     *
     * @param writeOnly whether to uses write-only mode or not
     */
    public JsonLibAdapter(boolean writeOnly)
    {
        _writeOnly = writeOnly;
    }

    /*
     * (non-Javadoc)
     *
     * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
     */
    public Object read( InputStream in ) throws Exception
    {
        String json = (String) super.read( in );
        if ( _writeOnly )
        {
            return json;
        }
        else
        {
            Object result = null;

            if ( json != null )
            {
                JSONObject jsonObject = JSONObject.fromObject( json );

                if ( !jsonObject.isNullObject() )
                {
                    if ( jsonObject.has( "uniqueName" ) )
                    {
                        result = JSONObject.toBean( jsonObject, Class.forName( jsonObject.getString( "uniqueName" ) ) );
                    }
                    else
                    {
                        result = JSONSerializer.toJava( jsonObject );
                    }
                }
            }

            return result;
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
     */
    public void write( Object obj, OutputStream out ) throws Exception
    {
        JSON json = null;
       
        if( JSONUtils.isObject( obj ))
        {
            json = JSONObject.fromObject( obj );           
        }
        else
        {
           json = JSONSerializer.toJSON( obj );
        }
       
        super.write( json, out );
    }

    /*
     * (non-Javadoc)
     *
     * @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
     */
    public String getContentType()
    {
        return "application/json";
    }
}
TOP

Related Classes of de.netseeker.ejoe.adapter.json.JsonLibAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.