Skip to content

Commit

Permalink
Merge pull request #2 from Low012/master
Browse files Browse the repository at this point in the history
Fix for missing method in Android's JSON library
  • Loading branch information
sudheesh001 committed Jun 8, 2016
2 parents f34c8c8 + ec12b78 commit 8bf3f7f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/org/loklak/objects/MessageEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.json.JSONException;
import org.json.JSONObject;
import org.loklak.tools.JsonCompatHelper;

import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -458,11 +459,11 @@ public JSONObject toJSON(final UserEntry user, final boolean calculatedData, fin
if (this.provider_hash != null && this.provider_hash.length() > 0) json.put("provider_hash", this.provider_hash);
json.put("retweet_count", this.retweet_count);
json.put("favourites_count", this.favourites_count); // there is a slight inconsistency here in the plural naming but thats how it is noted in the twitter api
json.put("images", this.images);
JsonCompatHelper.put(json, "images", this.images);
json.put("images_count", this.images.size());
json.put("audio", this.audio);
JsonCompatHelper.put(json, "audio", this.audio);
json.put("audio_count", this.audio.size());
json.put("videos", this.videos);
JsonCompatHelper.put(json, "videos", this.videos);
json.put("videos_count", this.videos.size());
json.put("place_name", this.place_name);
json.put("place_id", this.place_id);
Expand Down
51 changes: 51 additions & 0 deletions src/org/loklak/tools/JsonCompatHelper.java
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));
}
}

}

0 comments on commit 8bf3f7f

Please sign in to comment.