-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Low012/master
Fix for missing method in Android's JSON library
- Loading branch information
Showing
2 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* JsonCompatHelper | ||
* Copyright 06.06.2016 by Marc Nause, @nause_marc | ||
* | ||
* This library 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. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; wo 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 program in the file lgpl21.txt | ||
* If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.loklak.tools; | ||
|
||
import java.util.Collection; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* Provides workaround methods for different JSONObject implementations in the | ||
* original json.org library and the code include in Android. | ||
*/ | ||
public class JsonCompatHelper { | ||
|
||
private static boolean hasCollectionMethod; | ||
static { | ||
try { | ||
hasCollectionMethod = JSONObject.class.getMethod("put", new Class[] { String.class, Collection.class }) != null; | ||
} catch (NoSuchMethodException | SecurityException e) { | ||
hasCollectionMethod = false; | ||
} | ||
} | ||
|
||
public static JSONObject put(final JSONObject jsonObject, final String key, final Collection<?> value) { | ||
|
||
if (hasCollectionMethod) { | ||
return jsonObject.put(key, value); | ||
} else { | ||
return jsonObject.put(key, new JSONArray(value)); | ||
} | ||
} | ||
|
||
} |