Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

In this page you can find the example usage for org.json JSONObject put.

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

public static DbObject from(Context context, Uri videoUri) throws IOException {
    // Query gallery for camera picture via
    // Android ContentResolver interface
    ContentResolver cr = context.getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;//from  w  w w  .  j  av a  2 s  . c  o  m
    long videoId = Long.parseLong(videoUri.getLastPathSegment());
    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(cr, videoId,
            MediaStore.Video.Thumbnails.MINI_KIND, options);
    int targetSize = 200;
    int width = curThumb.getWidth();
    int height = curThumb.getHeight();
    int cropSize = Math.min(width, height);
    float scaleSize = ((float) targetSize) / cropSize;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleSize, scaleSize);
    curThumb = Bitmap.createBitmap(curThumb, 0, 0, width, height, matrix, true);
    JSONObject base = new JSONObject();
    String localIp = ContentCorral.getLocalIpAddress();
    if (localIp != null) {
        try {
            // TODO: Security breach hack?
            base.put(Contact.ATTR_LAN_IP, localIp);
            base.put(LOCAL_URI, videoUri.toString());
            base.put(MIME_TYPE, cr.getType(videoUri));
        } catch (JSONException e) {
            Log.e(TAG, "impossible json error possible!");
        }
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    curThumb.compress(Bitmap.CompressFormat.JPEG, 90, baos);
    byte[] data = baos.toByteArray();
    return from(base, data);
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

public JSONObject mergeRaw(JSONObject objData, byte[] raw) {
    try {/*ww w .  j a  va2s . com*/
        if (raw != null)
            objData = objData.put(DATA, Base64.encodeToString(raw, false));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return objData;
}

From source file:org.stockchart.series.LinearSeries.java

@Override
public JSONObject toJSONObject() throws JSONException {
    JSONObject j = super.toJSONObject();
    j.put("pointSize", fPointSizeInPercents);
    j.put("pointStyle", fPointStyle);
    j.put("pointsVisible", fPointsVisible);
    j.put("pointAppearance", fPointAppearance.toJSONObject());

    return j;//from  ww w  .  j  a  v  a2s . c  o m
}

From source file:utils.tour.Step.java

/**
 * Get the step as a json object.//from   ww w.  ja va 2s .c  o  m
 * 
 * @param uid
 *            the tour uid
 * @param index
 *            the index of the step
 * @param previous
 *            the previous step
 * @param next
 *            the next step
 */
public JSONObject render(String uid, Integer index, Step previous, Step next) {

    JSONObject json = new JSONObject();

    try {
        json.put("title", this.title);
        json.put("content", this.content);
        if (this.element != null) {
            json.put("element", this.element);
            json.put("placement", this.placement);
        } else {
            json.put("orphan", true);
        }
        if (this.route != null) {
            json.put("path", this.route);
        }
        if (previous != null && previous.getRoute() != null && this.route != null
                && !previous.getRoute().equals(this.route)) {
            json.put("onPrev", "function(){ document.location.href = '" + previous.getRoute() + "?tourStep="
                    + (index - 1) + "&tourUid=" + uid + "'; return (new jQuery.Deferred()).promise(); }");
        }
        if (next != null && next.getRoute() != null && this.route != null
                && !next.getRoute().equals(this.route)) {
            json.put("onNext", "function(){ document.location.href = '" + next.getRoute() + "?tourStep="
                    + (index + 1) + "&tourUid=" + uid + "'; return (new jQuery.Deferred()).promise(); }");
        }
        return json;
    } catch (JSONException e) {
        Logger.error("error when render a step tour", e);
    }

    return null;
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills archive dates./*from   w ww.j a v a 2 s .c o m*/
 *
 * @param dataModel data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillArchiveDates(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Archive Dates");

    try {
        LOGGER.finer("Filling archive dates....");
        final List<JSONObject> archiveDates = archiveDateRepository.getArchiveDates();

        final String localeString = preference.getString(Preference.LOCALE_STRING);
        final String language = Locales.getLanguage(localeString);

        for (final JSONObject archiveDate : archiveDates) {
            final long time = archiveDate.getLong(ArchiveDate.ARCHIVE_TIME);
            final String dateString = ArchiveDate.DATE_FORMAT.format(time);
            final String[] dateStrings = dateString.split("/");
            final String year = dateStrings[0];
            final String month = dateStrings[1];
            archiveDate.put(ArchiveDate.ARCHIVE_DATE_YEAR, year);

            archiveDate.put(ArchiveDate.ARCHIVE_DATE_MONTH, month);
            if ("en".equals(language)) {
                final String monthName = Dates.EN_MONTHS.get(month);
                archiveDate.put(Common.MONTH_NAME, monthName);
            }
        }

        dataModel.put(ArchiveDate.ARCHIVE_DATES, archiveDates);
    } catch (final JSONException e) {
        LOGGER.log(Level.SEVERE, "Fills archive dates failed", e);
        throw new ServiceException(e);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, "Fills archive dates failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills post comments recently./*from   w  w w.  j av a2s  . c  o  m*/
 *
 * @param dataModel data model
 * @param preference the specified preference
 * @throws ServiceException service exception
 */
public void fillRecentComments(final Map<String, Object> dataModel, final JSONObject preference)
        throws ServiceException {
    Stopwatchs.start("Fill Recent Comments");
    try {
        LOGGER.finer("Filling recent comments....");
        final int recentCommentDisplayCnt = preference.getInt(Preference.RECENT_COMMENT_DISPLAY_CNT);

        final List<JSONObject> recentComments = commentRepository.getRecentComments(recentCommentDisplayCnt);

        for (final JSONObject comment : recentComments) {
            final String content = comment.getString(Comment.COMMENT_CONTENT)
                    .replaceAll(SoloServletListener.ENTER_ESC, "&nbsp;");
            comment.put(Comment.COMMENT_CONTENT, content);
            comment.put(Comment.COMMENT_NAME,
                    StringEscapeUtils.escapeHtml(comment.getString(Comment.COMMENT_NAME)));
            comment.put(Comment.COMMENT_URL,
                    StringEscapeUtils.escapeHtml(comment.getString(Comment.COMMENT_URL)));

            comment.remove(Comment.COMMENT_EMAIL); // Erases email for security reason
        }

        dataModel.put(Common.RECENT_COMMENTS, recentComments);

    } catch (final JSONException e) {
        LOGGER.log(Level.SEVERE, "Fills recent comments failed", e);
        throw new ServiceException(e);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, "Fills recent comments failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Fills page navigations.//ww w . j  a v a  2s.co  m
 *
 * @param dataModel data model
 * @throws ServiceException service exception
 */
private void fillPageNavigations(final Map<String, Object> dataModel) throws ServiceException {
    Stopwatchs.start("Fill Navigations");
    try {
        LOGGER.finer("Filling page navigations....");
        final List<JSONObject> pages = pageRepository.getPages();

        for (final JSONObject page : pages) {
            if ("page".equals(page.optString(Page.PAGE_TYPE))) {
                final String permalink = page.optString(Page.PAGE_PERMALINK);
                page.put(Page.PAGE_PERMALINK, Latkes.getServePath() + permalink);
            }
        }

        dataModel.put(Common.PAGE_NAVIGATIONS, pages);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.SEVERE, "Fills page navigations failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Sets some extra properties into the specified article with the specified author and preference, performs content and 
 * abstract editor processing./*from w w w  . ja  va 2 s.c om*/
 * 
 * <p>
 * Article ext properties:
 * <pre>
 * {
 *     ...., 
 *     "authorName": "",
 *     "authorId": "",
 *     "hasUpdated": boolean
 * }
 * </pre>
 * </p>
 * 
 * @param article the specified article
 * @param author the specified author
 * @param preference the specified preference
 * @throws ServiceException service exception
 * @see #setArticlesExProperties(java.util.List, org.json.JSONObject) 
 */
private void setArticleExProperties(final JSONObject article, final JSONObject author,
        final JSONObject preference) throws ServiceException {
    try {
        final String authorName = author.getString(User.USER_NAME);
        article.put(Common.AUTHOR_NAME, authorName);
        final String authorId = author.getString(Keys.OBJECT_ID);
        article.put(Common.AUTHOR_ID, authorId);

        if (preference.getBoolean(Preference.ENABLE_ARTICLE_UPDATE_HINT)) {
            article.put(Common.HAS_UPDATED, articleUtils.hasUpdated(article));
        } else {
            article.put(Common.HAS_UPDATED, false);
        }

        processArticleAbstract(preference, article);

        articleQueryService.markdown(article);
    } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "Sets article extra properties failed", e);
        throw new ServiceException(e);
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Sets some extra properties into the specified article with the specified preference, performs content and 
 * abstract editor processing./*from   www . java 2 s  . c o m*/
 * 
 * <p>
 * Article ext properties:
 * <pre>
 * {
 *     ...., 
 *     "authorName": "",
 *     "authorId": "",
 *     "hasUpdated": boolean
 * }
 * </pre>
 * </p>
 * 
 * @param article the specified article
 * @param preference the specified preference
 * @throws ServiceException service exception
 * @see #setArticlesExProperties(java.util.List, org.json.JSONObject) 
 */
private void setArticleExProperties(final JSONObject article, final JSONObject preference)
        throws ServiceException {
    try {
        final JSONObject author = articleUtils.getAuthor(article);
        final String authorName = author.getString(User.USER_NAME);
        article.put(Common.AUTHOR_NAME, authorName);
        final String authorId = author.getString(Keys.OBJECT_ID);
        article.put(Common.AUTHOR_ID, authorId);

        if (preference.getBoolean(Preference.ENABLE_ARTICLE_UPDATE_HINT)) {
            article.put(Common.HAS_UPDATED, articleUtils.hasUpdated(article));
        } else {
            article.put(Common.HAS_UPDATED, false);
        }

        processArticleAbstract(preference, article);

        articleQueryService.markdown(article);
    } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "Sets article extra properties failed", e);
        throw new ServiceException(e);
    }
}

From source file:org.b3log.solo.processor.util.Filler.java

/**
 * Processes the abstract of the specified article with the specified preference.
 * /*from   ww  w  .j  av  a2 s .com*/
 * <p>
 *   <ul>
 *     <li>If the abstract is {@code null}, sets it with ""</li>
 *     <li>If user configured preference "titleOnly", sets the abstract with ""</li>
 *     <li>If user configured preference "titleAndContent", sets the abstract with the content of the article</li>
 *   </ul>
 * </p>
 * 
 * @param preference the specified preference
 * @param article the specified article
 */
private void processArticleAbstract(final JSONObject preference, final JSONObject article) {
    final String articleAbstract = article.optString(Article.ARTICLE_ABSTRACT, null);
    if (null == articleAbstract) {
        article.put(Article.ARTICLE_ABSTRACT, "");
    }

    final String articleListStyle = preference.optString(Preference.ARTICLE_LIST_STYLE);
    if ("titleOnly".equals(articleListStyle)) {
        article.put(Article.ARTICLE_ABSTRACT, "");
    } else if ("titleAndContent".equals(articleListStyle)) {
        article.put(Article.ARTICLE_ABSTRACT, article.optString(Article.ARTICLE_CONTENT));
    }
}