private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { final String msgPattern ="The JSON object's field <{0>' is not in ISO-8601 format as expected!"; throw JsonParseException.newBuilder() .message(MessageFormat.format(msgPattern, Thing.JsonFields.MODIFIED.getPointer())) .cause(e) .build(); } }
private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { final String msgPattern ="The JSON object's field <{0>' is not in ISO-8601 format as expected!"; throw JsonParseException.newBuilder() .message(MessageFormat.format(msgPattern, Thing.JsonFields.MODIFIED.getPointer())) .cause(e) .build(); } }
private static com.eclipsesource.json.JsonValue tryToRead(final String json) { try { return Json.parse(json); } catch (final ParseException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse ''{0}''!", json)) .cause(e) .build(); } }
private static <T> T tryToParseJsonValue(final String jsonString, final DittoJsonHandler<?, ?, T> dittoJsonHandler) { try { return parseJsonValue(jsonString, dittoJsonHandler); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse JSON string ''{0}''!", jsonString)) .cause(e) .build(); } }
private static com.eclipsesource.json.JsonValue tryToRead(final Reader reader) { try { return Json.parse(reader); } catch (final ParseException | IOException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to parse JSON from reader!") .cause(e) .build(); } }
private static JsonValue tryToReadJsonValueFrom(final Reader reader) { try { return readJsonValueFrom(reader); } catch (final ParseException | IOException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to parse JSON value from reader!") .cause(e) .build(); } }
private static com.eclipsesource.json.JsonArray tryToReadMinimalJsonArrayFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asArray(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON array from string!") .cause(e) .build(); } }
@SuppressWarnings("squid:S1166") private static String tryToDecodeString(final String s, final JsonParseOptions jsonParseOptions) { try { return decode(s, jsonParseOptions); } catch (final Exception e) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(s) // .description("Check if the field selector is correctly URL encoded.") // .cause(e) // .build(); } }
/** * Creates a new {@code SudoRetrieveModifiedThingTags} from a JSON object. * * @param jsonObject the JSON object of which a new SudoRetrieveModifiedThingTags is to be created. * @param dittoHeaders the optional command headers of the request. * @return the SudoRetrieveModifiedThingTags which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws JsonMissingFieldException if the passed in {@code jsonObject} was not in the expected format. */ public static SudoStreamModifiedEntities fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { try { final Instant start = Instant.parse(jsonObject.getValueOrThrow(JSON_START)); final Instant end = Instant.parse(jsonObject.getValueOrThrow(JSON_END)); final Integer burst = jsonObject.getValue(JSON_BURST).orElse(null); final Long timeoutMillis = jsonObject.getValue(JSON_TIMEOUT_MILLIS).orElse(null); return SudoStreamModifiedEntities.of(start, end, burst, timeoutMillis, dittoHeaders); } catch (final DateTimeParseException e) { throw JsonParseException.newBuilder() .message("A given instant is not a valid timestamp.") .description(jsonObject.toString()) .cause(e) .build(); } }
private static com.eclipsesource.json.JsonObject tryToReadJsonObjectFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asObject(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON object from string!") .cause(e) .build(); } }
@SuppressWarnings("squid:S1166") private static String tryToDecodeString(final String s, final JsonParseOptions jsonParseOptions) { try { return decode(s, jsonParseOptions); } catch (final Exception e) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(s) // .description("Check if the field selector is correctly URL encoded.") // .cause(e) // .build(); } }
private T fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final String type = resolveType(jsonObject); final JsonParsable<T> jsonObjectParsable = parseStrategies.get(type); if (null != jsonObjectParsable) { try { return jsonObjectParsable.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException jre) { final JsonExceptionBuilder builder = JsonRuntimeException.newBuilder(jre.getErrorCode()) .message("Error when parsing Json type '" + type + "': " + jre.getMessage()) .cause(jre.getCause()); jre.getDescription().ifPresent(builder::description); jre.getHref().ifPresent(builder::href); // rethrow after enhancing the exception with the type which failed to parse throw (JsonRuntimeException) builder.build(); } } else { throw JsonTypeNotParsableException.newBuilder(type, getClass().getSimpleName()) .dittoHeaders(dittoHeaders) .build(); } }
private T fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final String type = resolveType(jsonObject); final JsonParsable<T> jsonObjectParsable = parseStrategies.get(type); if (null != jsonObjectParsable) { try { return jsonObjectParsable.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException jre) { final JsonExceptionBuilder builder = JsonRuntimeException.newBuilder(jre.getErrorCode()) .message("Error when parsing Json type '" + type + "': " + jre.getMessage()) .cause(jre.getCause()); jre.getDescription().ifPresent(builder::description); jre.getHref().ifPresent(builder::href); // rethrow after enhancing the exception with the type which failed to parse throw (JsonRuntimeException) builder.build(); } } else { throw JsonTypeNotParsableException.newBuilder(type, getClass().getSimpleName()) .dittoHeaders(dittoHeaders) .build(); } }
throw JsonParseException.newBuilder() .message("Could not transform JSON: " + e.getMessage()) .cause(e) .build();