Skip to content

Commit

Permalink
Optimize json parsing
Browse files Browse the repository at this point in the history
Micro optimizations in parsing
  • Loading branch information
AppearamidGuy committed May 14, 2023
1 parent 8f0f921 commit c8785d4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ protected Void doInBackground(Void... voids) {
JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
String name = data.getString(JSONUtils.DISPLAY_NAME_KEY);
String bannerImageUrl = data.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY);
if (bannerImageUrl.equals("") || bannerImageUrl.equals("null")) {
if (bannerImageUrl.isEmpty() || bannerImageUrl.equals("null")) {
bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY);
if (bannerImageUrl.equals("null")) {
bannerImageUrl = "";
}
}
String iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY);
if (iconUrl.equals("") || iconUrl.equals("null")) {
if (iconUrl.isEmpty() || iconUrl.equals("null")) {
iconUrl = data.getString(JSONUtils.ICON_IMG_KEY);
if (iconUrl.equals("null")) {
iconUrl = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static Post parseBasicData(JSONObject data) throws JSONException {
flair = postFlairHTMLBuilder.toString();
}

if (flair.equals("") && data.has(JSONUtils.LINK_FLAIR_TEXT_KEY) && !data.isNull(JSONUtils.LINK_FLAIR_TEXT_KEY)) {
if (flair.isEmpty() && !data.isNull(JSONUtils.LINK_FLAIR_TEXT_KEY)) {
flair = data.getString(JSONUtils.LINK_FLAIR_TEXT_KEY);
}

Expand Down Expand Up @@ -640,15 +640,8 @@ private static Post parseData(JSONObject data, String permalink, String id, Stri
}

JSONObject galleryItem = galleryIdsArray.getJSONObject(i);
String galleryItemCaption = "";
String galleryItemCaptionUrl = "";
if (galleryItem.has(JSONUtils.CAPTION_KEY)) {
galleryItemCaption = galleryItem.getString(JSONUtils.CAPTION_KEY).trim();
}

if (galleryItem.has(JSONUtils.CAPTION_URL_KEY)) {
galleryItemCaptionUrl = galleryItem.getString(JSONUtils.CAPTION_URL_KEY).trim();
}
String galleryItemCaption = galleryItem.optString(JSONUtils.CAPTION_KEY);
String galleryItemCaptionUrl = galleryItem.optString(JSONUtils.CAPTION_URL_KEY).trim();

if (previews.isEmpty() && (mimeType.contains("jpg") || mimeType.contains("png"))) {
previews.add(new Post.Preview(galleryItemUrl, singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY),
Expand Down Expand Up @@ -714,7 +707,7 @@ private static Post parseData(JSONObject data, String permalink, String id, Stri
if (selfTextPlain.length() > 250) {
selfTextPlain = selfTextPlain.substring(0, 250);
}
if (!selfText.equals("")) {
if (!selfText.isEmpty()) {
Pattern p = Pattern.compile(">!.+!<");
Matcher m = p.matcher(selfText.substring(0, Math.min(selfText.length(), 400)));
if (m.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static SubredditData parseSubredditData(JSONObject subredditDataJsonObje
} else {
bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY);
}
if (bannerImageUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.BANNER_IMG_KEY)) {
if (bannerImageUrl.isEmpty() && !subredditDataJsonObject.isNull(JSONUtils.BANNER_IMG_KEY)) {
bannerImageUrl = subredditDataJsonObject.getString(JSONUtils.BANNER_IMG_KEY);
}

Expand All @@ -51,7 +51,7 @@ private static SubredditData parseSubredditData(JSONObject subredditDataJsonObje
} else {
iconUrl = subredditDataJsonObject.getString(JSONUtils.COMMUNITY_ICON_KEY);
}
if (iconUrl.equals("") && !subredditDataJsonObject.isNull(JSONUtils.ICON_IMG_KEY)) {
if (iconUrl.isEmpty() && !subredditDataJsonObject.isNull(JSONUtils.ICON_IMG_KEY)) {
iconUrl = subredditDataJsonObject.getString(JSONUtils.ICON_IMG_KEY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static UserData parseUserDataBase(JSONObject userDataJson, boolean parse
String iconImageUrl = userDataJson.getString(JSONUtils.ICON_IMG_KEY);
String bannerImageUrl = "";
boolean canBeFollowed;
if (userDataJson.has(JSONUtils.SUBREDDIT_KEY) && !userDataJson.isNull(JSONUtils.SUBREDDIT_KEY)) {
if (!userDataJson.isNull(JSONUtils.SUBREDDIT_KEY)) {
bannerImageUrl = userDataJson.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.BANNER_IMG_KEY);
canBeFollowed = true;
} else {
Expand Down

0 comments on commit c8785d4

Please sign in to comment.