public Config getNestedOrderedOrGetEmpty(String key) { JsonNode value = getNode(key); if (value == null) { value = newObjectNode(); } else if (value.isArray()) { Config config = new Config(mapper); Iterator<JsonNode> ite = ((ArrayNode) value).elements(); while (ite.hasNext()) { JsonNode nested = ite.next(); if (!(nested instanceof ObjectNode)) { throw new RuntimeJsonMappingException("Expected object but got "+nested); } // here assumes config is an order-preserving map config.setAll(new Config(mapper, (ObjectNode) nested)); } return config; } else if (!value.isObject()) { throw new ConfigException("Parameter '"+key+"' must be an object or array of objects"); } return new Config(mapper, (ObjectNode) value); }
@Override public Object decode(Response response, Type type) throws IOException, FeignException { if (response.body() == null) { return null; } Reader reader = response.body().asReader(); if (!reader.markSupported()) { reader = new BufferedReader(reader, 1); } try { // Read the first byte to see if we have any data reader.mark(1); if (reader.read() == -1) { return null; // Eagerly returning null avoids "No content to map due to end-of-input" } reader.reset(); return mapper.readValue(reader, mapper.constructType(type)); } catch (RuntimeJsonMappingException e) { Throwable cause = e.getCause(); if (cause != null && cause instanceof IOException) { throw IOException.class.cast(cause); } throw e; } }
@Override public Object read() throws IOException { if (closed) { throw new IOException("The parser is closed"); } try { Object value = null; switch (mode) { case ARRAY_OBJECTS: value = readObjectFromArray(); break; case MULTIPLE_OBJECTS: value = readObjectFromStream(); break; } return value; } catch (RuntimeJsonMappingException ex) { throw new JsonParseException(ex.toString(), jsonParser.getTokenLocation(), ex); } }
@Override public Object decode(Response response, Type type) throws IOException { if (response.status() == 404) return Util.emptyValueOf(type); if (response.body() == null) return null; Reader reader = response.body().asReader(); if (!reader.markSupported()) { reader = new BufferedReader(reader, 1); } try { // Read the first byte to see if we have any data reader.mark(1); if (reader.read() == -1) { return null; // Eagerly returning null avoids "No content to map due to end-of-input" } reader.reset(); return mapper.readValue(reader, mapper.constructType(type)); } catch (RuntimeJsonMappingException e) { if (e.getCause() != null && e.getCause() instanceof IOException) { throw IOException.class.cast(e.getCause()); } throw e; } }
/** * {@inheritDoc} */ @Override public Object decode(final Response response, final Type type) throws IOException { if ( response.status() == HttpURLConnection.HTTP_NO_CONTENT || response.body() == null || (response.body().length() != null && response.body().length() == 0) ) { return null; } try (final Reader reader = response.body().asReader()) { return this.mapper.readValue(reader, this.mapper.constructType(type)); } catch (final JsonMappingException jme) { // The case where for whatever reason (most likely bad design) where the server returned OK and // trying to de-serialize the content had no content (e.g. the return status should have been no-content) if (response.status() == HttpURLConnection.HTTP_OK && jme.getMessage().startsWith(NO_CONTENT_MESSAGE)) { return null; } throw jme; } catch (final RuntimeJsonMappingException e) { if (e.getCause() != null && e.getCause() instanceof IOException) { throw IOException.class.cast(e.getCause()); } throw e; } }
private ObjectNode normalizeValidateObjectNode(Object object) throws IOException { JsonNode node = treeObjectMapper.readTree(treeObjectMapper.writeValueAsString(object)); if (!node.isObject()) { throw new RuntimeJsonMappingException("Expected object to load Config but got "+node); } return (ObjectNode) node; }
@JsonCreator public static Config deserializeFromJackson(@JacksonInject ObjectMapper mapper, JsonNode object) { if (!object.isObject()) { throw new RuntimeJsonMappingException("Expected object but got "+object); } return new Config(mapper, (ObjectNode) object); }
/** * Merges the current application configuration with the provided one. * * @param config The configuration to retrieve values from. */ public void merge(Map<String, String> config) { String languageCode = getString(config, "language"); Locale theLanguage; if (languageCode != null) { theLanguage = Locale.forLanguageTag(languageCode); } else { theLanguage = Locale.getDefault(); } setLanguage(theLanguage); pageSizeForText = getInt(config, "paging.textPageSize"); pageSizeForImages = getInt(config, "paging.imagePageSize"); pageSizeForAlbums = getInt(config, "paging.albumPageSize"); thumbnailWidth = getInt(config, "thumbnail.width"); thumbnailHeight = getInt(config, "thumbnail.height"); // Load the header links as JSON data headerLinksSpec = getString(config, "header.quickLinks", "[]"); JsonFactory jsonFactory = new JsonFactory(); ObjectMapper jsonMapper = new ObjectMapper(jsonFactory); CollectionType jsonType = jsonMapper.getTypeFactory().constructCollectionType(ArrayList.class, HeaderLink.class); List<HeaderLink> links = new ArrayList<>(); try { LOGGER.debug("Header links: {}", headerLinksSpec); JsonParser jsonParser = jsonFactory.createParser(headerLinksSpec); links = jsonMapper.<List<HeaderLink>> readValue(jsonParser, jsonType); } catch (RuntimeJsonMappingException | IOException e) { LOGGER.warn("Failed to load the header configuration", e); } headerLinks = links; }