Java Code Examples for org.json.JSONObject#valueToString()

The following examples show how to use org.json.JSONObject#valueToString() . 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: Vout.java    From SPADE with GNU General Public License v3.0 5 votes vote down vote up
public Vout(JSONObject vout) throws JSONException {
    value = vout.getDouble("value");
    n = vout.getInt("n");
    String address;
    for(int i = 0; i < vout.getJSONObject("scriptPubKey").getJSONArray("addresses").length(); i++) { 
        // addresses.add( JSONObject.valueToString( vout.getJSONObject("scriptPubKey").getJSONArray("addresses").getString(i) ) );
        address = JSONObject.valueToString( vout.getJSONObject("scriptPubKey").getJSONArray("addresses").getString(i) );
        addresses.add( address.substring(1, address.length()-1) );
    }
}
 
Example 2
Source File: ProvisioningAction.java    From account-provisioning-for-google-apps with Apache License 2.0 5 votes vote down vote up
/**
 * Method exposed as a REST GET service that suggests usernames. This method doesn't support
 * custom fields. Use the POST service instead. Exposed for testing only.
 *
 * @param firstName The user's first name.
 * @param lastName The user's last name.
 * @return In case of success, it returns a JSON serialized array with suggestions, in case of
 *         error it returns a JSON serialized map with the "errorMessage" index explaining the
 *         error.
 */
@GET
@Path("suggest")
public String suggestGet(@QueryParam("firstname") String firstName,
    @QueryParam("lastname") String lastName) {
  HashMap<String, String> userDataMap = new HashMap<String, String>();
  userDataMap.put(UsernameManager.FIRST_NAME, firstName);
  userDataMap.put(UsernameManager.LAST_NAME, lastName);
  try {
    return JSONObject.valueToString(ProvisioningApp.getInstance().getUsernameManager()
        .suggest(userDataMap));
  } catch (Exception e) {
    return createJSONErrorResponse(e.getMessage());
  }
}
 
Example 3
Source File: ProvisioningAction.java    From account-provisioning-for-google-apps with Apache License 2.0 5 votes vote down vote up
/**
 * @return A JSON serialized map with the following configuration parameters:
 *         suggestedUsernamesTimeout, numberOfSuggestions and domain.
 */
public String getServerConfig() {
  ConfigData config = ProvisioningApp.getInstance().getContext().getConfig();
  long suggestedUsernamesTimeout = config.getSuggestedUsernamesTimeout();
  Integer numberOfSuggestions = config.getNumberOfSuggestions();
  String domain = config.getDomain();
  HashMap<String, String> configMap = new HashMap<String, String>();
  configMap.put("suggestedUsernamesTimeout", String.valueOf(suggestedUsernamesTimeout));
  configMap.put("numberOfSuggestions", numberOfSuggestions.toString());
  configMap.put("domain", domain);
  return JSONObject.valueToString(configMap);
}
 
Example 4
Source File: JSONObjectTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * Confirm that https://github.com/douglascrockford/JSON-java/issues/167 is fixed.
 * The following code was throwing a ClassCastException in the 
 * JSONObject(Map<String, Object>) constructor
 */
@SuppressWarnings("boxing")
@Test
public void valueToStringConfirmException() {
    Map<Integer, String> myMap = new HashMap<Integer, String>();
    myMap.put(1,  "myValue");
    // this is the test, it should not throw an exception
    String str = JSONObject.valueToString(myMap);
    // confirm result, just in case
    Object doc = Configuration.defaultConfiguration().jsonProvider().parse(str);
    assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
    assertTrue("expected myValue", "myValue".equals(JsonPath.read(doc, "$.1")));
}
 
Example 5
Source File: EnumTest.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
/**
 * The default action of valueToString() is to call object.toString().
 * For enums, this means the assigned value will be returned as a string.
 */
@Test
public void enumValueToString() {
    String expectedStr1 = "\"VAL1\"";
    String expectedStr2 = "\"VAL1\"";
    MyEnum myEnum = MyEnum.VAL1;
    MyEnumField myEnumField = MyEnumField.VAL1;
    MyEnumClass myEnumClass = new MyEnumClass();
    
    String str1 = JSONObject.valueToString(myEnum);
    assertTrue("actual myEnum: "+str1+" expected: "+expectedStr1,
            str1.equals(expectedStr1));
    String str2 = JSONObject.valueToString(myEnumField);
    assertTrue("actual myEnumField: "+str2+" expected: "+expectedStr2,
            str2.equals(expectedStr2));

    /**
     * However, an enum within another class will not be rendered
     * unless that class overrides default toString() 
     */
    String expectedStr3 = "\"org.json.junit.data.MyEnumClass@";
    myEnumClass.setMyEnum(MyEnum.VAL1);
    myEnumClass.setMyEnumField(MyEnumField.VAL1);
    String str3 = JSONObject.valueToString(myEnumClass);
    assertTrue("actual myEnumClass: "+str3+" expected: "+expectedStr3,
            str3.startsWith(expectedStr3));
}
 
Example 6
Source File: Task.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public <T> T getResultEntryTyped(String key, Class<T> clazz){
	String json = getEntity().getResult();
	if (json != null) {
		JSONObject map = new JSONObject(json);
		try {
			String valueToString = JSONObject.valueToString(map.get(key));
			return gson.fromJson(valueToString, clazz);
		} catch (JSONException e) {
			// do nothing
		}
	}
	return null;
}
 
Example 7
Source File: GsonTransformer.java    From captain with Apache License 2.0 4 votes vote down vote up
@Override
public String render(Object model) {
	return JSONObject.valueToString(model);
}