All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src-main.org.awakefw.sql.json.ConnectionHolderTransport Maven / Gradle / Ivy

/*
 * Awake File: Easy file upload & download through HTTP with Java
 * Awake SQL: Remote JDBC access through HTTP.                                    
 * Copyright (C) 2012, Kawan Softwares S.A.S.
 * (http://www.awakeframework.org). All rights reserved.                                
 *                                                                               
 * Awake File/SQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * Awake File/SQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */

//Last Updates: 
// 7 mars 2011 16:34:17 Nicolas de Pomereu
// 16/03/12 14:50 NDP : ConnectionHolderTransport: free connectionHolder after toJson conv

package org.awakefw.sql.json;

import java.lang.reflect.Type;

import org.awakefw.file.api.util.AwakeDebug;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ConnectionHolderTransport {
    /** Debug flag */
    private static boolean DEBUG = AwakeDebug
	    .isSet(ConnectionHolderTransport.class);

    /**
     * Convert to Json a List of ConnectionHolder
     * 
     * @return the instance converted to Json
     */
    public static String toJson(ConnectionHolder connectionHolder) {
	// See http://sites.google.com/site/gson/gson-user-guide
	Gson gson = new Gson();
	Type theType = new TypeToken() {
	}.getType();
	String jsonString = gson.toJson(connectionHolder, theType);

	// free connectionHolder
	connectionHolder = null;
	
	debug("PC parameterTypesAndValues jsonString: " + jsonString);
	return jsonString;
    }

    /**
     * Format from JSON string the list of StatementHolder
     * 
     * @param jsonString
     *            formated JSON string containing the List of StatementHolder
     * @return List of StatementHolder
     */
    public static ConnectionHolder fromJson(String jsonString) {
	if (jsonString == null) {
	    throw new IllegalArgumentException("jsonString is null!");
	}

	debug("jsonString: " + jsonString);

	// See http://sites.google.com/site/gson/gson-user-guide
	Gson gson = new Gson();
	Type theType = new TypeToken() {
	}.getType();
	ConnectionHolder connectionHolder = gson.fromJson(jsonString, theType);
	return connectionHolder;
    }

    /**
     * Debug
     * 
     * @param s
     */
    public static void debug(String s) {
	if (DEBUG) {
	    System.out.println(s);
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy