Java Code Examples for org.json.JSONStringer#toString()

The following examples show how to use org.json.JSONStringer#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AcceptSDKParser.java    From accept-sdk-android with MIT License 6 votes vote down vote up
/**
 * Method create Json Object for Encryption API call. Using this method order of insertion is
 * preserved.
 *
 * @param transactionObject encryption transaction Object. Also see {@link
 * EncryptTransactionObject}
 * @return String json String
 * @throws JSONException, exception will be thrown when creation of Json fails.
 */
public static String getOrderedJsonFromEncryptTransaction(
    EncryptTransactionObject transactionObject) throws JSONException {

  // Json related to token section
  CardData cardData = transactionObject.getCardData();

  JSONStringer stringer = new JSONStringer().object();
  stringer.key(CONTAINER_REQUEST).object();
  prepareJsonForAuthenticationSection(stringer, transactionObject);
  stringer.key(CLIENT_ID).value(CLIENT_ID_VALUE);
  stringer.key(DATA).object(); //Data section
  stringer.key(TYPE).value(TYPE_VALUE_TOKEN);
  stringer.key(ID).value(transactionObject.getGuid());
  prepareJsonForTokenSection(stringer, cardData);
  stringer.endObject();
  stringer.endObject();
  stringer.endObject();

  LogUtil.log(LOG_LEVEL.INFO, "getJsonFromEncryptTransaction : " + stringer.toString());
  return stringer.toString();
}
 
Example 2
Source File: Utils.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * serialize String keys and String values in JSON format, sort keys
 * according to order in sortedList
 * 
 * @param map
 * @return
 */
public static String getJSON(List<String> sortedList, Map<String, Object> map) throws Exception
{
	try
	{
		JSONStringer js = new JSONStringer();
		js.object();
		if (map != null)
		{
			for (String key : sortedList)
			{
				js.key(key);
				js.value(map.get(key));
			}
		}
		js.endObject();
		return js.toString();
	}
	catch (JSONException e)
	{
		throw new Exception("cannot produce JSON", e);
	}
}
 
Example 3
Source File: Utils.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * serialize String values in JSON format
 * 
 * @param array
 * @return
 */
public static String getJSON(String[] array) throws Exception
{
	try
	{
		JSONStringer js = new JSONStringer();
		js.array();
		if (array != null)
		{
			for (String value : array)
			{
				js.value(value);
			}
		}
		js.endArray();
		return js.toString();
	}
	catch (JSONException e)
	{
		throw new Exception("cannot produce JSON", e);
	}
}
 
Example 4
Source File: Evaluation.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Override
public String toJSONString() {
  try {
    JSONStringer stringer =
        new JSONStringer().object().key(STATUS_KEY).value(status).key(VALUE_KEY);
    if ((value instanceof String)
        || (value instanceof Number)
        || (value instanceof Boolean)
        || (value == null)) {
      stringer.value(value);
    } else {
      String jsonValue = ModelCodec.encode(value);
      stringer.value(new JSONTokener(jsonValue).nextValue());
    }
    stringer.endObject();
    return stringer.toString();
  } catch (JSONException je) {
    throw new RuntimeException(je);
  }
}
 
Example 5
Source File: Util.java    From AutoInteraction-Library with Apache License 2.0 5 votes vote down vote up
/**
 * @param key
 * @param value
 * @return
 * @throws
 * @Title: createJsonString
 * @Description: 生成json字符串
 * @return: String
 */
public static String createJsonString(String key, JSONArray value) {
    JSONStringer jsonStringer = new JSONStringer();
    try {
        jsonStringer.object().key(key).value(value).endObject();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonStringer.toString();
}
 
Example 6
Source File: OwcVersionController.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@RequestMapping(value = "/dhusversion", method = RequestMethod.GET)
public String getVersion() throws JSONException
{                
   final String version = DHuS.class.getPackage().getImplementationVersion();
   JSONStringer jstring = new JSONStringer();
   jstring.object().key ("value").value (
      (version==null? "Development Version" : version )).endObject ();
   return jstring.toString ();
}
 
Example 7
Source File: StubVersionController.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@RequestMapping(value = "/version", method = RequestMethod.GET)
public String getVersion() throws JSONException
{                
   final String version = DHuS.class.getPackage().getImplementationVersion();
   JSONStringer jstring = new JSONStringer();
   jstring.object().key ("value").value (
      (version==null? "Development Version" : version )).endObject ();
   return jstring.toString ();
}
 
Example 8
Source File: PackageInstallerCompatV16.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
private static String infoToJson(PackageInstallInfo info) {
    String value = null;
    try {
        JSONStringer json = new JSONStringer()
                .object()
                .key(KEY_STATE).value(info.state)
                .key(KEY_PROGRESS).value(info.progress)
                .endObject();
        value = json.toString();
    } catch (JSONException e) {
        Log.e(TAG, "failed to serialize app state update", e);
    }
    return value;
}
 
Example 9
Source File: Settings.java    From onedev with MIT License 4 votes vote down vote up
public CharSequence toJson() {
	try {
		JSONStringer writer = new JSONStringer();
		writer.object();
		Json.writeObject(writer, "minimumInputLength", minimumInputLength);
		Json.writeObject(writer, "minimumResultsForSearch", minimumResultsForSearch);
		Json.writeObject(writer, "maximumSelectionSize", maximumSelectionSize);
		Json.writeObject(writer, "placeholder", placeholder);
		Json.writeObject(writer, "allowClear", allowClear);
		Json.writeObject(writer, "multiple", multiple);
		Json.writeObject(writer, "closeOnSelect", closeOnSelect);
		Json.writeFunction(writer, "id", id);
		Json.writeFunction(writer, "matcher", matcher);
		Json.writeFunction(writer, "tokenizer", tokenizer);
		Json.writeFunction(writer, "sortResults", sortResults);
		Json.writeFunction(writer, "formatSelection", formatSelection);
		Json.writeFunction(writer, "formatResult", formatResult);
		Json.writeFunction(writer, "formatNoMatches", formatNoMatches);
		Json.writeFunction(writer, "formatInputTooShort", formatInputTooShort);
		Json.writeFunction(writer, "formatResultCssClass", formatResultCssClass);
		Json.writeFunction(writer, "formatSelectionTooBig", formatSelectionTooBig);
		Json.writeFunction(writer, "formatLoadMore", formatLoadMore);
		Json.writeFunction(writer, "formatSearching", formatSearching);
		Json.writeFunction(writer, "escapeMarkup", escapeMarkup);
		Json.writeFunction(writer, "createSearchChoice", createSearchChoice);
		Json.writeFunction(writer, "initSelection", initSelection);
		Json.writeFunction(writer, "query", query);
		Json.writeObject(writer, "width", width);
		Json.writeObject(writer, "openOnEnter", openOnEnter);
		Json.writeFunction(writer, "containerCss", containerCss);
		Json.writeObject(writer, "containerCssClass", containerCssClass);
		Json.writeFunction(writer, "dropdownCss", dropdownCss);
		Json.writeObject(writer, "dropdownCssClass", dropdownCssClass);
		Json.writeObject(writer, "separator", separator);
		Json.writeObject(writer, "tokenSeparators", tokenSeparators);
		Json.writeObject(writer, "dropdownAutoWidth", dropdownAutoWidth);
		if (ajax != null) {
			writer.key("ajax");
			ajax.toJson(writer);
		}
		Json.writeFunction(writer, "data", data);
		Json.writeFunction(writer, "tags", tags);
		writer.endObject();

		return writer.toString();
	} catch (JSONException e) {
		throw new RuntimeException("Could not convert Select2 settings object to Json", e);
	}
}
 
Example 10
Source File: SqlDBOperate.java    From FlyWoo with Apache License 2.0 4 votes vote down vote up
public String sendUserInfoToJSON() {
    List<UserInfo> users;
    int count = (int) getCountOfUserInfo();
    users = getScrollDataOfUserInfo(0, count);
    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("user");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (UserInfo user : users) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(user.getId());
            jsonText.key("name");
            jsonText.value(user.getName());
            jsonText.key("sex");
            jsonText.value(user.getSex());
            jsonText.key("age");
            jsonText.value(user.getAge());
            jsonText.key("IMEI");
            jsonText.value(user.getIMEI());
            jsonText.key("ip");
            jsonText.value(user.getIPAddr());
            jsonText.key("status");
            jsonText.value(user.getIsOnline());
            jsonText.key("avater");
            jsonText.value(user.getAvater());
            jsonText.key("lastdate");
            jsonText.value(user.getLastDate());
            jsonText.key("device");
            jsonText.value(user.getDevice());
            jsonText.key("constellation");
            jsonText.value(user.getConstellation());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}
 
Example 11
Source File: SqlDBOperate.java    From FlyWoo with Apache License 2.0 4 votes vote down vote up
public String sendChattingInfoToJSON(int sendID, int receiverID) {
    List<ChattingInfo> infos;

    infos = getAllMessageFromChattingInfo(sendID, receiverID);

    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("chatting");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (ChattingInfo info : infos) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(info.getId());
            jsonText.key("sendID");
            jsonText.value(info.getSendID());
            jsonText.key("receiverID");
            jsonText.value(info.getReceiverID());
            jsonText.key("chatting");
            jsonText.value(info.getInfo());
            jsonText.key("date");
            jsonText.value(info.getDate());
            jsonText.key("style");
            jsonText.value(info.getStyle());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}
 
Example 12
Source File: JsonRepresentation.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
     * Returns the JSON text for the wrapped JSON object or representation.
     * 
     * @return The JSON text.
     * @throws JSONException
     */
    private String getJsonText() throws JSONException {
        String result = null;

        if (this.jsonValue != null) {
            if (this.jsonValue instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) this.jsonValue;

                if (isIndenting()) {
                    result = jsonArray.toString(getIndentingSize());
                } else {
                    result = jsonArray.toString();
                }
            } else if (this.jsonValue instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) this.jsonValue;

                if (isIndenting()) {
                    result = jsonObject.toString(getIndentingSize());
                } else {
                    result = jsonObject.toString();
                }
            } else if (this.jsonValue instanceof JSONStringer) {
                JSONStringer jsonStringer = (JSONStringer) this.jsonValue;
                result = jsonStringer.toString();
            } else if (this.jsonValue instanceof JSONTokener) {
                JSONTokener jsonTokener = (JSONTokener) this.jsonValue;
                result = jsonTokener.toString();
            }
        } else if (this.jsonRepresentation != null) {
            try {
                result = this.jsonRepresentation.getText();
            } catch (IOException e) {
            	/*
            	 * NOTE(LocalOAuth):
            	 * Android標準のorg.jsonではJSONException(e)が無くエラーになるので、e.getLocalizedMessage()で文字列を渡すように修正。
            	 */
//                throw new JSONException(e);
                throw new JSONException(e.getLocalizedMessage());
            }
        }

        return result;
    }
 
Example 13
Source File: JsonEncoder.java    From egads with GNU General Public License v3.0 4 votes vote down vote up
public static String toJson(Object object) throws Exception {
    JSONStringer jsonOut = new JSONStringer();
    toJson(object, jsonOut);
    return jsonOut.toString();
}
 
Example 14
Source File: MindmupExporter.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Nonnull
private String makeContent(@Nonnull final MindMapPanel panel) {
  final JSONStringer stringer = new JSONStringer();
  writeRoot(stringer, panel.getConfiguration(), panel.getModel().getRoot());
  return stringer.toString();
}
 
Example 15
Source File: SqlDBOperate.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
public String sendUserInfoToJSON() {
    List<UserInfo> users;
    int count = (int) getCountOfUserInfo();
    users = getScrollDataOfUserInfo(0, count);
    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("user");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (UserInfo user : users) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(user.getId());
            jsonText.key("name");
            jsonText.value(user.getName());
            jsonText.key("sex");
            jsonText.value(user.getSex());
            jsonText.key("age");
            jsonText.value(user.getAge());
            jsonText.key("IMEI");
            jsonText.value(user.getIMEI());
            jsonText.key("ip");
            jsonText.value(user.getIPAddr());
            jsonText.key("status");
            jsonText.value(user.getIsOnline());
            jsonText.key("avater");
            jsonText.value(user.getAvater());
            jsonText.key("lastdate");
            jsonText.value(user.getLastDate());
            jsonText.key("device");
            jsonText.value(user.getDevice());
            jsonText.key("constellation");
            jsonText.value(user.getConstellation());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}
 
Example 16
Source File: SqlDBOperate.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
public String sendChattingInfoToJSON(int sendID, int receiverID) {
    List<ChattingInfo> infos;

    infos = getAllMessageFromChattingInfo(sendID, receiverID);

    JSONStringer jsonText = new JSONStringer();
    try {
        // 首先是{,对象开始。object和endObject必须配对使用
        jsonText.object();

        jsonText.key("chatting");

        // 键user的值是数组。array和endArray必须配对使用
        jsonText.array();
        for (ChattingInfo info : infos) {
            jsonText.object();

            jsonText.key("id");
            jsonText.value(info.getId());
            jsonText.key("sendID");
            jsonText.value(info.getSendID());
            jsonText.key("receiverID");
            jsonText.value(info.getReceiverID());
            jsonText.key("chatting");
            jsonText.value(info.getInfo());
            jsonText.key("date");
            jsonText.value(info.getDate());
            jsonText.key("style");
            jsonText.value(info.getStyle());
            jsonText.endObject();
        }
        jsonText.endArray();

        // },对象结束
        jsonText.endObject();
    }
    catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
    return jsonText.toString();
}