/** * Returns the JSON string for this value in its minimal form, without any additional whitespace. * The result is guaranteed to be a valid input for the method {@link #readJSON(String)} and to * create a value that is <em>equal</em> to this object. * * @return a JSON string that represents this value */ @Override public String toString() { return toString(Stringify.PLAIN); }
/** * Returns the JSON string for this value in its minimal form, without any additional whitespace. * The result is guaranteed to be a valid input for the method {@link #readJSON(String)} and to * create a value that is <em>equal</em> to this object. * * @return a JSON string that represents this value */ @Override public String toString() { return toString(Stringify.PLAIN); }
/** * Returns this JSON value as {@link JsonObject}, assuming that this value represents a JSON * object. If this is not the case, an exception is thrown. * * @return a JSONObject for this value * @throws UnsupportedOperationException if this value is not a JSON object */ public JsonObject asObject() { throw new UnsupportedOperationException("Not an object: "+toString()); }
/** * Returns this JSON value as String, assuming that this value represents a JSON string. If this * is not the case, an exception is thrown. * * @return the string represented by this value * @throws UnsupportedOperationException if this value is not a JSON string */ public String asString() { throw new UnsupportedOperationException("Not a string: "+toString()); }
/** * Returns this JSON value as a <code>boolean</code> value, assuming that this value is either * <code>true</code> or <code>false</code>. If this is not the case, an exception is thrown. * * @return this value as <code>boolean</code> * @throws UnsupportedOperationException if this value is neither <code>true</code> or <code>false</code> */ public boolean asBoolean() { throw new UnsupportedOperationException("Not a boolean: "+toString()); }
/** * Returns this JSON value as {@link JsonObject}, assuming that this value represents a JSON * object. If this is not the case, an exception is thrown. * * @return a JSONObject for this value * @throws UnsupportedOperationException if this value is not a JSON object */ public JsonObject asObject() { throw new UnsupportedOperationException("Not an object: "+toString()); }
/** * Returns this JSON value as a <code>double</code> value, assuming that this value represents a * JSON number. If this is not the case, an exception is thrown. * <p> * If the JSON number is out of the <code>Double</code> range, {@link Double#POSITIVE_INFINITY} or * {@link Double#NEGATIVE_INFINITY} is returned. * </p> * * @return this value as <code>double</code> * @throws UnsupportedOperationException if this value is not a JSON number */ public double asDouble() { throw new UnsupportedOperationException("Not a number: "+toString()); }
/** * Returns this JSON value as {@link JsonArray}, assuming that this value represents a JSON array. * If this is not the case, an exception is thrown. * * @return a JSONArray for this value * @throws UnsupportedOperationException if this value is not a JSON array */ public JsonArray asArray() { throw new UnsupportedOperationException("Not an array: "+toString()); }
/** * Returns this JSON value as String, assuming that this value represents a JSON string. If this * is not the case, an exception is thrown. * * @return the string represented by this value * @throws UnsupportedOperationException if this value is not a JSON string */ public String asString() { throw new UnsupportedOperationException("Not a string: "+toString()); }
/** * Returns this JSON value as a <code>float</code> value, assuming that this value represents a * JSON number. If this is not the case, an exception is thrown. * <p> * If the JSON number is out of the <code>Float</code> range, {@link Float#POSITIVE_INFINITY} or * {@link Float#NEGATIVE_INFINITY} is returned. * </p> * * @return this value as <code>float</code> * @throws UnsupportedOperationException if this value is not a JSON number */ public float asFloat() { throw new UnsupportedOperationException("Not a number: "+toString()); }
private void readAccountsFromJsonResource() { try (Reader reader = new InputStreamReader(jsonResource.getInputStream(), StandardCharsets.UTF_8)) { final TypeReference<Map<String, JsonBackedAccount>> personList = new TypeReference<>() { }; this.jsonBackedAccounts = MAPPER.readValue(JsonValue.readHjson(reader).toString(), personList); } catch (final Exception e) { LOGGER.warn(e.getMessage(), e); } }
@SneakyThrows private void loadResource(final Resource res) { try (Reader reader = new InputStreamReader(res.getInputStream(), StandardCharsets.UTF_8)) { val personList = new TypeReference<Map<String, UserAuthorizationDefinition>>() { }; this.rules = this.objectMapper.readValue(JsonValue.readHjson(reader).toString(), personList); } }
@Override @SneakyThrows public void to(final OutputStream out, final T object) { try (val writer = new StringWriter()) { this.objectMapper.writer(this.prettyPrinter).writeValue(writer, object); val hjsonString = isJsonFormat() ? JsonValue.readHjson(writer.toString()).toString(Stringify.HJSON) : writer.toString(); IOUtils.write(hjsonString, out, StandardCharsets.UTF_8); } }
/** * Read json from stream. * * @param json the json * @return the string * @throws IOException the io exception */ protected String readJsonFrom(final InputStream json) throws IOException { return isJsonFormat() ? JsonValue.readHjson(IOUtils.toString(json, StandardCharsets.UTF_8)).toString() : String.join("\n", IOUtils.readLines(json, StandardCharsets.UTF_8)); }
@SneakyThrows private void readResourceForInterrupts() { this.interrupts = new LinkedHashMap<>(); if (ResourceUtils.doesResourceExist(resource)) { try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { final TypeReference<Map<String, InterruptResponse>> personList = new TypeReference<>() { }; this.interrupts = MAPPER.readValue(JsonValue.readHjson(reader).toString(), personList); } } } }
@Override public T fromJson(final String json) { try { final String jsonString = JsonValue.readHjson(json).toString(); return this.objectMapper.readValue(jsonString, getTypeToSerialize()); } catch (final Exception e) { throw new IllegalArgumentException(e); } }
@Override public String render(final Map<String, Object> model, final AccessToken accessToken) { val value = getRenderedUserProfile(model); LOGGER.debug("Final user profile is [{}]", JsonValue.readHjson(value).toString(Stringify.FORMATTED)); return value; }
@Override @SneakyThrows public T from(final File json) { val string = isJsonFormat() ? JsonValue.readHjson(FileUtils.readFileToString(json, StandardCharsets.UTF_8)).toString() : FileUtils.readFileToString(json, StandardCharsets.UTF_8); return readObjectFromString(string); }
@Override @SneakyThrows public T from(final Reader json) { val string = isJsonFormat() ? JsonValue.readHjson(json).toString() : String.join("\n", IOUtils.readLines(json)); return readObjectFromString(string); }
@Override @SneakyThrows public T from(final String json) { val jsonString = isJsonFormat() ? JsonValue.readHjson(json).toString() : json; return readObjectFromString(jsonString); }