src/core/net/sf/basedb/util/json/NameableConverter.java

Code
Comments
Other
Rev Date Author Line
6223 15 Jan 13 nicklas 1 /**
6223 15 Jan 13 nicklas 2   $Id $
6223 15 Jan 13 nicklas 3
6223 15 Jan 13 nicklas 4   Copyright (C) 2014 Nicklas Nordborg
6223 15 Jan 13 nicklas 5
6223 15 Jan 13 nicklas 6   This file is part of BASE - BioArray Software Environment.
6223 15 Jan 13 nicklas 7   Available at http://base.thep.lu.se/
6223 15 Jan 13 nicklas 8
6223 15 Jan 13 nicklas 9   BASE is free software; you can redistribute it and/or
6223 15 Jan 13 nicklas 10   modify it under the terms of the GNU General Public License
6223 15 Jan 13 nicklas 11   as published by the Free Software Foundation; either version 3
6223 15 Jan 13 nicklas 12   of the License, or (at your option) any later version.
6223 15 Jan 13 nicklas 13
6223 15 Jan 13 nicklas 14   BASE is distributed in the hope that it will be useful,
6223 15 Jan 13 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
6223 15 Jan 13 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6223 15 Jan 13 nicklas 17   GNU General Public License for more details.
6223 15 Jan 13 nicklas 18
6223 15 Jan 13 nicklas 19   You should have received a copy of the GNU General Public License
6223 15 Jan 13 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
6223 15 Jan 13 nicklas 21 */
6223 15 Jan 13 nicklas 22 package net.sf.basedb.util.json;
6223 15 Jan 13 nicklas 23
6223 15 Jan 13 nicklas 24 import org.json.simple.JSONObject;
6223 15 Jan 13 nicklas 25
6223 15 Jan 13 nicklas 26 import net.sf.basedb.core.Nameable;
6223 15 Jan 13 nicklas 27
6223 15 Jan 13 nicklas 28
6223 15 Jan 13 nicklas 29 /**
6223 15 Jan 13 nicklas 30   Simple "converter" implementation that create JSON objects
6223 15 Jan 13 nicklas 31   with the name and id of {@link Nameable} items. Subclasses
6497 26 Jun 14 nicklas 32   may implement the {@link #setMore(JSONObject, Nameable)} method
6223 15 Jan 13 nicklas 33   to add more properties to the JSON object.
6223 15 Jan 13 nicklas 34   
6223 15 Jan 13 nicklas 35   @author nicklas
6223 15 Jan 13 nicklas 36   @since 3.3
6223 15 Jan 13 nicklas 37 */
6223 15 Jan 13 nicklas 38 public class NameableConverter<T extends Nameable>
6223 15 Jan 13 nicklas 39   implements JsonConverter<T>
6223 15 Jan 13 nicklas 40 {
6223 15 Jan 13 nicklas 41
6223 15 Jan 13 nicklas 42   public NameableConverter()
6223 15 Jan 13 nicklas 43   {}
6223 15 Jan 13 nicklas 44   
6223 15 Jan 13 nicklas 45   /**
6223 15 Jan 13 nicklas 46     @return The JSON object
6223 15 Jan 13 nicklas 47   */
6223 15 Jan 13 nicklas 48   @Override
6223 15 Jan 13 nicklas 49   public Object convert(T object)
6223 15 Jan 13 nicklas 50   {
6223 15 Jan 13 nicklas 51     JSONObject json = new JSONObject();
6223 15 Jan 13 nicklas 52     json.put("id", object.getId());
6223 15 Jan 13 nicklas 53     json.put("name", object.getName());
6223 15 Jan 13 nicklas 54     setMore(json, object);
6223 15 Jan 13 nicklas 55     return json;
6223 15 Jan 13 nicklas 56   }
6223 15 Jan 13 nicklas 57   
6223 15 Jan 13 nicklas 58   /**
6223 15 Jan 13 nicklas 59     Can be used by subclasses to set more properties on the
6223 15 Jan 13 nicklas 60     JSON object. The default implementation does nothing.
6223 15 Jan 13 nicklas 61     @param json The JSON object that store the converted properties
6223 15 Jan 13 nicklas 62     @param object The object that is being converted
6223 15 Jan 13 nicklas 63   */
6223 15 Jan 13 nicklas 64   protected void setMore(JSONObject json, T object)
6223 15 Jan 13 nicklas 65   {}
6223 15 Jan 13 nicklas 66   
6223 15 Jan 13 nicklas 67 }