Java Code Examples for com.google.gson.JsonObject#getAsString()

The following examples show how to use com.google.gson.JsonObject#getAsString() . 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: FluidRecipeSerializer.java    From the-hallow with MIT License 6 votes vote down vote up
/**
 * Attempts to extract the recipe result count from the given JsonObject.
 * Assumes we're inside the top level of a result block:
 *
 *  "result": {
 *    "item": "minecraft:cobblestone",
 *    "count": 2
 *  }
 *
 * If the count is invalid or is not an int, 0 is returned.
 * @param countJson JsonObject to extract recipe result count from
 * @return recipe result count
 */
private int getCount(JsonObject countJson) {
	int count;
	// get count int
	if(countJson.has(COUNT_KEY)) {
		if (countJson.get(COUNT_KEY).isJsonPrimitive()) {
			JsonPrimitive countPrimitive = countJson.getAsJsonPrimitive(COUNT_KEY);

			if (countPrimitive.isNumber()) {
				count = countPrimitive.getAsNumber().intValue();
			} else {
				throw new IllegalArgumentException("Expected JsonPrimitive to be an int, got " + countJson.getAsString() + "!\n" + prettyPrintJson(countJson));
			}
		} else {
			throw new InvalidJsonException("\"" + ITEM_KEY + "\" needs to be a JsonPrimitive int, found " + countJson.getClass() + "!\n" + prettyPrintJson(countJson));
		}
	} else {
		return 1;
	}

	return count;
}
 
Example 2
Source File: AuthenticatorAttestationResponse.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
/**
 * @return json encoded representation of the AuthenticatorAttestationResponse
 */
public String encode() {
  JsonObject json = new JsonObject();
  json.addProperty("clientDataJSON", BaseEncoding.base64().encode(clientDataBytes));
  try {
    json.addProperty("attestationObject", BaseEncoding.base64().encode(decodedObject.encode()));
  } catch (CborException e) {
    return null;
  }
  return json.getAsString();
}
 
Example 3
Source File: AbstractCache.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public String toJson() throws JsonProcessingException {

        JsonObject retVal = new JsonObject();
        ObjectMapper mapper = mapper();
        Iterator<T> iter = vocabulary.values().iterator();
        Class clazz = null;
        if (iter.hasNext())
            clazz = iter.next().getClass();
        else
            return retVal.getAsString();

        retVal.addProperty(CLASS_FIELD, mapper.writeValueAsString(this.getClass().getName()));

        JsonArray jsonValues = new JsonArray();
        for (T value : vocabulary.values()) {
            JsonObject item = new JsonObject();
            item.addProperty(CLASS_FIELD, mapper.writeValueAsString(clazz));
            item.addProperty(VOCAB_ITEM_FIELD, mapper.writeValueAsString(value));
            jsonValues.add(item);
        }
        retVal.add(VOCAB_LIST_FIELD, jsonValues);

        retVal.addProperty(DOC_CNT_FIELD, mapper.writeValueAsString(documentsCounter.longValue()));
        retVal.addProperty(MINW_FREQ_FIELD, mapper.writeValueAsString(minWordFrequency));
        retVal.addProperty(HUGE_MODEL_FIELD, mapper.writeValueAsString(hugeModelExpected));

        retVal.addProperty(STOP_WORDS_FIELD, mapper.writeValueAsString(stopWords));

        retVal.addProperty(SCAVENGER_FIELD, mapper.writeValueAsString(scavengerThreshold));
        retVal.addProperty(RETENTION_FIELD, mapper.writeValueAsString(retentionDelay));
        retVal.addProperty(TOTAL_WORD_FIELD, mapper.writeValueAsString(totalWordCount.longValue()));

        return retVal.toString();
    }