6223 |
15 Jan 13 |
nicklas |
1 |
/** |
6223 |
15 Jan 13 |
nicklas |
$Id $ |
6223 |
15 Jan 13 |
nicklas |
3 |
|
6223 |
15 Jan 13 |
nicklas |
Copyright (C) 2014 Nicklas Nordborg |
6223 |
15 Jan 13 |
nicklas |
5 |
|
6223 |
15 Jan 13 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6223 |
15 Jan 13 |
nicklas |
Available at http://base.thep.lu.se/ |
6223 |
15 Jan 13 |
nicklas |
8 |
|
6223 |
15 Jan 13 |
nicklas |
BASE is free software; you can redistribute it and/or |
6223 |
15 Jan 13 |
nicklas |
modify it under the terms of the GNU General Public License |
6223 |
15 Jan 13 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6223 |
15 Jan 13 |
nicklas |
of the License, or (at your option) any later version. |
6223 |
15 Jan 13 |
nicklas |
13 |
|
6223 |
15 Jan 13 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6223 |
15 Jan 13 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6223 |
15 Jan 13 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6223 |
15 Jan 13 |
nicklas |
GNU General Public License for more details. |
6223 |
15 Jan 13 |
nicklas |
18 |
|
6223 |
15 Jan 13 |
nicklas |
You should have received a copy of the GNU General Public License |
6223 |
15 Jan 13 |
nicklas |
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 |
Simple "converter" implementation that create JSON objects |
6223 |
15 Jan 13 |
nicklas |
with the name and id of {@link Nameable} items. Subclasses |
6497 |
26 Jun 14 |
nicklas |
may implement the {@link #setMore(JSONObject, Nameable)} method |
6223 |
15 Jan 13 |
nicklas |
to add more properties to the JSON object. |
6223 |
15 Jan 13 |
nicklas |
34 |
|
6223 |
15 Jan 13 |
nicklas |
@author nicklas |
6223 |
15 Jan 13 |
nicklas |
@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 |
@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 |
Can be used by subclasses to set more properties on the |
6223 |
15 Jan 13 |
nicklas |
JSON object. The default implementation does nothing. |
6223 |
15 Jan 13 |
nicklas |
@param json The JSON object that store the converted properties |
6223 |
15 Jan 13 |
nicklas |
@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 |
} |