6153 |
28 Sep 12 |
nicklas |
1 |
/** |
6153 |
28 Sep 12 |
nicklas |
$Id $ |
6153 |
28 Sep 12 |
nicklas |
3 |
|
6153 |
28 Sep 12 |
nicklas |
Copyright (C) 2012 Nicklas Nordborg |
6153 |
28 Sep 12 |
nicklas |
5 |
|
6153 |
28 Sep 12 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6153 |
28 Sep 12 |
nicklas |
Available at http://base.thep.lu.se/ |
6153 |
28 Sep 12 |
nicklas |
8 |
|
6153 |
28 Sep 12 |
nicklas |
BASE is free software; you can redistribute it and/or |
6153 |
28 Sep 12 |
nicklas |
modify it under the terms of the GNU General Public License |
6153 |
28 Sep 12 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6153 |
28 Sep 12 |
nicklas |
of the License, or (at your option) any later version. |
6153 |
28 Sep 12 |
nicklas |
13 |
|
6153 |
28 Sep 12 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6153 |
28 Sep 12 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6153 |
28 Sep 12 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6153 |
28 Sep 12 |
nicklas |
GNU General Public License for more details. |
6153 |
28 Sep 12 |
nicklas |
18 |
|
6153 |
28 Sep 12 |
nicklas |
You should have received a copy of the GNU General Public License |
6153 |
28 Sep 12 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6153 |
28 Sep 12 |
nicklas |
21 |
*/ |
6153 |
28 Sep 12 |
nicklas |
22 |
package net.sf.basedb.util.json; |
6153 |
28 Sep 12 |
nicklas |
23 |
|
6153 |
28 Sep 12 |
nicklas |
24 |
import java.util.Collection; |
6223 |
15 Jan 13 |
nicklas |
25 |
import java.util.Iterator; |
6153 |
28 Sep 12 |
nicklas |
26 |
|
6153 |
28 Sep 12 |
nicklas |
27 |
import org.json.simple.JSONArray; |
6153 |
28 Sep 12 |
nicklas |
28 |
|
6153 |
28 Sep 12 |
nicklas |
29 |
/** |
6153 |
28 Sep 12 |
nicklas |
Utility functions for working with JSON data. |
6153 |
28 Sep 12 |
nicklas |
@author nicklas |
6153 |
28 Sep 12 |
nicklas |
@since 3.3 |
6153 |
28 Sep 12 |
nicklas |
33 |
*/ |
6153 |
28 Sep 12 |
nicklas |
34 |
public class JsonUtil |
6153 |
28 Sep 12 |
nicklas |
35 |
{ |
6153 |
28 Sep 12 |
nicklas |
36 |
|
6153 |
28 Sep 12 |
nicklas |
37 |
/** |
6153 |
28 Sep 12 |
nicklas |
Convert a collection of objects to an array with JSON-compatible |
6153 |
28 Sep 12 |
nicklas |
objects. The converter may be null if the objects in the collection |
6153 |
28 Sep 12 |
nicklas |
are already JSON-compatible. |
6153 |
28 Sep 12 |
nicklas |
41 |
|
6153 |
28 Sep 12 |
nicklas |
@param objects The collection of objects to convert and place in the array |
6153 |
28 Sep 12 |
nicklas |
@param converter An optional converter if needed |
6153 |
28 Sep 12 |
nicklas |
@return A JSON array with the converted objects, if the collection is null |
6153 |
28 Sep 12 |
nicklas |
or empty an empty array is returned |
6153 |
28 Sep 12 |
nicklas |
46 |
*/ |
6153 |
28 Sep 12 |
nicklas |
47 |
public static <T> JSONArray toArray(Collection<T> objects, JsonConverter<? super T> converter) |
6153 |
28 Sep 12 |
nicklas |
48 |
{ |
6223 |
15 Jan 13 |
nicklas |
49 |
return toArray(objects != null ? objects.iterator() : null, converter); |
6223 |
15 Jan 13 |
nicklas |
50 |
} |
6223 |
15 Jan 13 |
nicklas |
51 |
|
6223 |
15 Jan 13 |
nicklas |
52 |
/** |
6223 |
15 Jan 13 |
nicklas |
Convert all objects returned by the iterator to an array with JSON-compatible |
6223 |
15 Jan 13 |
nicklas |
objects. The converter may be null if the objects in the collection |
6223 |
15 Jan 13 |
nicklas |
are already JSON-compatible. |
6223 |
15 Jan 13 |
nicklas |
56 |
|
6497 |
26 Jun 14 |
nicklas |
@param it An iterator returning the objects to convert and place in the array |
6223 |
15 Jan 13 |
nicklas |
@param converter An optional converter if needed |
6223 |
15 Jan 13 |
nicklas |
@return A JSON array with the converted objects, if the iterator is null |
6223 |
15 Jan 13 |
nicklas |
or empty an empty array is returned |
6223 |
15 Jan 13 |
nicklas |
61 |
*/ |
6223 |
15 Jan 13 |
nicklas |
62 |
public static <T> JSONArray toArray(Iterator<T> it, JsonConverter<? super T> converter) |
6223 |
15 Jan 13 |
nicklas |
63 |
{ |
6153 |
28 Sep 12 |
nicklas |
64 |
JSONArray json = new JSONArray(); |
6223 |
15 Jan 13 |
nicklas |
65 |
if (it != null) |
6153 |
28 Sep 12 |
nicklas |
66 |
{ |
6153 |
28 Sep 12 |
nicklas |
67 |
if (converter == null) converter = new IdentityConverter<T>(); |
6223 |
15 Jan 13 |
nicklas |
68 |
while (it.hasNext()) |
6153 |
28 Sep 12 |
nicklas |
69 |
{ |
7501 |
09 Aug 18 |
nicklas |
70 |
Object t = converter.convert(it.next()); |
7501 |
09 Aug 18 |
nicklas |
71 |
if (t != null) json.add(t); |
6153 |
28 Sep 12 |
nicklas |
72 |
} |
6153 |
28 Sep 12 |
nicklas |
73 |
} |
6153 |
28 Sep 12 |
nicklas |
74 |
return json; |
6153 |
28 Sep 12 |
nicklas |
75 |
} |
6153 |
28 Sep 12 |
nicklas |
76 |
|
6153 |
28 Sep 12 |
nicklas |
77 |
} |